symfony - How to get a collection of related entities by using Doctrine ResultSetMapping? -
i use doctrine 2.3.4. , symfony 2.3.0
i have 2 entities: person
, application
.
application gets created when person applies job.
relation person
application
onetomany
, bidirectional.
using regular doctrine documentation here managed correct result set when working single entity. however, when add joined entity, collection of root entities joined wrong related entity.
in other words, problem collection of applications having same person.
native sql query, when executed directly returns correct result.
this code:
$sql = "select a.id, a.job, p.first_name, p.last_name application inner join person p on a.person_id = p.id"; $rsm = new resultsetmapping; $rsm->addentityresult('\company\department\domain\model\application', 'a'); $rsm->addfieldresult('a','id','id'); $rsm->addfieldresult('a','job','job'); $rsm->addjoinedentityresult('\company\department\domain\model\person' , 'p', 'a', 'person'); $rsm->addfieldresult('p','first_name','firstname'); $rsm->addfieldresult('p','last_name','lastname'); $query = $this->em->createnativequery($sql, $rsm); $result = $query->getresult(); return $result;
here entity classes:
namespace company\department\domain\model; use doctrine\orm\mapping orm; /** * person * * @orm\entity * @orm\table(name="person") */ class person { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string first name * * @orm\column(name="first_name",type="string",length=255) */ private $firstname; /** * @var string last name * * @orm\column(name="last_name",type="string",length=255) */ private $lastname; /** * * @var applications[] * @orm\onetomany(targetentity="application", mappedby="person") */ private $applications;
application class:
namespace company\department\domain\model; use doctrine\orm\mapping orm; /** * application (person applied job) * * @orm\entity * @orm\table(name="application") */ class application { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var person * * @orm\manytoone(targetentity="person", inversedby="applications") * @orm\joincolumn(name="person_id", referencedcolumnname="id") */ private $person; /** * @var string * @orm\column(name="job",type="string", length=100) */ private $job;
i must missing here?
found out error was:
the person->id
property has mapped too. also, order of columns in select
clause has match order of addfieldresult()
statements.
therefore, $sql
should this:
select a.id, a.job, p.id personid, p.first_name, p.last_name application inner join person p on a.person_id=p.id
and mapping related property this:
$rsm->addjoinedentityresult('\company\department\domain\model\person' , 'p', 'a', 'person'); $rsm->addfieldresult('p','personid','id'); $rsm->addfieldresult('p','first_name','firstname'); $rsm->addfieldresult('p','last_name','lastname');
so, mapped field result column name corresponds sql result column name, , third parameter, id
in case, should property actual name.
Comments
Post a Comment