Assume I have Product
entities and Review
entities attached to products. Is it possible to attach a fields to a Product
entity based o
Yes, it is possible, you need to use QueryBuilder
to achieve that:
$result = $em->getRepository('AppBundle:Product')
->createQueryBuilder('p')
->select('p, count(r.id) as countResult')
->leftJoin('p.Review', 'r')
->groupBy('r.id')
->getQuery()
->getArrayResult();
and now you can do something like:
foreach ($result as $row) {
echo $row['countResult'];
echo $row['anyOtherProductField'];
}