如何在一个Doctrine查询中获取所有左连接数据和过滤数据?

时间:2011-02-21 20:25:23

标签: php sql symfony1 doctrine

我希望能够将我的博客文章缩小到包含特定标记的帖子。但是,对于包含特定标记的每篇博文,我想在同一查询中获取所有标记。所以,使用这个查询:

BlogTable::getInstance()
    ->createQuery()
    ->select('b.*, t.*')
    ->from('Blog b')
    ->leftJoin('b.Tags t')
    ->where('t.content = ?', $tag)
    ->execute()

只会加入与指定标记相同的标记,这会导致在需要时延迟加载标记的额外查询。

如何按标记缩小博客帖子,但同时在一个查询中获取帖子的所有标记?

1 个答案:

答案 0 :(得分:1)

您需要两个内部联接,一个用于确保特定标记存在(我的样本中为t1),另一个用于返回所有现有标记(我的样本中为t2)。

BlogTable::getInstance()
    ->createQuery()
    ->select('b.*, t2.*')
    ->from('Blog b')
    ->InnerJoin('b.Tags t1')
    ->InnerJoin('b.Tags t2')
    ->where('t1.content = ?', $tag)
    ->execute()