0

我尝试在学说中获得 AVG 值,在我的 Repositoru 中我这样做:

public function getFotoSpecifica($idFoto) {
    $q = $this->createQueryBuilder('f');
    $q->leftJoin("f.foto_votata", 'v');
    $q->where('f.id =:idFoto');
    $q->andWhere('(f.foto_privata IS NULL OR f.foto_privata != 1)');
    $q->andWhere('(f.foto_eliminata IS NULL OR f.foto_eliminata != 1)');
    $q->expr()->avg('v.punteggio',  true);
    $q->setParameter('idFoto', $idFoto);
    $dql = $q->getQuery();
    $results = $dql->getArrayResult();
    return $results;
}

但我没有找到任何值 avg,我看到了我所有的对象...我尝试在我的控制器中使用 createQuery 但我有很多错误,例如:错误:无效的路径表达式。必须是 StateFieldPathExpression。(这是外键)

mySql 查询是:

SELECT profilo_id, proprietario_id, round(AVG( punteggio ),2) as avg_rate, SUM(punteggio) as score, foto_id, count(*) as numero_votanti

 FROM prof_voto_foto
 WHERE foto_id = ?
4

2 回答 2

0

假设外键的变量名是 $foto 并且实体是 ProfVotoFoto 在你的控制器中你可以做这样的事情。

   $em = $this->getDoctrine()->getManager();
   $q=$em->createQuery("
          SELECT profilo_id, proprietario_id, round(AVG( punteggio ),2) as avg_rate,
          SUM(punteggio) as score, foto_id, count(*) as numero_votanti
          FROM YourBundleName:ProfVotoFoto f
          WHERE f.foto = :idFoto
   ")
     ->setPArameter('idFoto',$idFoto);

    $averageResult = $q->getSingleResult();
于 2013-09-07T02:58:36.197 回答
0

您忘记添加选择语句进行查询。也可能需要添加group by.

->addSelect('AVG(v.punteggio) as punteggio_avg')
于 2013-09-07T04:28:24.427 回答