SQL / Doctrine: getResult in leftJoin returns an object and a reference object as 2 records in an array

I have the following problem in Symfony2 / Doctrine-Environment:

Class a is related to class b, however this relationship is not registered through ORM annotations or anything else, since the reference table is a variable. Therefore, I join as follows:

$query = $this->createQueryBuilder('a') ->select('a') ->addSelect('b') ->leftJoin('My\Bundle\EntityBundle\Entity\OtherClass','b',\Doctrine\ORM\Query\Expr\Join::WITH,'a.referenceId=b.id') ->getQuery()->getResult($hydrationMode); 

As a result, the array now contains both objects a and b (sort of like

 array('a1', 'b1', 'a2', 'b2', .... ) 

) I could filter it again, but I feel this is not the way to go. I tried different hydration modes, but that didnโ€™t change anything.

Is there a way to get it back, so the association is saved?

By this I mean sth as the following:

 array(array('a1', 'b1'), array('a2', 'b2'), ...) 

?

+5
source share
1 answer

You can achieve this using the standard ScalarHydrator:

 $query = $this->createQueryBuilder('a') ->select('a') ->addSelect('b') ->leftJoin('My\Bundle\EntityBundle\Entity\OtherClass','b','WITH','a.referenceId=b.id') ->getQuery() ->getResult(\Doctrine\ORM\Query::HYDRATE_SCALAR); 
+9
source

Source: https://habr.com/ru/post/1201813/


All Articles