Doctrine2通过字段数组中的值查找 [英] Doctrine2 find by value in field array

查看:44
本文介绍了Doctrine2通过字段数组中的值查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以搜索如下所示的文档字段:

i wonder if there is a way to search for a document field looking like :

/**
 * @var array
 *
 * @ORM\Column(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]
只是不明白

which always returns array[0] just dont get it

我可以更改保存方式
的保存方式,谢谢,

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天全站免登陆