1
votes

Having the following error in my doctrine query when trying to find a specific tag selected by the user.

[Semantical Error] line 0, col 78 near 'tag WHERE blog.tags': Error: Class Acme\DemoBundle\Entity\Blog has no association named tags

Can someone tell what's wrong with the query? (trying to query for a tag selected in the side bar that brings up all posts related to the tag)

Repository

public function getPostsByTags($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;
}

Controller

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

Sidebar 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>

Tag results twig

{% 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 %}

Updated solution: repository query (no blogs found)

public function getPostsByTags($tags)
{

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

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

Updated solution: controller using query (no blogs found)

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
In blog entity there is no relation defined for tags and in query you are using unmapped joinM Khalid Junaid
So what do I need to do to correct this?esteemed.squire
You need to read the docs carefully to map your entities based on their nature of relation one to many or many to manyM Khalid Junaid

3 Answers

2
votes

Change your getPostsByTags function to:

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

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

return $query->getResult();
1
votes

This is the query that worked. Hope this helps others.

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

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

You can take a look at the answer i give to a similar problem (3rd solution) : 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;
}