Is it possible to use result of an SQL function as a field in Doctrine?

后端 未结 5 1587
失恋的感觉
失恋的感觉 2021-01-01 22:38

Assume I have Product entities and Review entities attached to products. Is it possible to attach a fields to a Product entity based o

5条回答
  •  温柔的废话
    2021-01-01 23:25

    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'];
    }
    

提交回复
热议问题