简而言之,Users
有很多Posts
,它们有很多Tags
。
此外,Users
有许多Recipes
,它们具有并属于许多Ingredients
。
$this->User->find('all'); // Yields one level, no associated Tags or Ingredients.
添加:
$this->User->find('all', array('recursive' => 2); // Yields two levels, all Tags and Ingredients
如果我只想递归一个字段,即 Recipe
,所以我得到所有Ingredients
怎么办?
看来我想使用contain
行为,但这似乎没有效果。如果我删除recursive
行为,我只会得到一个级别的数据。
也把它包含在我的模型中。
public $actsAs = array('Containable');
您可以使用可包含的行为,如下所示。
$this->User->find('all', array('contain' => 'Ingredient'));
不过,如果您已经使用recursive
,并且希望删除所有帖子数据,则快速解决方案是按如下所示unbind
帖子模型。
$this->User->unbindModel(
array('hasMany' => array('Post'))
);
$this->User->find('all', array('recursive' => 2);