Symfony - 使用 Doctrine 创建复杂的查询

Symfony - Creating complex queries using 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)

使用 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 以及一些括号的查询? 谢谢!

你在寻找类似的东西吗?

  $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();