在Symfony中使用Doctrine创建更复杂查询的最佳方法是什么?例如,我需要具有3个参数,两个AND和一个嵌套OR条件的查询。在mySQL中,它看起来像这样:
SELECT *
FROM schedule sched
WHERE shed.date = '01-01-2018' --first parameter
AND (sched.timefrom BETWEEN 8 AND 12 -- second and third parameter
OR sched.timeto BETWEEN 8 AND 12) -- also second and third
我试过很多方法,但没有任何作用:
$query = $em->createQuery(
'SELECT sched
FROM schedule sched
WHERE shed.date = :d
AND (sched.timefrom BETWEEN :tf AND :tt
OR sched.timeto BETWEEN :tf AND :tt)'
) ->setParameters(['d' => $date->format('Y-m-d'), 'tf' => $timefrom, 'tt' => $timeto]);
即使我用完整的条件替换BETWEEN,这也不起作用,如下所示:
(sched.timefrom > :tf AND sched.timefrom < :tt)
使用查询生成器也没有工作,我尝试了几种方式,这是最后一个(也尝试使用表达式构建器,也不起作用):
$repository = $this->getDoctrine()->getRepository(schedule::Class);
$query = $repository->createQueryBuilder('sched');
$query->where('sched.date = :date AND ((sched.timefrom > :timefrom AND sched.timefrom < :timeto) OR (shced.timeto > :timefrom AND sched.timeto < :timeto))')
->setParameter('date', $date->format('Y-m-d'))
->setParameter('tf', $timefrom)
->setParameter('tt', $timeto);
$query = $query->getQuery();
也:
$repository = $this->getDoctrine()->getRepository(schedule::Class);
$query = $repository->createQueryBuilder('sched');
$query->where('sched.date = :date')
->andWhere('sched.timefrom > :timefrom AND sched.timefrom < :timeto')
->orwhere('sched.timeto > :timefrom AND sched.timeto < :timeto')
->setParameter('date', $date->format('Y-m-d'))
->setParameter('tf', $timefrom)
->setParameter('tt', $timeto);
$query = $query->getQuery();
如何创建具有多个OR和AND以及一些括号的查询?谢谢!
你找那样的东西吗?
$repository = $this->getDoctrine()->getRepository(schedule::Class);
$q = $repository->createQueryBuilder('sched');
$expr = $q->expr();
$exprQuery =
$expr->andX(
$expr->eq('sched.date',$date-format('Y-m-d')),
$expr->orX(
$expr->between('sched.timefrom',$timefrom,$timeto),
$expr->between('sched.timeto',$timefrom,$timeto)
)
);
$q->where($exprQuery);
$q = $q->getQuery();
第一个版本的问题是查询
(sched.timefrom BETWEEN :tf AND :tt
OR sched.timeto BETWEEN :tf AND :tt)
应该
((sched.timefrom BETWEEN :tf AND :tt)
OR (sched.timeto BETWEEN :tf AND :tt))
第二个和第三个版本的主要问题是设置参数,因为在查询中你使用:timefrom
和:timeto
但设置tf
和tt
。我更喜欢使用BETWEEN
。那是
$query->where('sched.date = :date AND ((sched.timefrom > :timefrom AND sched.timefrom < :timeto)
OR (shced.timeto > :timefrom AND sched.timeto < :timeto))')
->setParameter('date', $date->format('Y-m-d'))
->setParameter('tf', $timefrom)
->setParameter('tt', $timeto);
应该
$query->where('sched.date = :date AND ((sched.timefrom BETWEEN :timefrom AND :timeto)
OR (shced.timeto BETWEEN :timefrom AND :timeto))')
->setParameter('date', $date->format('Y-m-d'))
->setParameter('timefrom', $timefrom)
->setParameter('timeto', $timeto);
试试这个。我希望它有所帮助!
$repository = $this->getDoctrine()->getRepository(schedule::Class);
$query = $repository->createQueryBuilder('sched')
->where('sched.date = :date')
->andWhere('(sched.timefrom BETWEEN :timefrom AND :timeto)
OR (sched.timeto BETWEEN :timefrom AND :timeto)')
->setParameter('date', $date->format('Y-m-d'))
->setParameter('timefrom', $timefrom)
->setParameter('timeto', $timeto);
$query = $query->getQuery();