查询的逻辑 - 逻辑顺序 - 其中(condition1和[condition2或condition3])

时间:2017-07-12 15:49:47

标签: php doctrine-orm doctrine doctrine-query shopware

我正在写一个Shopware插件。 Shopware使用Doctrine for Database操作。

我想写一个这样的学说:

Where(condition1 and [condition2 or condition3])

在代码中查看我的评论。

$builder = Shopware()->Models()->createQueryBuilder();
$builder->select([
        'orders',
        'details',
        'customer',
        'shipping',
        'billing'
]);
$builder->from('Shopware\Models\Order\Order', 'orders');
$builder->leftJoin('orders.details', 'details')
        ->leftJoin('orders.customer', 'customer')
        ->leftJoin('orders.shipping', 'shipping')
        ->leftJoin('orders.billing', 'billing');


//--> Here I would like to write an or statement.
// So that i get all the orders by
// where(customers.email = email) and [(shipping.zipCode = zip) or (billing.zipcode = zip)]
$builder->where('customer.email = :email');
$builder->andWhere('(shipping.zipCode = :zipCode) orWhere (billing.zipCode = :zipCode)');

$builder->setParameter('email', $email);
$builder->setParameter('zipCode', $zip);
$builder->orderBy('orders.id', 'DESC');

$order = $builder->getQuery()->getResult($this->getResultMode());

1 个答案:

答案 0 :(得分:2)

使用:

$builder->where('customer.email = :email');
$builder->andWhere('(shipping.zipCode = :zipCode OR billing.zipCode = :zipCode)')

除了单一条件,where采用任何形式的条件