Little known fact: Cake DOES return them as objects, or well properties of an object, anyway. The arrays are the syntactical sugar:
// In your View:
debug($this->viewVars);
Shwoing $this
is a View object and the viewVars
property corresponds with the $this->set('key', $variable)
or $this->set(compact('data', 'for', 'view'))
from the controller action.
The problem with squashing them into $Post->id
for the sake of keystrokes is Cake is why. Cake is designed to be a heavy lifter, so its built-in ORM is ridiculously powerful, unavoidable, and intended for addressing infinity rows of infinity associated tables - auto callbacks, automatic data passing, query generation, etc. Base depth of multidimensional arrays depends on your find method, as soon as you're working with more than one $Post with multiple associated models (for example), you've introduced arrays into the mix and there's just no avoiding that.
Different find
methods return arrays of different depths. From the default generated controller code, you can see that index uses $this->set('posts', $this->paginate());
- view uses $this->set('post', $this->Post->read(null, $id));
and edit doesn't use $this->set
with a Post find at all - it assigns $this->data = $this->Post->read(null, $id);
.
FWIW, Set::map
probably throws those undefined index
errors because (guessing) you happen to be trying to map an edit action, amirite? By default, edit actions only use $this->set
to set associated model finds to the View. The result of $this->read is sent to $this->data
instead. That's probably why Set::map is failing. Either way, you're still going to end up aiming at $Post[0]->id
or $Post->id
(depending on what you find method you used), which isn't much of an improvement.
Here's some generic examples of Set::map() property depth for these actions:
// 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.