2014-03-05 4 views
1

"invoice"와 "subscription"이 many to many 관계에 있도록 "subscription"이 많은 "invoice"라는 엔티티가 있습니다. querybuilder를 사용Doctrine 배열이있는 where 절

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 
} 

, 어떻게 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(); 

답변

5

Doctrine에는이 목적을 위해 특별한 진술 MEMBER OF이 있습니다. 다른 해결책은 하위 쿼리를 만드는 것입니다.

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

예를 들면 당신은 official doctrine documentation에서 찾을 수 있습니다.

0

전적으로 확실하지 않지만 구독에 가입해야합니다. 가입 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(); 

또는 대신 객체의를 사용할 수 있습니다.