Symfony - 使用Doctrine创建复杂查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Symfony - 使用Doctrine创建复杂查询相关的知识,希望对你有一定的参考价值。

在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但设置tftt。我更喜欢使用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();

以上是关于Symfony - 使用Doctrine创建复杂查询的主要内容,如果未能解决你的问题,请参考以下文章

APC 没有为 Symfony.4/Doctrine 应用程序带来任何性能提升?

Symfony2 & Doctrine:创建自定义 SQL 查询

使用 Doctrine orm symfony 创建新数据库时出错

Symfony 2 Doctrine 内存使用情况

Symfony 2 Doctrine 导出到 JSON

在 Symfony 中使用 Doctrine 的 DBAL 检索布尔值