Question

I'm trying to figure out a way to get tags by blog. I know you can just get all blogs and use a for loop to get the tags per blog but using this method I am unable to select on an individual tag, I end up having all tags instead of being able to select on just one tag.

Related to what I am trying to do here: Symfony2 - How to you list one specific post tag instead of all of them?

The tags are setup as strings in the blog entity.

The query I'm theoretically trying to put together doesn't work---what is missing from it?

public function getTagsByBlog()
{
    $blogTags = $this->createQueryBuilder('b')
        ->select('b.id')
        ->where('b.id = :tag')
        ->setParameter('tag', 'b.tags');

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

entity

/**
 * 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;
}
Was it helpful?

Solution

You should select from tags table joined with blog (inside Tags repository), like this:

$blogTags = $this->createQueryBuilder('Tags')
        ->select('Tags.id')
        ->leftJoin('Tags.blog', 'Blog')
        ->where('Blog.id = :blogId')
        ->setParameter('blogId', $blogId);

This will create query with all tags ids related to Blog that has Id = $blogId.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top