问题描述
我正在通过DQL中的常规LEFT JOIN选择3个实体.它们通过联接表进行关联,联接表也具有为其定义的实体以及带注释的关系.查询执行没有问题,但是我的结果以平面数组形式返回.我希望将具有三个实体的数组作为每个索引的数组元素:
I am selecting 3 entities via regular LEFT JOIN in DQL. They are related via join tables which also have entities defined for them as well as annotated relationships.The query executes without issue but my results are returned as a flat array. I would expect an array with the three entities as array elements for each index:
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()
时,无论是作为对象还是数组进行补水,我都会得到一个平坦的结果集:
When I call getResult()
on the query, either hydrating as an object or an array, I get a flat results set:
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 ( ... ),
)
我希望或希望有一个多维数组:
I would expect, or like to have a multi dimensional array:
Array(
[0] => Array(
[0] /AppBundle/Entity/EntityOne,
[1] /AppBundle/Entity/EntityTwo,
[2] /AppBundle/Entity/EntityThree
),
[1] => . . .
)
结果与行无关.它们也不是我可以使用 array_chunk()
The results are not related by row. Nor are they in any predictable order that I can group them by with array_chunk()
生成的sql运行正常.DQL返回准确的结果-只是没有按照我期望的方式制定它们.我正在遵循有关急切加载联接的学说(难以捉摸)的文档:
The generated sql runs fine. The DQL returns accurate results -- they're just not formulated in a way that I would expect. I am following Doctrines (elusive) documentation on eager loading joins:
这个问题与
但是我的选择顺序有所不同,因为我的联接表是多对多的.非常感谢!
But my select order is different as my join tables are many-to-many.Much thanks!
有人问了这个问题的一个变种:
A variant of this question was asked:
我很惊讶该学说不支持通过联接表进行紧急加载.
I'm pretty surprised that doctrine does not support eager loading via join table.
推荐答案
这是我能想到的最好的方法:
Here is the best that I could come up with:
//in my entity repository:
public function getFoo($hydrate = true) {
$sql = "SELECT e1, e2, e3 FROM entity_one
JOIN entity_one_entity_two e1_2 ON e1_2.entity_one_id = e1.id
JOIN entity_two e2 ON e1_2.entity_two_id = e2.id
JOIN entity_one_entity_three e1_3 ON e1_3.entity_one_id = e1.id
JOIN entity_three e3 ON e3.id = e1_3.entity_three.id
WHERE e1.some_field IN ('some','values','in','e1')";
$stmt = $con->prepare($sql);
$stmt->execute();
return ($hydrate)
? $this->hydrateResponses($stmt->fetchAll(PDO::FETCH_ASSOC))
: $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function hyrdateResponses($responses) {
//call find by ids on repositories from array.
}
这很好用,因为当您只想从一个查询中获取结果时,您执行的查询数量是3倍!
This works great because you're performing 3XN the number of queries when you're only trying to get results from one!
这篇关于Dctrine DQL从连接返回平面数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!