Doctrine2 find by value in field array(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 类
这篇关于Doctrine2 在字段数组中按值查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine2 在字段数组中按值查找


- 正确分离 PHP 中的逻辑/样式 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- Laravel 仓库 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01