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
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
