i selecting 3 entities via regular left join in dql. related via join tables have entities defined them annotated relationships. query executes without issue results returned flat array. expect array 3 entities array elements each index:
select e1, e2, e3 appbundle:entityone join appbundle:joinentityonewithtwo e1_2 e1_2.entity1 = e1.id join appbundle:entitytwo e2 e1_2.entity2 = e2.id join appbundle:joinentityonewiththree e1_3 e1_3.entity1 = e1.id join appbundle:entitythree e3 e3.id = e1_3.entity3 e1.some_field in ('some','values','in','e1');
when call getresult()
on query, either hydrating object or array, 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 expect, or have multi dimensional array:
array( [0] => array( [0] /appbundle/entity/entityone, [1] /appbundle/entity/entitytwo, [2] /appbundle/entity/entitythree ), [1] => . . . )
the results not related row. nor in predictable order can group them array_chunk()
the generated sql runs fine. dql returns accurate results -- they're not formulated in way expect. following doctrines (elusive) documentation on eager loading joins:
this question similar
doctrine dql returns multiple types of entities
but select order different join tables many-to-many. thanks!
a variant of question asked:
getting doctrine dql results sql way
i'm pretty surprised doctrine not support eager loading via join table.
here best come with:
//in entity repository: public function getfoo($hydrate = true) { $sql = "select e1, e2, e3 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 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 ids on repositories array. }
this works great because you're performing 3xn number of queries when you're trying results one!
Comments
Post a Comment