I’m joining tables and I’d like to get multidimensional query results based on my joins.
# AppBundle:ProductRepository $this->createQueryBuilder('pr') ->select('pk.id', 'pk.name', 'pr.id', 'pr.label') ->join('pr.package', 'pk') ->getQuery()->getResult();
As you can see Product
is linked to a Package
(many to one)
Here is the kind of result I’m getting:
array( array('id' => '1', 'name' => 'package 1', 'id1' => 1, 'label' => 'product 1'), array('id' => '1', 'name' => 'package 1', 'id1' => 2, 'label' => 'product 2'), array('id' => '2', 'name' => 'package 2', 'id1' => 3, 'label' => 'product 3'), )
Here is what I’d like to get:
array( array( 'id' => '1', 'name' => 'package 1', 'products' => array( array( 'id' => 1, 'label' => 'product 1', ), array( 'id' => 2, 'label' => 'product 2', ), ), ), array( 'id' => '2', 'name' => 'package 2', 'products' => array( array( 'id' => 3, 'label' => 'product 3', ), ), ), )
Is there a way to hydrate this kind of multidimensional array with Doctrine 2 ?
I mean with Doctrine 2, not with “post prod” php loops
Advertisement
Answer
You need a combination of partial results with an array hydrator. Try:
$this->createQueryBuilder('pr') ->select('partial pk.{id, name}, partial pr.{id, label}') ->join('pr.package', 'pk') ->getQuery() ->getArrayResult() ;