I'm trying to load posts from tags but thus far haven't been able to find a working solution for this.
Not sure if the problem is with the query or how it's being selected from the twig file. Using the repository code below gives me an empty array.
If the problem is the query how do you set it up to where it takes the user input from the sidebar to load the tag that in turn loads the right post?
repository
/**
* @Route("/tag/{tag}", name="AcmeDemoBundle_tag")
* @Template("AcmeDemoBundle:Page:tag.html.twig")
*/
public function getPostsByTags($tag)
{
$query = $this->createQueryBuilder('b')
->where('b.tags = :tag')
->setParameter('tag', $tag);
return $query->getQuery()->getResult();
}
The twig file is loading the tags via tag weights in the controller see below:
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>
controller
/**
* @Template("AcmeDemoBundle:Page:sidebar.html.twig")
*/
public function sidebarAction()
{
$em = $this->getDoctrine()->getManager();
$tags = $em->getRepository('AcmeDemoBundle:Blog')
->getTags();
$tagWeights = $em->getRepository('AcmeDemoBundle:Blog')
->getTagWeights($tags);
return array(
'tags' => $tagWeights,
);
}
/**
* @Route("/tag/{tag}", name="AcmeDemoFitnessBundle_tag")
* @Template("AcmeDemoFitnessBundle:Page:tag.html.twig")
*/
public function tagAction($tag = null)
{
$em = $this->getDoctrine()->getManager();
$tags = $em->getRepository('AcmeDemoBundle:Blog')
->getPostsByTags($tag);
return array(
'tags' => $tag,
);
}
Selecting on a tag from the sidebar yields nothing, (an empty array) I'm not sure if it's a twig problem or how the doctrine query is setup.
Please help.
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;
}