CakePHP包含不与递归一起使用

Gar*_*ett 3 cakephp model containable cakephp-1.2

当我尝试:

// 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')
    )
));
Run Code Online (Sandbox Code Playgroud)

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

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

Dav*_*ave 8

要仅获取所需的模型,请使用[可包含行为]:

  • 将递归设置为-1
  • 像你一样使用'contain'单数,而不是'contains'复数
  • 确保$actsAs在模型中设置变量:public $actsAs = array('Containable');

我认识的每个人都$this->recursive = -1;在AppModel中设置...这默认所有内容都是递归-1所以你不必再次设置它,除非你想要包含更多的数据......在这种情况下,我几乎总是使用contain(),而不是$recursive.

  • 是的 - 总是那样.加勒特:建议前进 - 标记最好的答案,而不是第一个答案.它将使StackOverflow社区受益,而不仅仅是标记谁是第一个.我不一定说我的是 - 只是对未来的建议.我们不参与竞争/获得代表 - 我们帮助人们!:) (2认同)