sql – Doctrine 2.1其中外键id =?,编辑:在Doctrine 2.2中修复
我在这里看了很多答案,这些答案与Doctrine 2.1中严重缺乏功能有关,这可能是OOP正确性超越关系理智的结果. 我有两个表,有多对一的关系,文章和成员.会员可以发表许多文章.拥有方的注释是 /** * @var Member * @ORMManyToOne(targetEntity="Member") * @ORMJoinColumns({ * @ORMJoinColumn(name="member_id",referencedColumnName="id") * }) */ private $member; 我想获得成员6的所有活动文章,这是一个简单的SQL查询: SELECT * FROM mbr_article WHERE active = 1 AND member_id = 6 ORDER BY article_id DESC 我最终得到的是 $rep = $this->getDoctrine()->getRepository('SMWMemberBundle:Article'); $q = $rep->createQueryBuilder('a') ->leftJoin('a.member','m') ->where('m.id = ?1') ->andWhere('a.active = 1') ->orderBy('a.id','DESC') ->setParameter(1,$id) ->getQuery(); 哪个产生了 SELECT m0_.id AS id0,m0_.active AS active1,m0_.update_time AS update_time2,m0_.content AS content3,m0_.member_id AS member_id4 FROM mbr_article m0_ LEFT JOIN mbr_member m1_ ON m0_.member_id = m1_.id WHERE m1_.id = ? AND m0_.active = 1 ORDER BY m0_.id DESC 哪个工作,可能不会慢很多,但不需要JOIN,因为我已经有了Member对象.当我以另一种方式尝试时,我得到的所有文章不仅仅是活跃的文章. 我已经看到像Can you get a foreign key from an object in Doctine2 without loading that object?那样使用getEntityIdentifier的响应,并提到2.2中的改进,我可以说是IDENTITY(成员). 在Doctrine 2.1中有没有合理的方法呢?增强是否允许在查询构建器中使用andWhere(‘IDENTITY(member)=?’)? 编辑: 感谢@Ocramius,– >其中(‘IDENTITY(a.member)=?1’)在Doctrine 2.2中有效 解决方法IDENTITY适用于Doctrine 2.2中查询构建器生成的WHERE子句.$q = $rep->createQueryBuilder('a') ->where('IDENTITY(a.member) = ?1') ->andWhere('a.active = 1') ->orderBy('a.id','DESC') ->setParameter(1,$id) ->getQuery(); 生成以下SQL: SELECT m0_.id AS id0,m0_.member_id AS member_id4 FROM mbr_article m0_ WHERE m0_.member_id = ? AND m0_.active = 1 ORDER BY m0_.id DESC (编辑:南京站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |