仅当我在参数标签中选择某些内容时,下面的查询才有效,否则返回空结果。如果参数标签为空时没有 where 限制,我该怎么做才能让它工作。在下面的情况下,f.tags 是实体 Student 和实体标签之间的多对多关系字段。
public function FindStudent($tags)
{
$qb = $this
->createQueryBuilder('s');
if ($tags) {
$qb->andWhere(':tag MEMBER OF s.tags')
->setParameter('tag', $tags);
}
$qb->Select('s');
return $qb
->getQuery()
->getResult();
}
在分析器中,Doctrine 查询在没有选择任何内容时有一个空的 IN。我想要做的是当参数标签为空时根本没有 INNER JOIN 和 WHERE IN 。
AND EXISTS (SELECT 1 FROM student_tag f3_ INNER JOIN tag s4_ ON f3_.tag_id = s4_.id WHERE f3_.student_id = f0_.id AND s4_.id IN ())
大概
if(!empty($tags)){
会做的伎俩。
从您的评论中编辑:在这种情况下:
use Doctrine\Common\Collections\ArrayCollection;
....
if(!empty($tags) && ($tags instanceof ArrayCollection && !$tags->empty())){
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句