Reputation: 25765
I want to be able to narrow my blog posts to posts that contain a certain tag. But, for each blog post that contains the specific tag, I want to grab all of the tags in the same query. So, using this query:
BlogTable::getInstance()
->createQuery()
->select('b.*, t.*')
->from('Blog b')
->leftJoin('b.Tags t')
->where('t.content = ?', $tag)
->execute()
will only join the tags that equal the specified tag, which results in extra queries for lazy loading the tags when needed.
How do I narrow the blog posts by tag, but at the same time grab all of the tags for the post in one query?
Upvotes: 0
Views: 207
Reputation: 135808
You want two inner joins, one to ensure the specific tag exists (t1 in my sample) and one to return all existing tags (t2 in my sample).
BlogTable::getInstance()
->createQuery()
->select('b.*, t2.*')
->from('Blog b')
->InnerJoin('b.Tags t1')
->InnerJoin('b.Tags t2')
->where('t1.content = ?', $tag)
->execute()
Upvotes: 1