获取Doctrine / symfony中的平均值

时间:2013-09-07 00:19:34

标签: symfony doctrine

我试图在学说中获得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;
}

但我没有找到任何有价值的平均值,我看到我的所有对象...我尝试在我的控制器中使用createQuery但我有很多错误,如:错误:无效的PathExpression。必须是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 = ?

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

您忘记添加select语句进行查询。也可以添加group by

->addSelect('AVG(v.punteggio) as punteggio_avg')