【问题标题】:How to Grab All of Left Joined Data and Filter Data in One Doctrine Query?如何在一个 Doctrine 查询中获取所有左连接数据并过滤数据?
【发布时间】:2011-02-21 20:25:23
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: php sql symfony1 doctrine


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多