当我尝试:
// 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] :
'contain'
单数,非 'contains'
复数,就像你有 $actsAs
模型中的变量:public $actsAs = array('Containable');
我认识的每个人都设置了
$this->recursive = -1;
在 AppModel 中...这将所有内容默认为递归 -1,因此除非您想包含更多数据,否则您不必再次设置它...在这些情况下,我几乎总是使用 contain()
, 不是 $recursive
.
关于CakePHP 包含不适用于递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9504946/