我已经阅读了 CakeBook 上关于Containable 数组的指南。
class Post extends AppModel {
public $actsAs = array('Containable');
结果是:
array(
(int) 0 => array(
'Post' => array(
'id' => '1',
'title' => 'This is the first post',
'slug' => 'this-is-the-first-post',
'body' => 'body body 111body body 111body body 111body body 111body body 111body body 111body body 111',
'image_id' => '0',
'language' => 'sv',
'translation_id' => '0',
'created' => '2012-09-03 17:45:57',
'modified' => '2012-09-03 19:12:21'
),
'Image' => array(
'id' => null,
'name' => null,
'url' => null,
'alt' => null,
'description' => null
),
'Asset' => array(),
'Readmore' => array(),
'Reference' => array(),
'Category' => array(),
'Course' => array(),
'Tag' => array(),
'User' => array(
(int) 0 => array(
'password' => '*****',
'id' => '2',
'username' => 'test0',
'fullname' => 'Test Test',
'url' => '',
'email' => '',
'role' => 'admin',
'phone' => '+',
'created' => '2012-09-03 17:44:22',
'PostsUser' => array(
'id' => '1',
'post_id' => '1',
'user_id' => '2'
)
),
(int) 1 => array(
'password' => '*****',
'id' => '1',
'username' => 'test1',
'fullname' => 'Frank',
'url' => '',
'email' => '',
'role' => 'admin',
'phone' => '',
'created' => '2012-09-03 17:41:25',
'PostsUser' => array(
'id' => '3',
'post_id' => '1',
'user_id' => '1'
)
)
)
),
但是打印出数组的特定部分是行不通的。因此,如果我在 Post 的索引视图中,我会写:
echo h($post['Post']['title']);
一切都很好,但是当我尝试时:
echo h($post['User']['fullname']);
它不起作用。我明白了,当我在帖子和用户之间有一个 hasmany 和 belgosto 关系时,我必须遍历用户。
那么如何打印与帖子关联的所有用户?