我需要让所有在小学阶段就读的学生的所有家长。 我的ER模型是:
表名是西班牙语。翻译是:
我使用QuerBuilder构建此查询(在Responsable存储库中):
$qb = $this->createQueryBuilder('r');
$qb->innerJoin('r.tutelados', 't')
->innerJoin('t.alumno', 'a')
->innerJoin('a.grado', 'g')
->where('g.nivel = :Nivel')
->setParameter('Nivel', $unNivel);
这是原始SQL:
SELECT r0_.iduser AS iduser0, r0_.iduser AS iduser1
FROM responsable r0_
INNER JOIN tutelado t1_ ON r0_.iduser = t1_.responsable
INNER JOIN alumno a2_ ON t1_.alumno = a2_.iduser
INNER JOIN grado g3_ ON a2_.grado = g3_.idgrado
WHERE g3_.nivel = 'Primario'
我在PhpMyAdmin中执行SQL并获得空结果集。
任何想法?。
答案 0 :(得分:0)
您可以尝试以下方式:
$this->getDoctrine()->getManager()->getRepository('YourBundle:Responsable')
->createQueryBuilder('r')
->join('r.alumnos', 'a') // Check the name of the relation between alumno and responsable
->join('a.grado', 'g')
->join('g.nivel', 'n')
->where('n.nivel = ?1')
->setParameter(1, 'elementary')
->getQuery()
->getResult();
答案 1 :(得分:0)
我忘了加入“nivel”表:
$qb = $this->createQueryBuilder('r');
$qb->innerJoin('r.tutelados', 't')
->innerJoin('t.alumno', 'a')
->innerJoin('a.grado', 'g')
->innerJoin('g.nivel', 'n')
->where('n.nivel = :Nivel')
->setParameter('Nivel', $unNivel);