开发者

How to Grab All of Left Joined Data and Filter Data in One Doctrine Query?

开发者 https://www.devze.com 2023-02-12 16:45 出处:网络
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

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')
    ->wher开发者_运维问答e('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?


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消