当前位置: 动力学知识库 > 问答 > 编程问答 >

php - CakePHP how to automatically include 3rd level relationship on one field?

问题描述:

In short, Users have many Posts, which have and belong to many Tags.

Also, Users have many Recipes, which have and belong to many Ingredients.

$this->User->find('all'); // Yields one level, no associated Tags or Ingredients.

Adding:

$this->User->find('all', array('recursive' => 2); // Yields two levels, all Tags and Ingredients

What if I just want to recurse one field, namely Recipe, so I get all of the Ingredients?

It appears I want to use the contain behavior, but this seems to have no effect. If I remove the recursive behavior I only get one level of data.

Also have this included in my model.

public $actsAs = array('Containable');

网友答案:

You can use Containable Behavior as below.

$this->User->find('all', array('contain' => 'Ingredient'));

Though if you already used recursive , and if you wish to remove all post data, a quick solution is to unbind your post model as like below.

$this->User->unbindModel(
    array('hasMany' => array('Post'))
);
$this->User->find('all', array('recursive' => 2);
分享给朋友:
您可能感兴趣的文章:
随机阅读: