我通过 DQL 中的常规 LEFT JOIN 选择 3 个实体。它们通过连接表关联,连接表还定义了实体以及带注释的关系。
查询执行没有问题,但我的结果作为平面数组返回。我期望一个包含三个实体作为每个索引的数组元素的数组:
SELECT e1, e2, e3 FROM AppBundle:EntityOne
JOIN AppBundle:JoinEntityOneWithTwo e1_2 WITH e1_2.entity1 = e1.id
JOIN AppBundle:EntityTwo e2 WITH e1_2.entity2 = e2.id
JOIN AppBundle:JoinEntityOneWithThree e1_3 WITH e1_3.entity1 = e1.id
JOIN AppBundle:EntityThree e3 WITH e3.id = e1_3.entity3
WHERE e1.some_field IN ('some','values','in','e1');
当我打电话时getResult()
在查询中,无论是作为对象还是数组进行水合,我都会得到一个平坦的结果集:
array(
/AppBundle/Entity/EntityOne ( ... ),
/AppBundle/Entity/EntityTwo ( ... ),
/AppBundle/Entity/EntityThree ( ... ),
/AppBundle/Entity/EntityOne ( ... ),
/AppBundle/Entity/EntityTwo ( ... ),
/AppBundle/Entity/EntityThree ( ... ),
/AppBundle/Entity/EntityTwo ( ... ),
/AppBundle/Entity/EntityThree ( ... ),
/AppBundle/Entity/EntityOne ( ... ),
)
我期望或喜欢有一个多维数组:
Array(
[0] => Array(
[0] /AppBundle/Entity/EntityOne,
[1] /AppBundle/Entity/EntityTwo,
[2] /AppBundle/Entity/EntityThree
),
[1] => . . .
)
结果不按行关联。它们也没有按照任何可预测的顺序进行分组array_chunk()
生成的sql运行良好。 DQL 返回准确的结果 - 它们只是没有按照我期望的方式制定。我正在遵循有关急切加载连接的 Doctrines(难以捉摸的)文档:
http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html
这个问题非常类似于
Doctrine DQL 返回多种类型的实体 https://stackoverflow.com/questions/27175873/doctrine-dql-returns-multiple-types-of-entities
但我的选择顺序不同,因为我的连接表是多对多的。
非常感谢!
有人问了这个问题的一个变体:
以 SQL 方式获取 Doctrine DQL 结果 https://stackoverflow.com/questions/37545979/getting-doctrine-dql-results-the-sql-way?rq=1
我很惊讶学说不支持通过连接表进行预加载。