我在一个entity1关系中有两个实体(entity2和ManyToMany )。如果我只需要一个实体的对象,那么所有的表都可以正常工作。实体中的字段被标记为ManyToMany,包括mappedBy和reversedBy属性。我现在需要一个来自entity1的子集,它基于entity2上的where子句。
在SQL中,这很简单。
select
a.field1,a.field2
from entity1 as a
left join entity1_entity2 as b on a.entity1_id=b.foreignKey1
left join entity2 as c on b.foreignKey2=c.entity2_id
where c.whereField = 'value'
and a.whereField = 'anotherValue'我想使用来自entity1的存储库,并尝试了createQueryBuilder的查询。
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin(Entity2::class, 'e2')
->where("e2.whereField2= :whereE2")
->andWhere('e1.whereField1= :whereE1')
->setParameter("whereE2", $whereE2)
->setParameter("whereE1", $whereE1);结果看起来像一个完全的外部连接。DQL/SQL的转储似乎没有使用连接表。为什么?
发布于 2018-11-21 13:00:25
您不必在innerJoin子句中指定实体。教义可以自己做到这一点,也可以使用连接表。
例如,entity1作为secondEntities属性上的一个ManyToMany注释。
queryBuilder看起来可能如下:
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin('e1.secondEntities', 'e2')
->andWhere('e1.whereField1= :whereE1') // You can add additionnal conditions ;)
->setParameter("whereE1", $whereE1)
;注意:您可以使用ON连接条件,如:->innerJoin('e1.secondEntities', 'e2', Join::WITH, 'CONDITION HERE')
https://stackoverflow.com/questions/53412152
复制相似问题