Symfony2 - 需要帮助设置用于查找标签的学说查询 [英] Symfony2 - Need help setting up a doctrine query for finding tags

查看:10
本文介绍了Symfony2 - 需要帮助设置用于查找标签的学说查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找,但仍然无法找到一个示例,说明如何设置查询以查找用户从侧边栏中选择的特定标签",这反过来会显示所有带有该标签的帖子.

I've been looking far and wide and still haven't been able to find an example of how to setup a query to look for a specific 'tag' that the user selects from a sidebar which in turn will bring up all posts with that tag.

我了解如何查找所有标签,但不知道如何查找用户选择的特定标签.

I understand how to find all tags, but not to find a specific selected by the user.

博客存储库

public function getTags($tags)
{
    $qb = $this->createQueryBuilder('b');
    $qb->select('b')
        ->join('b.tags', 'tag')
        ->where('b.tags LIKE ?', '%'.$tags.'%');

    return $qb->getQuery()->getResult();
}

博客实体

/**
 * @var string
 *
 * @ORMColumn(name="tags", type="text")
 */
private $tags;

/**
 * Set tags
 *
 * @param string $tags
 * @return Blog
 */
public function setTags($tags)
{
    $this->tags = $tags;

    return $this;
}

/**
 * Get tags
 *
 * @return string
 */
public function getTags()
{
    return $this->tags;
}

推荐答案

我相信这对你有用.

public function getPostsByTags($tag)
    {
        $query = $this->createQueryBuilder('b')
            ->where('b.tags like :tag')
            ->setParameter('tag', '%'.$tag.'%');

        return $query->getQuery()->getResult();
    }

这篇关于Symfony2 - 需要帮助设置用于查找标签的学说查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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