我有一个名为"发票"的实体,其中包含许多"订阅",因此"发票"和"订阅"处于多对多关系中。
class Invoice
{
/**
* @ORMColumn(type="integer")
*/
protected $a;
/**
* @ORMColumn(type="integer")
*/
protected $b;
/**
* @ORMManyToMany(targetEntity="Subscription", inversedBy="invoices")
*/
protected $subscriptions;
public function __construct()
{
$this->subscriptions = new ArrayCollection();
}
//typical setters and getters
}
使用查询生成器,如何使用订阅上的 where 子句获取一组匹配的实体?
$subscription = ;//something pulled from the db and is a "subscription" object
$em->createQueryBuilder()
->select('p')
->from('Invoice', 'p')
->where('p.a = :a')
->andWhere('p.b = :b')
->andWhere('p.subscriptions has :subscription') //this line here I need help
->setParameter('a', 1)
->setParameter('b', 2)
->setParameter('subscription', $subscription)
->getQuery()
->getResult();
教义对此目标有特别的陈述MEMBER OF
。另一种解决方案是进行子查询。
->andWhere(':subscription MEMBER OF p.subscriptions')
您可以在官方教义文档中找到示例。
我不
完全确定,但也许你应该加入订阅。
$subscription = ;//something pulled from the db and is a "subscription" object
$em->createQueryBuilder()
->select('p')
->from('Invoice', 'p')
->join('p.subscriptions', 'subscription');
->where('p.a = :a')
->andWhere('p.b = :b')
->andWhere('subscription = :subscription') //this line here I need help
->setParameter('a', 1)
->setParameter('b', 2)
->setParameter('subscription', $subscription)
->getQuery()
->getResult();
或者,如果订阅具有 ID,则可以使用该 ID 而不是对象。