我有一个名为“发票”的实体,其中包含许多“订阅”,以便“发票”和“订阅”处于多对多的关系中。
class Invoice
{
/**
* @ORM\Column(type="integer")
*/
protected $a;
/**
* @ORM\Column(type="integer")
*/
protected $b;
/**
* @ORM\ManyToMany(targetEntity="Subscription", inversedBy="invoices")
*/
protected $subscriptions;
public function __construct()
{
$this->subscriptions = new ArrayCollection();
}
//typical setters and getters
}
使用querybuilder,如何使用subcription上的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();
答案 0 :(得分:5)
为此目的,学说有MEMBER OF
特别声明。另一种解决方案是制作子查询。
->andWhere(':subscription MEMBER OF p.subscriptions')
您可以在official doctrine documentation找到示例。
答案 1 :(得分:0)
我不完全确定,但也许你应该加入订阅。
$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而不是对象。