Sql 如何执行此查询
我需要得到所有小学生的家长。 我的ER模型是: 表名是西班牙语的。翻译如下:Sql 如何执行此查询,sql,symfony,doctrine-orm,Sql,Symfony,Doctrine Orm,我需要得到所有小学生的家长。 我的ER模型是: 表名是西班牙语的。翻译如下: 负责==家长 校友=学生 tutelado==是家长和学生之间的N:N关系 grado==班级或课程(我不知道正确的翻译) nivel==是水平学校(幼儿园、小学(阿根廷小学)、中学、高中等) 我使用QuerBuilder(在负责的存储库中)构造此查询: 这是原始SQL: SELECT r0_.iduser AS iduser0, r0_.iduser AS iduser1 FROM responsable r0
- 负责==家长
- 校友=学生
- tutelado==是家长和学生之间的N:N关系
- grado==班级或课程(我不知道正确的翻译)
- nivel==是水平学校(幼儿园、小学(阿根廷小学)、中学、高中等)
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,并得到空的结果集
有什么想法吗?您可以尝试以下方法:
$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();
我忘了加入“nivel”表:
谢谢,我试着这样做。
$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();
$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);