Reputation: 162
I have tables: categories
HABTM sculptures
hasMany images
from the CategoriesController#find()
produces an array like so:
array(
'Category' => array(
'id' => '3',
'name' => 'Modern',
),
'Sculpture' => array(
(int) 0 => array(
'id' => '25',
'name' => 'Ami',
'material' => 'Bronze',
'CategoriesSculpture' => array(
'id' => '18',
'category_id' => '3',
'sculpture_id' => '25'
)
),
(int) 1 => array(
'id' => '26',
'name' => 'Charis',
'material' => 'Bronze',
'CategoriesSculpture' => array(
'id' => '19',
'category_id' => '3',
'sculpture_id' => '26'
)
)
)
)
I'd like to be able to get the images related to sculpture in the array as well if this is possible?
Upvotes: 0
Views: 56
Reputation: 29121
Use CakePHP's Containable Behavior. After reading about it and following the instructions, your find should look something like this:
$this->Category->find('all', array(
'contain' => array(
'Sculpture' => array(
'Image'
)
)
));
In your Category model, you need:
public $actsAs = array('Containable');
public $recursive = -1; //can set this in many places, but it must be -1 for the Containable find to work.
Upvotes: 0
Reputation: 33232
The simple way to do this is to set recursive to 2 when you call find()
(example). This will tell find()
to connect all the associated models as well as the model associated with the associated models.
However, this approach can cause your dataset to grow quite large so a better approach is to use the containable behavior when including deeper associations.
Upvotes: 1