带有数组的Doctrine where子句

No_*_*ame 1 php sql arraycollection symfony doctrine-orm

我有一个名为"发票"的实体,其中有许多"订阅",因此"发票"和"订阅"处于多对多的关系中.

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
}
Run Code Online (Sandbox Code Playgroud)

使用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();
Run Code Online (Sandbox Code Playgroud)

Ale*_* B. 5

学说MEMBER OF对此目的有特殊声明.另一种解决方案是制作子查询.

->andWhere(':subscription MEMBER OF p.subscriptions') 
Run Code Online (Sandbox Code Playgroud)

您可以在官方学说文档中找到示例.