CakePHP 包含不适用于递归

标签 cakephp model cakephp-1.2 containable

当我尝试:

// Removed the limit to ensure that all of the group notes items can be found and collapsed
$recent_notes = $this->User->Note->find('all', array(
    'recursive' => 2,
    'order' => 'Note.created DESC',
    'conditions' => $conditions,
    'contains' => array(
        'NotesUser', 'Poster', 'Comment' => array('Poster')
    )
));

它不限制输出 - 我得到了每个相关的模型。但是,当我没有指定 recursive 时如 2 ,或者如果我将其指定为 1 ,我错过了Comment=>Poster模型。

我怎样才能只得到我需要的模型? 谢谢!

最佳答案

要仅获取您需要的模型,请使用 [Containable behavior] :

  • 将递归设置为 -1
  • 使用 'contain'单数,非 'contains'复数,就像你有
  • 确保您正在设置 $actsAs模型中的变量:public $actsAs = array('Containable');

  • 我认识的每个人都设置了 $this->recursive = -1;在 AppModel 中...这将所有内容默认为递归 -1,因此除非您想包含更多数据,否则您不必再次设置它...在这些情况下,我几乎总是使用 contain() , 不是 $recursive .

    关于CakePHP 包含不适用于递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9504946/

    相关文章:

    mysql - 我如何在 cakephp 中获取用户与所有其他用户的最后消息

    model-view-controller - 模型如何更新 MVC 模式中的 View ?

    c# - 从 DataTable 映射到属性名称不同于列名称的自定义模型

    CakePHP 为不同类型的评论制作子模型

    php - 使用 CakePHP 获取插入的 ID,无需使用模型

    php - 在 CakePHP 中插入多个表

    CakePHP:使用自定义数组提供 paginate()

    ruby-on-rails-3 - rails 3 基于 bool 值验证存在

    php - 避免同一个 JS 文件的多个实例

    php - CakePHP Auth 检索额外数据