我想知道在Doctrine查询构建器中是否可以检查一个数组是否包含另一个数组的任何元素。
在我的情况下,我想获取所有至少有一个类别与传递的参数数组匹配的产品(商品)。
Item和Category之间的关系如下:
在我的情况下,我想获取所有至少有一个类别与传递的参数数组匹配的产品(商品)。
Item和Category之间的关系如下:
/**
* @ORM\ManyToMany(targetEntity="Category")
* @ORM\JoinTable(name="items_categories",
* joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false)}
* )
*/
private $categories;
我第一次尝试从项目库中获取数据(当我只需要检查一个值时,我知道这个方法是有效的):
public function getListItemsFromCatList($listCat) {
$qb = $this->createQueryBuilder('i');
$qb->select('i')
->where($qb->expr()->like('i.categories', ':listCat'))
->setParameter('listCat', '%"' . $listCat . '"%');
return $qb->getQuery()->getResult();
}
$listCat是一个类别实体数组:
array (size=5)
0 =>
object(ItemBundle\Entity\Category)[518]
private 'id' => int 22
private 'children' =>
object(Doctrine\ORM\PersistentCollection)[520]
private 'snapshot' =>
array (size=2)
...
private 'owner' =>
&object(ItemBundle\Entity\Category)[518]
private 'association' =>
array (size=15)
...
private 'em' =>
object(Doctrine\ORM\EntityManager)[796]
...
private 'backRefFieldName' => string 'parent' (length=6)
private 'typeClass' =>
object(Doctrine\ORM\Mapping\ClassMetadata)[579]
...
private 'isDirty' => boolean false
protected 'collection' =>
object(Doctrine\Common\Collections\ArrayCollection)[515]
...
protected 'initialized' => boolean true
private 'parent' => null
private 'name' => string 'Luxe' (length=4)
1 =>
object(ItemBundle\Entity\Category)[504]
private 'id' => int 25
private 'children' =>
object(Doctrine\ORM\PersistentCollection)[505]
private 'snapshot' =>
array (size=0)
...
private 'owner' =>
&object(ItemBundle\Entity\Category)[504]
private 'association' =>
array (size=15)
...
private 'em' =>
object(Doctrine\ORM\EntityManager)[796]
...
private 'backRefFieldName' => string 'parent' (length=6)
private 'typeClass' =>
object(Doctrine\ORM\Mapping\ClassMetadata)[579]
...
private 'isDirty' => boolean false
protected 'collection' =>
object(Doctrine\Common\Collections\ArrayCollection)[500]
...
protected 'initialized' => boolean false
private 'parent' =>
object(ItemBundle\Entity\Category)[512]
private 'id' => int 23
private 'children' =>
object(Doctrine\ORM\PersistentCollection)[513]
...
private 'parent' =>
object(ItemBundle\Entity\Category)[518]
...
private 'name' => string 'Bijoux' (length=6)
private 'name' => string 'Bagues' (length=6)