Merging data into $this->data

In CakePHP, $this->data is a good place to store info. It's populated by model information when submitted by the form, it's accessible in the view, and most of the examples show model reads being pushed onto this array.

$this->data = $this->Post->read();

I've been changing things up to use array_merge instead. Any new model data will simply overwrite what is currently there and it'll prevent reads from multiple models from accidentally overwriting data in the model.

$this->data = array_merge($this->data, $this->Post->read());

If you have keys that might be the same and you want those to be merged, as well, then use array_merge_recursive.

Published August 02, 2006 · Updated August 03, 2006
Categorized as CakePHP
Short URL: https://snook.ca/s/692