2013-09-07 4 views
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; 
} 

을하지만 난 값의 평균을 찾을 수없는, 내가 내 모든 객체를 참조하십시오 .. .i 내 컨트롤러에서 createQuery를 사용하려고하지만 다음과 같은 많은 오류가 있습니다. Error : Invalid PathExpression. StateFieldPathExpression이어야합니다. 외래 키가 $의 사진라는 이름이 뭔가를 할 수있는 개체는 컨트롤러에서 ProfVotoFoto 이다의 변수 이름을 가정

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 = ? 

답변

0

:

MySQL의 쿼리입니다 (이것은 외래 키)입니다.

$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(); 
+0

은 내가 으로 시도 $ 쿼리 = $ 보하기> createQuery (' SELECT v.id, SNFotoBundle FROM v.profilo : Voti의 V WHERE v.foto = idFoto ') - > setParameter ('idFoto', intval ($ id_immagine)); profilo는 ManyToOne/@ORM \ JoinColumn (name = "profilo_id", referencedColumnName = "id")입니다. – Barno

0

쿼리에 select 문을 추가하는 것을 잊지 마십시오. 가능하면 group by을 추가해야합니다.

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

시도해보십시오 -> addSelect ('AVG (v.punteggio) as punteggio_avg') 및 $ q-> groupBy 'v.foto'); 하지만 난 var dump에서 내 punteggio_avg를 찾지 못한다. – Barno