I need to get records from the database as an associative array. I have greenhouses, plants and families that are related to connections (One to Many). Plants grow in greenhouses, plants have families, for a greenhouse you need to get families. Therefore, the greenhouse should be the key - family value. My attempt:

public function getFamiliesInGardens($client) { $qb = $this->createQueryBuilder('g'); $qb ->select(['g.name as garden', 'family.name', 'family.id']) ->join('g.growing', 'growing') ->join('growing.plant', 'plant') ->join('plant.family', 'family') ->where('g.client = :client') ->orderBy('g.name') ->addOrderBy('growing.endDate', 'DESC') ->setParameter('client', $client); $grow = $qb->getQuery()->getArrayResult(); return $grow; } 

I get:

 [0] => array(3) { ["garden"]=> string(1) "1" ["name"]=>string(9) "Brassicas" ["id"]=> int(13) } [1] => array(3) { ["garden"]=> string(1) "1" ["name"]=> string(13) "Miscellaneous" ["id"]=> int(18) } 

But I expect:

 [0] => array(1) { ["1"] => array(2) { [0] => array(2) { ["name"] =>string(9) "Brassicas" ["id"] => int(13) }, [1] => array(2) { ["name"]=>string(9) "Miscellaneous" ["id"]=> int(18) }, } } 

If you add grouping by greenhouses, the result will be the same, but the greenhouse will be repeated once with one family. What needs to be changed to get an array: garden => families?

    1 answer 1

    Try it like this. $grow = $query->getResult(Query::HYDRATE_ARRAY); Unfortunately I can not check now.

    • The same result. So far, I have done this by simply iterating over the array. - Valentine Murnik