gpt4 book ai didi

php - 检查一个数组是否包含 Doctrine 查询生成器中另一个数组的任何元素

转载 作者:可可西里 更新时间:2023-11-01 01:14:52 24 4
gpt4 key购买 nike

我想知道是否可以在 Doctrine 查询构建器中检查一个数组是否包含另一个数组的任何元素。

在我的例子中,我想获得所有产品(项目),这些产品(项目)至少具有传入参数的数组中的一个类别。

Relationship between Item and 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;

My first try from the Item repository (i know this work if i have only one value to check):

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)

最佳答案

我会通过添加连接来解决它。

public function getListItemsFromCatList($listCat) {

$em = $this->getDoctrine()->getManager();

$qb = $em->createQueryBuilder();
$qb->select('i')
->from('AppBundle:Item', 'i')
->innerJoin('i.categories','cat')
->where('cat IN (:listCat)')
->setParameter('listCat', $listCat);

return = $qb->getQuery()->getResult();
}

请注意,此方法将过滤项目内的类别。这意味着当您尝试从给定项目中获取类别时 i , $i->getCategories() , 它只会返回 i 中的类别与 $listCat 匹配.

如果您需要使用每个项目的所有类别,即使那些与 $listCat 不匹配的类别.我会建议您使用子查询进行过滤,并使用主查询返回完整项目。如果您需要任何进一步的帮助,请发表评论。

关于php - 检查一个数组是否包含 Doctrine 查询生成器中另一个数组的任何元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41685305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com