鲜为人知的事实:无论如何,Cake 确实将它们作为对象或对象的属性返回。数组是语法糖:
// In your View:
debug($this->viewVars);
Shwoing$this
是一个 View 对象,属性对应于控制器动作的viewVars
$this->set('key', $variable)
or $this->set(compact('data', 'for', 'view'))
。
$Post->id
为了击键而将它们压入的问题是Cake就是原因。Cake 被设计成一个举重的工具,因此它的内置 ORM 非常强大,不可避免,并且旨在解决无限关联表的无限行 - 自动回调,自动数据传递,查询生成等。多维数组的基本深度取决于在您的 find 方法中,一旦您使用多个关联模型的多个 $Post(例如),您就已经将数组引入了组合中,并且无法避免这种情况。
不同find
的方法返回不同深度的数组。从默认生成的控制器代码中,您可以看到 index 使用$this->set('posts', $this->paginate());
- view 使用$this->set('post', $this->Post->read(null, $id));
和 edit 根本不$this->set
与 Post find 一起使用 - 它分配$this->data = $this->Post->read(null, $id);
.
FWIW,Set::map
可能会抛出这些undefined index
错误,因为(猜测)你碰巧试图映射一个编辑动作,amirite?默认情况下,编辑操作仅用于$this->set
将关联的模型查找设置到视图。$this->read 的结果被发送到$this->data
。这可能就是 Set::map 失败的原因。无论哪种方式,你最终还是会瞄准$Post[0]->id
or $Post->id
(取决于你发现你使用的方法),这并没有太大的改进。
以下是这些操作的 Set::map() 属性深度的一些通用示例:
// In posts/index.ctp
$Post = Set::map($posts);
debug($Post);
debug($Post[0]->id);
// In posts/edit/1
debug($this-viewVars);
debug($this->data);
// In posts/view/1
debug($this-viewVars);
$Post = Set::map($post);
debug($Post->id);
http://api13.cakephp.org/class/controller#method-Controllerset
http://api13.cakephp.org/class/model#method-Modelread
http://api13.cakephp.org/class/model#method-ModelsaveAll
HTH。