簡體   English   中英

函數順序在Doctrine2 Query Builder中是否重要?

[英]Does function order matter in Doctrine2 Query Builder?

我有兩個實體OrderShipment 每個訂單都有一個貨件,反之亦然。 我正在嘗試查詢待處理的訂單。 以下是標准:

  • 訂單已發送(而不僅僅是已保存)
  • 訂單尚未取消
  • 訂單沒有發貨
  • 或者訂單有貨
    • 但是發貨尚未發送(剛剛保存)
    • 如果已發送貨件,則也已取消

這是我提出的查詢:

<?php

use Doctrine\ORM\EntityRepository;

class OrderRepository extends EntityRepository
{

    public function findPending($id)
    {
        return $this->createQueryBuilder('o')
            ->addSelect('s')
            ->leftJoin('MyApp\\Model\\Entity\\Shipment', 's')
            ->orderBy('o.date_sent', 'DESC')

            // Order has been sent
            ->where($qb->expr()->neq('o.date_sent',':date_sent'))
            ->setParameter('date_sent', '0000-00-00 00:00:00')

            // Order was not cancelled
            ->where($qb->expr()->eq('o.date_cancelled',':date_cancelled'))
            ->setParameter('date_cancelled', '0000-00-00 00:00:00')

            // Order does not have a shipment
            ->andWhere($qb->expr()->isNull('s.order'))

            // OR Shipment has not been sent
            ->orWhere($qb->expr()->neq('s.date_sent', ':ship_date_sent'))
            ->setParameter('ship_date_sent', '0000-00-00 00:00:00')

            // AND Shipment has not been cancelled
            ->andWhere($qb->expr()->eq('s.date_cancelled',':ship_date_cancelled'))
            ->setParameter('ship_date_cancelled', '0000-00-00 00:00:00')

            ->setMaxResults(6)
            ->getQuery()
            ->getResult();
    }
}

它似乎工作,但我沒有太多的數據來測試它。 我擔心last ->andWhere()語句,看看Shipment是否還沒有被取消。 如果我使用'和',我擔心它只會返回“尚未發送且未被取消”的發貨訂單,而不是“尚未發送或未發送,未被取消”。 如果我改變->andWhere()->orWhere()我認為它會返回訂單與“已發送,但不取消”的出貨量。

我主要關心的是查詢函數的順序如何影響查詢。 另外, where()只能使用一次嗎? 我沒有看到where()andWhere()之間的區別?

如果我的問題不夠明確,請告訴我,我會更新。

先感謝您。

更新

我已經深入挖掘並提出了這個新查詢。 我認為哪個會起作用?

public function findPending($id)
{
    $qb = $this->createQueryBuilder('o')
        ->addSelect('s')
        ->leftJoin('MyApp\\Model\\Entity\\Shipment', 's')
        ->orderBy('o.date_sent', 'DESC')

        // Order has been sent and was not cancelled
        ->where($qb->expr()->andX(
            $qb->expr()->eq('o.date_cancelled','0000-00-00 00:00:00'),
            $qb->expr()->neq('o.date_sent','0000-00-00 00:00:00')
            ))

        ->andWhere($qb->expr()->orX(
            // Order doesn't have a shipment
            $qb->expr()->isNull('s.order'),
            // OR Order has a shipment
            $qb->expr()->orX(
                // Shipment has not been sent
                $qb->expr()->eq('s.date_sent','0000-00-00 00:00:00'),
                // OR Shipment has been sent AND it was cancelled
                $qb->expr()->andX(
                    $qb->expr()->neq('s.date_sent','0000-00-00 00:00:00'),
                    $qb->expr()->eq('s.date_cancelled','0000-00-00 00:00:00')
                    )
                )
            ))

        ->setMaxResults(6)
        ->getQuery()
        ->getResult();
    return $qb;
}

是的, 哪里只能使用一次,所以如果andWhere (或orhere )是有條件的但可以多次使用,你應該使用1 = 1的地方

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM