【问题标题】:Doctrine: join one-to-one relation教义:加入一对一的关系
【发布时间】:2010-12-16 11:13:37
【问题描述】:

假设我们有两个模型:PostViewsCount。关系类型为 1:1。

现在我想检索最近 5 个帖子及其观看次数统计信息:

$posts = PostTable::getInstance()->createQuery('p')
    ->leftJoin('p.ViewsCount') // relation name is "ViewsCount"
    ->orderBy('p.created_at DESC')
    ->limit(5)
    ->execute();

但是,没有运气。它抛出一个错误。如果我取消加入 - 一切正常。

所以,我的问题是 - 如何在 Doctrine 中自动加入/检索一对一关系以避免大量额外查询?

【问题讨论】:

    标签: orm symfony1 doctrine entity-relationship


    【解决方案1】:

    您的语法有误。您还需要告诉学说您要检索 ViewCount 字段:

    $posts = PostTable::getInstance()->createQuery('p')
      ->select('p.*, vc.*')
      ->leftJoin('p.ViewsCount vc')
      ->orderBy('p.created_at DESC')
      ->limit(5)
      ->execute();
    

    【讨论】:

    • 谢谢,但没有帮助。现在我又遇到了一个关于补水的错误
    【解决方案2】:

    似乎您应该正确定义关系。 为每个模型定义与类型键(类型:一)的关系。

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多