2011-11-28 71 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 –

相关问题