4

I want to create a Doctrine Query: (Doctrine 2.3)

SELECT * FROM `car` WHERE `plate` like '%' AND (`datetime` BETWEEN '2013-03-13 22:20:18' AND '2013-03-13 22:20:20') OR (`datetime` BETWEEN '2013-03-13 15:10:18' AND '2013-03-13 15:10:16')

I tried the following but its not working:

$qry = $this->manager()->createQueryBuilder()
                    ->from($this->entity, 'e')
                    ->select('e');
$qry->where('e.plate like :plate');
$qry->setParameter('plate', $plate);
$qry->andWhere(
        qry->expr()->between(
             'e.datetime',
            ':dateFrom',
            ':dateTo'
         )
        )
    ->setParameter('dateFrom', $fromdate)
    ->setParameter('dateTo', $todate);
    $qry->orWhere(
            $qry->expr()->between(
                'e.datetime',
                ':dateFrom',
                ':dateTo'
             )
            )
    ->setParameter('dateFrom1', $fromdate1)
    ->setParameter('dateTo1', $todate1);

OutPut of above query:

SELECT e FROM user e WHERE (e.plate like :plate AND (e.datetime BETWEEN :dateFrom AND :dateTo)) OR (e.datetime BETWEEN :dateFrom AND :dateTo)

I want to check two dates in same column how can I check? Is the syntax correct? Currently It is like this:

(Plate AND (Date)) OR Date)

Case 1

But it should come like the following for good output.

(Plate) AND ((Date) OR (Date))

Case 2

In other case it should come like this:

((Plate) or (Plate)) AND ((Date) OR (Date))

Can some one help me I am not an expert in Doctrine I am a learner!

4

1 回答 1

3

经过许多人的搜索和建议,我终于找到了一些逻辑并理解了Doctrine中的表达。下面我用一个例子给出了所有的表达式。要测试的代码。

$ex1    =   $qry->expr()->like('e.user', "'".$user."'");
$ex2    =   $qry->expr()->orX(
                $qry->expr()->between('e.datetime', "'".$datetimefrom."'", "'".$datetimeto."'")
            );
$ex3    =   $qry->expr()->in('e.country', $country);
$final_expression = $qry->expr()->andX($ex1,$ex2,$ex3); 

您还可以参考以下问题和解决方案,这些问题和解决方案帮助我解决了上述问题。

使用 NAND、NOR、NOT、AND 运算符的REF多条查询

于 2013-06-14T08:24:24.647 回答