Doctrine2 在字段数组中按值查找 [英] Doctrine2 find by value in field array

查看:15
本文介绍了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 在字段数组中按值查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆