I have the following situation: A work has a Image which belongs to a ImageGroup.
If I do this:
$works = $this->Work->find('all', array(
'contain' => array(
'PreviewImage' => array(
'fields' => array('PreviewImage.id', 'PreviewImage.filename', 'PreviewImage.alt'),
'ImageGroup' => array(
'fields' => array('ImageGroup.id')
)
)
)
));
I'll get this:
array(
(int) 0 => array(
'Work' => array(
'id' => '1',
'title_us' => 'Work Title',
'title_de' => 'Arbeit Titel',
'alias' => 'work-title',
'pdf_id' => '1',
'date' => '2013-10-08 10:36:00',
'title_graphic_us_id' => '1',
'title_graphic_de_id' => '2',
'body_us' => 'English Content',
'body_de' => 'Deutscher Inhalt',
'preview_image_id' => '3',
'preview_us' => 'English Preview Text',
'preview_de' => 'Deutscher Vorschau Text',
'work_layout_id' => '2',
'state_id' => '1'
),
'PreviewImage' => array(
'id' => '3',
'filename' => 'preview_image.png',
'alt' => 'Preview Image',
'image_group_id' => '2',
'ImageGroup' => array(
'id' => '2'
)
)
)
)
But if I'm trying to reduce the fields for work by the following:
$works = $this->Work->find('all', array(
'fields' => array(
'Work.id'
),
'contain' => array(
'PreviewImage' => array(
'fields' => array('PreviewImage.id', 'PreviewImage.filename', 'PreviewImage.alt'),
'ImageGroup' => array(
'fields' => array('ImageGroup.id')
)
)
)
));
I suddenly don't get the ImageGroup in PreviewImage anymore, see here:
array(
(int) 0 => array(
'Work' => array(
'id' => '1'
),
'PreviewImage' => array(
'id' => '3',
'filename' => 'preview_image.png',
'alt' => 'Preview Image',
'image_group_id' => '2'
)
)
)
I don't understand why, do you have any idea?
I'm not 100% sure whether this is the intended behaviour, but you have to include the PreviewImage
foreign key field in the fields
configuration to get it working:
$works = $this->Work->find('all', array(
'fields' => array(
'Work.id',
'Work.preview_image_id'
),
'contain' => array(
...
)
));
The docs mention to ensure that all necessary foreign keys are included when using the fields
option with contain
:
When using ‘fields’ and ‘contain’ options - be careful to include all foreign keys that your query directly or indirectly requires.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
So this might be something for feature request. Personally I would have expected that this happens automagically.