2011-11-28 33 views
1
$categories= array(3,20,24);  

$qb = $this->em->createQueryBuilder(); 
     $qb->select('p') 
       ->from('\Entities\Productss', 'p') 
       ->leftJoin('p.category', 'c') 
       ->andWhere('p.id =?1') 
       ->andWhere('p.id =?2') 
       ->andWhere('p.id =?2') 
      ->setParameter(1, $categories[0])    
->setParameter(2, $categories[1]) 
->setParameter(3, $categories[2]) 

       ->getQuery(); 

,這並不讓多個何在2查詢...學說,如果產品等於多個類別

$類是由它必須以正確的選擇匹配的產品類別的數組。如鞋子(3),黑色(20),小(24)

可能嗎?

回答

0

教義的文檔,我發現這一點:

// Example - $qb->expr()->in('u.id', array(1, 2, 3)) 
// Make sure that you do NOT use something similar to $qb->expr()->in('value', array('stringvalue')) as this will cause Doctrine to throw an Exception. 
// Instead, use $qb->expr()->in('value', array('?1')) and bind your parameter to ?1 (see section above) 
public function in($x, $y); // Returns Expr\Func instance 

// Example - $qb->expr()->notIn('u.id', '2') 
public function notIn($x, $y); // Returns Expr\Func instance 

應該可以把一個子查詢中此功能。我從來沒有用過它,但試試看。

編輯

我明白這是一個許多一對多的關係。在這種情況下,您應該使用MEMBER OF選項。

所以像:

$qb->... 
    ->andWhere("p.category MEMBER OF ?1") 
    ->andWhere("p.category MEMBER OF ?2") 
    ->... 
+0

$ QB-> EXPR() - >在()。是說,Id在1或2或3。我需要知道它是否在1和2和3 –

相關問題