聚合函数不适用于QB语句。

时间:2015-10-26 00:24:33

标签: mysql symfony dql query-builder

我只是想知道(id = 1)的学生通过了多少测试。 在我的数据库中,id = 1的学生已通过一次测试。

DB和下面链接中的运行结果:

Capture

  

TestRepository中的GetAvg()方法做了学生1已经通过的测试计数。

NoteMatiereController:

     public  function showMarksByStudentAction($idMat)

{    $iduser=$this->getUser()->getId();

         $em = $this->getDoctrine()->getManager(); 
    $notes = $em->getRepository("AcmeMyBundle:NoteMatiere")
            ->showMarksByStudent($iduser, $idMat);

      $moys = $em->getRepository("AcmeMyBundle:Test")
            ->GetAVG();

     print_r ($moys);
 return($this->render("AcmeMyBundle:NoteMatiere:listNotes.html.twig", array("notes" => $notes,"moys"=>$moys)));


} 

NoteMatiereRepository:

     public function showMarksByStudent($student_id,$mat_id)
{ 
    $query=$this->getEntityManager()
        ->createQuery("SELECT n from AcmeMyBundle:Etudiant e ,AcmeMyBundle:NoteMatiere n,AcmeMyBundle:Test t,AcmeMyBundle:MatiereProf m WHERE n.idEt=?1 and t.idTest = n.idTest and t.idMatProf = m.id and m.idMat=?2")
        ->setParameter(1, $student_id)
        ->setParameter(2, $mat_id);
    return $query->getResult();   

}

TestRepository:

  public function GetAVG()
{         
  $qb = $this->createQueryBuilder('m');
    $qb->select('n.idEt,t.idTest,t.coefTest,count(t.coefTest) as ts')
            ->from('AcmeMyBundle:Test', 't')
            ->from('AcmeMyBundle:NoteMatiere', 'n')
            ->where('n.idEt=1 and t.idTest = n.idTest');
        // -> and where(t.idTest = n.idTest) ; is the same   
    return $qb->getQuery()->getResult();


}

不添加条件t.idTest = n.idTest计数结果为16!

ListNote.html.twig:它将帮助您了解视图的工作原理。



<table border=1>
  <tr>
    <!--   <th>Description</th>
        <th>Note</th>
         !-->
    <th>Liste des Tests</th>
    <th>Note</th>
  </tr>
  {% for note in notes %}
  <tr>
    <td>{{ note.idTest }}</td>
    <td>{{ note.note }}</td>


  </tr>
  {%endfor%}

</table>
<table>
  {% for moy in moys %}

  <td>id Test ==>{{ moy.idTest }}</td>
  <td>Coef Test ==>{{ moy.coefTest }}</td>
  <td>Count Coef Test ==>{{ moy.ts }}</td>


  {%endfor%}


</table>
&#13;
&#13;
&#13;

我的陈述是如此之久,但经过长时间的测试,问题就在那里,我真的很累。感谢帮助!

1 个答案:

答案 0 :(得分:0)

试试这个:

public function GetAVG()
{         
  $qb = $this->createQueryBuilder('test');

  $qb->select('nota.idEt,test.idTest,test.coefTest,count(test.coefTest) as ts')
     ->join('test.noteMaterie', 'nota', Join::WITH. 'test.idTest=nota.idTest') //You can skip the 4 param here if its the regular realtionship parameter
     ->where('nota.idEt = 1');  

   return $qb->getQuery()->getResult();
}

请改变关系,因为它们在您的情况下命名!