格式:JOIN(SELECT)与学说createQueryBuilder或createQuery

时间:2019-04-13 09:09:06

标签: php symfony

我在phpmyadmin(SQL控制台)中尝试了此查询,它运行得很好,我想使用Symfony主义使用createQueryBuilder(技巧1)或createQuery(技巧2)

SELECT * FROM inscription_etudiant t

            JOIN (  SELECT etudiant_id, max(inscription_etudiant.date_inscription) as MaxDate
                    FROM inscription_etudiant 
                    GROUP BY etudiant_id ) tm
            ON t.etudiant_id = tm.etudiant_id AND t.date_inscription = tm.MaxDate

            JOIN etudiant e ON e.id = t.etudiant_id
            JOIN classe c ON c.id = t.classe_id

我尝试了第二个技巧,但语法错误==>语义错误]行0,在'(SELECT r,max(inscription_etudiant.date_inscription)''附近的第100行:错误:类'('未定义。

我想使用至少一种方法来编写此查询,请提出任何建议

public function indexGridAction()
    {
        $em = $this->getDoctrine()->getManager();
    //Tip 1
    $inscriptionEtudiantRepository = $em->getRepository('AdminBundle:InscriptionEtudiant');
    $query = $inscriptionEtudiantRepository->createQueryBuilder('p') 
        ->select('t')
        ->from('AdminBundle:InscriptionEtudiant', 'inscription')
        //->addSelect() ??
        ->join('inscription.etudiant','etudiant')
        ->join('inscription.classe','classe')
        ->groupBy('inscription.etudiant')
        ->orderBy('inscription.dateInscription', 'DESC')
        ->getQuery();



    // Tip 2
    $query2 = $em->createQuery("
        SELECT t, tm, c, e FROM AdminBundle:InscriptionEtudiant t

        JOIN(SELECT r, max(inscription_etudiant.date_inscription) as MaxDate
                FROM AdminBundle:InscriptionEtudiant r  
                GROUP BY etudiant_id ) tm
        ON t.etudiant_id = tm.etudiant_id AND t.date_inscription = tm.MaxDate 
        JOIN AdminBundle:Etudiant e ON e.id = t.etudiant_id
        JOIN AdminBundle:Classe c ON c.id = t.classe_id
    ");


    $inscriptions = $query2->getResult();



    return $this->render('@Admin/etudiant/index_grid.html.twig', array(
        'inscriptions' => $inscriptions,
    ));
}

0 个答案:

没有答案