Question

In my controller i need to verify that some input-values are different form certain db-table-records values.
Example:

An input with a price value, when updated, must be lower then the current price. so what i do is:

$this->Model->read(null, $the_id);
//and then:
if($this->data['Model']['price'] >= $this->Model->data['Model']['Price']) do_stuff();

It works. but i fear that it's not something that can trust since i found no documentation regard this method.

Would it be right to assume that $this->data will always contain the submitted data, and that $this->Model->data will always hold the results of a read() call (or any model data reading related content)?

Was it helpful?

Solution

$this->data (in the controller) represents the POST'ed data from the browser.

$this->Model->data represents the data that is saved to the Database (or set in the model by the controller).

If you were to do:

$this->Model->set($this->data);
$this->Model->save();

That is the same as:

$this->Model->save($this->data);

Doing the latter will store the data in $this->Model->data. If you were to manipulate that data in $this->Model->data via a behavior or other beforeSave() callback, it will not replicate to the controllers $this->data array. So be careful.

So in response to your question:

Would it be right to assume that $this->data will always contain the submitted data

Yes, that is correct

and that $this->Model->data will always hold the results of a read() call (or any model data reading related content)?

Yes - but you can also store the Model data from a read directly to a variable:

$data = $this->Model->read(null, $id);

Just be aware, that the two data arrays are not linked, the Model->data will not replicate back to the $this->data in the controller. You would need to manually set that in your controller:

$this->data = $this->Model->data;

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top