我有两个实体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();
}
}
它似乎有效,但我没有太多数据可以测试它。我担心最后->andWhere()
声明,看看货物是否没有被取消。如果我使用&#39;和&#39;,我担心它只会退回已发货且未被取消的发货订单&#34;而不是&#34;尚未发送或发送,未被取消&#34;。如果我将->andWhere()
更改为->orWhere()
,我认为它会返回已发送但未被取消的发货订单&#34;。
我主要关心的是查询函数的顺序如何影响查询。另外,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;
}
答案 0 :(得分:0)
是的,其中只能使用一次,所以如果和(或或者)是有条件的,但可以多次使用,你应该使用,其中1 = 1