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



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

[语义错误]第0行,第78列,靠近"tag WHERE blog.tags":错误:Class 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
 *
 * @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;
}

控制器

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

getPostsByTags函数更改为:

$repository = $this->getDoctrine()
    ->getRepository('AcmeDemoBundle:Blog');
$query = $repository->createQueryBuilder('b')
->where('b.tags = :tags')
->setParameter('tags', $tags)
->getQuery();
return $query->getResult();

这是一个有效的查询。希望这能帮助其他人。

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

您可以看看我对类似问题的回答(第三个解决方案):Symfony2-需要帮助设置用于查找标签的条令查询

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;
}

相关内容

  • 没有找到相关文章

最新更新