Php 带数组的where子句
我有一个名为“发票”的实体,其中有许多“订阅”,因此“发票”和“订阅”处于多对多关系中Php 带数组的where子句,php,sql,symfony,doctrine-orm,arraycollection,Php,Sql,Symfony,Doctrine Orm,Arraycollection,我有一个名为“发票”的实体,其中有许多“订阅”,因此“发票”和“订阅”处于多对多关系中 class Invoice { /** * @ORM\Column(type="integer") */ protected $a; /** * @ORM\Column(type="integer") */ protected $b; /** * @ORM\ManyToMany(targetEntity="Subscription", inversedBy="invoices") */ pro
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,如何使用子描述上的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();
该原则为此目的有特别声明
成员。另一个解决方案是进行子查询
->andWhere(':subscription MEMBER OF p.subscriptions')
您可以在。条令中找到关于这一目标的特别声明成员。另一个解决方案是进行子查询
->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而不是对象。我不完全确定,但也许您应该加入订阅
$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而不是对象