Symfony2 - 查询标记时需要有关以下错误的帮助

时间:2014-04-20 10:10:24

标签: symfony doctrine-orm tags tag-cloud

在尝试查找用户选择的特定标记时,在我的学说查询中出现以下错误。

[语义错误]第0行,第78行附近标记WHERE blog.tags':错误:类Acme \ DemoBundle \ Entity \ Blog没有关联命名标记

有人能说出查询有什么问题吗? (尝试查询在侧栏中选择的标签,该标签会显示与标签相关的所有帖子)

存储库

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

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

}

博客实体

/**
 * @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;
}

控制器

/**
 * @Route("/tag/{tag}", name="AcmeDemoBundle_tag")
 * @Template("AcmeDemoBundle:Page:tag.html.twig")
 */
public function tagAction($tag = null)
{
    $em = $this->getDoctrine()->getManager();

    $tags = $em->getRepository('AcmeDemoBundle:Blog')
        ->getPostsByTags($tag);

    if (!$tags) {
        throw $this->createNotFoundException('Unable to find blog posts');
    }

    return array(
        'tags'  => $tags,
    );
}

补充工具栏Twig

<p class="tags">
    {% for tag, weight in tags %}
    <span class="weight-{{ weight }}"><a href="{{ path('AcmeDemoBundle_tag', { 'tag': tag }) }}">{{ tag }}</a></span>
    {% else %}
<p>There are no tags</p>
{% endfor %}
</p>

标记结果小枝

{% block body %}
{% for tag in tags %}
    <article class="result">
        <div class="date"><time datetime="{{ tag.created|date('c') }}">{{ tag.created|date('l, F j, Y') }}</time></div>
        <header>
            <h2><a href="{{ path('AcmeDemoBundle_show', { 'id': tag.id, 'slug': tag.slug }) }}">{{ tag.title }}</a></h2>
        </header>

        <img src="{{ asset(['images/', tag.image]|join) }}" />
        <div class="snippet">
            <p>{{ tag.blog|truncate(250, true) }}</p>
            <p class="continue"><a href="{{ path('AcmeDemoBundle_show', { 'id': tag.id, 'slug': tag.slug }) }}">More...</a></p>
        </div>

        <footer class="meta">
            <p>Comments: -</p>
            <p>Posted by <span class="highlight">{{tag.author}}</span> at {{ tag.created|date('h:iA') }}</p>
            <p>Tags: <span class="highlight">{{ tag.tags }}</span></p>
        </footer>
    </article>
{% else %}
    <p>There are no blog entries for Health&Fitness blog</p>
{% endfor %}
{% endblock %}

更新的解决方案:存储库查询(未找到博客)

public function getPostsByTags($tags)
{

    $query = $this->createQueryBuilder('b')
        ->where('b.tags = :tags')
        ->setParameter('tags', $tags);

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

更新的解决方案:使用查询的控制器(未找到博客)

public function tagAction(tags=null)
{
    $em = $this->getDoctrine()->getManager();

    $repository = $em->getRepository('AcmeDemoBundle:Blog');

    $tags = $repository->createQueryBuilder('b')
        ->where('b.tags = :tags')
        ->setParameter('tags', $tags)
        ->getQuery()
        ->getResult();

    return array(
        'tags' => $tags,
    );
}

3 个答案:

答案 0 :(得分:2)

getPostsByTags功能更改为:

$repository = $this->getDoctrine()
    ->getRepository('AcmeDemoBundle:Blog');

$query = $repository->createQueryBuilder('b')
->where('b.tags = :tags')
->setParameter('tags', $tags)
->getQuery();

return $query->getResult();

答案 1 :(得分:1)

这是有效的查询。希望这有助于其他人。

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

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

答案 2 :(得分:0)

你可以看看我给出类似问题的答案(第3个解决方案):Symfony2 - Need help setting up a doctrine query for finding tags

public function getBlogsWithTag($tagRequested)
{
$blogs = $this->findAll();

$blogsWithTag = array();
$tags = array();
foreach ($blogs as $blog)
{
    $tags = explode(",", $blog->getTags());
    foreach ($tags as &$tag)
    {
        $tag = trim($tag);
    }

    if(in_array($tagRequested, $tags)) {
        array_push($blogsWithTag, $blog);
    }
}

return $blogsWithTag;
}