[英]Does function order matter in Doctrine2 Query Builder?
我有兩個實體Order
和Shipment
。 每個訂單都有一個貨件,反之亦然。 我正在嘗試查詢待處理的訂單。 以下是標准:
這是我提出的查詢:
<?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.