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,
);
}
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();