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

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

问题描述

我一直在寻找的范围很广,仍然无法找到一个如何设置查询的示例,以查找用户从侧边栏中选择的特定标签



我了解如何查找所有标签,但无法找到用户选择的具体内容。



blogrepository

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

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

博客实体

  / ** 
* @var string
*
* @ ORM\Column(name =tags type =text)
* /
private $ tags;

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

return $ this;
}

/ **
*获取标签
*
* @return string
* /
public function getTags )
{
return $ this-> tags;
}


解决方案

我相信这将有效你。

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

返回$ query-> getQuery() - > getResult();
}


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.

blogrepository

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

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

blog entity

/**
 * @var string
 *
 * @ORM\Column(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;
}

解决方案

I believe this will work for you.

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

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

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

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