在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)
使用QueryBuilder也不起作用,我尝试了几种方法,这是最后一种方法(也使用expressionBuilder尝试过,也没有用):
$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以及一些方括号的查询? 谢谢!
答案 0 :(得分:1)
您是否正在寻找类似的东西?
$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();
答案 1 :(得分:1)
第一个版本的问题是查询
(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();