简体   繁体   English

教义带数组的where子句

[英]Doctrine where clause with an array

I have an entity called "invoice" with many "subscription" such that "invoice" and "subscription" are in a many to many relationship. 我有一个名为“发票”的实体,其中包含许多“订阅”,因此“发票”和“订阅”之间存在多对多关系。

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
}

Using the querybuilder, how do I get a set of matching entities using the where clause on subcription? 使用querybuilder,如何使用子预订的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();

Doctrine have special statement MEMBER OF for this aim. 学说有特别声明MEMBER OF为这一目标。 Another solution is to make subquery. 另一个解决方案是进行子查询。

->andWhere(':subscription MEMBER OF p.subscriptions') 

Examples you can find in official doctrine documentation . 您可以在官方学说文档中找到示例。

I'm not entirely sure, but perhaps you should join 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();

Alternatively, if subscription has an id, you can use that instead of the object. 或者,如果预订具有ID,则可以使用该ID代替该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM