Symfony querybuilder – Cannot select extra field and OneToMany relationship

I am building a query throw querybuilder objects, something like

$qb = $em->createQueryBuilder()
->select('n, t, ns1m')
->from('AppEntityNuclei','n')
->leftJoin('n.statistiche1M', 'ns1m', 'WITH', 'DATE_PART('year', ns1m.dataora) = YEAR(CURRENT_DATE())')
->leftJoin('n.codicitag', 't')
->where('n.comune = :id_comune')
->setParameter('id_comune', $this->user->getComune()->getId());

This query return an array of entity objects "nucleo":

array:3 [▼
    0 => AppEntityNuclei { ... }
    1 => AppEntityNuclei { ... }
    2 => AppEntityNuclei { ... }

I want to add another column to select

->addSelect('SUM(ns1m.totale_peso_conferimenti_indifferenziata) AS total_test')

but now the result is:

array:2 [▼
  0 => array:2 [▼
    0 => AppEntityNuclei {#1671 ▶}
    "total_test" => 1520
  ]
  1 => array:2 [▶]
]

n.codicitag is a onetomany relations and is a collections of other objects

how can to handle this? Made many tests without success

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *