如何使用 Symfony 和 Doctrine Query Builder 执行连接查询

2022-01-16 00:00:00 sql php symfony doctrine

我有两个通过 1:1 关系连接的实体,例如:MyEntity.idRelatedEntity我想创建一个 Doctrine 查询,我可以根据 RelatedEntity 中某个列的值从 MyEntity 检索数据.像这样的东西(当然不起作用):

I have two entities which are connected through a 1:1 relationship, e.g: MyEntity.idRelatedEntity I want to create a Doctrine query where I can retrieve data from MyEntity depending on a value from a certain column in RelatedEntity. Something like this (it doesn't work of course):

$entity = $em
    ->getRepository('MyBundle:RelatedEntity')
    ->createQueryBuilder('e')
    ->leftJoin('MyBundle:RelatedEntity', 'r')
    ->where('r.foo = 1')
    ->getQuery()
    ->getResult();

任何帮助将不胜感激:)

Any help would be much appreciated :)

推荐答案

$entity = $em
    ->getRepository('MyBundle:MyEntity')
    ->createQueryBuilder('e')
    ->join('e.idRelatedEntity', 'r')
    ->where('r.foo = 1')
    ->getQuery()
    ->getResult();

左连接在这里也没有任何意义(因为 where 子句会让它像内连接一样工作)

Also left join makes no sense here (because of where clause that will make it work like inner join)

相关文章