php - One-To-Many DB tables rellation with Doctrine2 -
i have following setup of related db tables:
organization +--------+---------+ | id | integer | +--------+---------+ | name | string | +--------+---------+ division +---------------+---------+ | id | integer | +--------+----------------+ |organization_id| integer | +---------------+---------+ | name | string | +---------------+---------+ subdivision +---------------+---------+ | id | integer | +--------+----------------+ | division_id | integer | +---------------+---------+ | name | string | +---------------+---------+
i'm using symfony2 doctrine2 orm , fosrestbundle.
now got confused association mapping.
when require organization, following
{ id: 1, name: "organization1", divisions: [ { id: 1, organization_id: 1, name: "division1" subdivisions: [ { id: 1, division_id: 1, name: "subdivision1" } ] } ] }
i figured out one, , used one-to-many bidirectional association mapping of doctrine.
what gave me headache, opposite direction of relation. if require specifig subdivision, following:
{ id: 1, division_id: 1, name: "subdivision1", division: { id: 1, organization_id: 1, name: "division1" organization: { id: 1, name: "organization" } } ] }
but this:
{ id: 1, division_id: 1, name: "subdivision1", division: { id: 1, organization_id: 1, name: "division1", organization: { id: 1, name: "organization1", divisions: [/* ..list of other divisions.. */] }, subdivisions: [/* ..list of other subdivisions.. */] }
}
how can remove subdivisions , divisions returned data using doctrine?
because need subdivision, division belongs , organization division belongs to. , listing tree takes huge amount off data , time, while don't need all.
edit:
i'm using following retrieve records (everythings works espected):
$organization = $this ->getdoctrine() ->getrepository('mytestbundle:organization') ->find($id); $organization = $this ->getdoctrine() ->getrepository('mytestbundle:organization') ->findall();
and opposite direction:
$subdivision = $this ->getdoctrine() ->getrepository('mytestbundle:subdivision') ->find($id);
this generates following sql:
select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.id = ? ["11"] [] select t0.id id1, t0.organization_id organization_id2, t0.name name3, t0.organization_id organization_id4 division t0 t0.id = ? [16] [] select t0.id id1, t0.name name2 organization t0 t0.id = ? [15] [] select t0.id id1, t0.organization_id organization_id2, t0.name name3, t0.organization_id organization_id4 division t0 t0.organization_id = ? [15] [] select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.division_id = ? [15] [] select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.division_id = ? [18] [] select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.division_id = ? [19] [] select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.division_id = ? [45] [] select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.division_id = ? [49] [] select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.division_id = ? [51] [] select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.division_id = ? [56] [] select t0.id id1, t0.division_id division_id2, t0.name name3, t0.division_id division_id4 subdivision t0 t0.division_id = ? [16] []
replace $subdivision ... ->find($id)
with
$subdivision = $this ->getdoctrine() ->getrepository('mytestbundle:subdivision') ->createquerybuilder('s') ->leftjoin('s.division', 'd') ->leftjoin('d.organization', 'o') ->where('s.id = :id') ->setparameter('id', $id) ->getquery() ->getsingleresult();
this should lower count of request made doctrine. of course divisions
property of organization
still exist, content not automatically loaded first query. if access property via getdivisions()
content loaded database. feature of doctrine load necessary data without having developer thinking it. of course not performant way , have adopte needs. have @ lazy loading , related topics.
Comments
Post a Comment