I've been looking far and wide and still haven't been able to find an example of how to setup a query to look for a specific 'tag' that the user selects from a sidebar which in turn will bring up all posts with that tag.
I understand how to find all tags, but not to find a specific selected by the user.
blogrepository
public function getTags($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;
}
1st solution : You should use a doctrine query.
PostRepository.php
2nd solution : Use Many To Many relation and get directly from doctrine
Entity/Tag.php
Entity/Post.php
So you can do
$tag->getPosts();
to get all relative posts3rd solution : Really ugly, but the tutorial is not designed to be improved ... Get all blog post and parsing each string to find if your tag is in.
I believe this will work for you.