php-检查一个数组是否在学说查询生成器中包含另一个数组的任何元素
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-检查一个数组是否在学说查询生成器中包含另一个数组的任何元素,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3136字,纯文字阅读大概需要5分钟。
内容图文
![php-检查一个数组是否在学说查询生成器中包含另一个数组的任何元素](/upload/InfoBanner/zyjiaocheng/667/00fc83d4beef4638ac3e1d23d9b42b28.jpg)
我想知道是否可以在学说查询构建器中检查一个数组是否包含另一个数组的任何元素.
就我而言,我想获得所有至少具有传递给参数的数组中类别之一的产品(项目).
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是Category Entity的数组:
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-检查一个数组是否在学说查询生成器中包含另一个数组的任何元素全部内容,希望文章能够帮你解决php-检查一个数组是否在学说查询生成器中包含另一个数组的任何元素所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。