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();

这不允许多个wheres ...

$categories 是一个数组,其中包含必须匹配的类别才能选择正确的产品。如鞋(3),黑色(20),小(24)

可能的?

4

1 回答 1

0

In the documentation of Doctrine I found this:

// 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

It should be possible to put a subquery in this function. I never used it myself, but give it a try.

EDIT

I understand it is a many-to-many relation. In this case you should use the MEMBER OF option.

So like:

$qb->...
   ->andWhere("p.category MEMBER OF ?1")
   ->andWhere("p.category MEMBER OF ?2")
   ->...
于 2011-11-28T22:54:38.550 回答