Doctrine2 在字段数组中按值查找
我想知道是否有办法搜索看起来像这样的文档字段:
i wonder if there is a way to search for a document field looking like :
/**
* @var array
*
* @ORMColumn(name="tags", type="array", nullable=true)
*/
private $tags;
在数据库中看起来像 php 数组解释:
which in database looks like php array interpretation :
a:3:{i:0;s:6:"tagOne";i:1;s:6:"tagTwo";i:2;s:8:"tagThree";}
现在我尝试通过标签搜索实体
now i try to search the entity by a tag tryed
public function findByTag($tag) {
$qb = $this->em->createQueryBuilder();
$qb->select('u')
->from("myBundle:Entity", 'u')
->where('u.tags LIKE :tag')
->setParameter('tag', $tag );
$result=$qb->getQuery()->getResult();
return $result;
}
总是返回 array[0]
只是不明白
我能够更改它们的保存方式任何帮助,在此先感谢
i am able to change the way how they are saved for any help, thanks in advance
推荐答案
你需要为 %
在你想要的值之前和/或之后定义一个 literal
标签搜索;在这种情况下,您甚至不需要在短语前后加上单引号:
You need to define a literal
tag for %
before and/or after the value you want to search; in this case you won't even need to have single quotation before and after your phrase:
$qb = $this->em->createQueryBuilder();
$qb->select('u')
->from("myBundle:Entity", 'u')
->where($qb->expr()->like('u.tags', $qb->expr()->literal("%$tag%")))
$result=$qb->getQuery()->getResult();
return $result;
您可以关注所有 Doctrine expr 类
相关文章