0

in a CakePHP 2.3 project, in one of the controller actions I want to update several records of a table. The data to be updated is posted as an array, and I iterate through that array.

However, some new field values are related to current field values, therefore I cannot simply write the data in an array $data and do model-save($data). Instead I do

$record = model->read(null, $id); //$id is retrieved from the posted data array. 
$record['some_field'] = $new_value;
unset($record['modified']);
//in addition I used model->modified = null;, but to no avail
model->save($record);

Problem is, that the field modified is not automagically updated. In the CakePHP documentation I found that the value for "Modified" must not be present in the data that is to be saved. But the unset() alone doesn't seem to be enough.

In cakePHP - modified field not updating user tadasZ mentioned that it doesn't work when you use model->read() in advance.

I couldn't find anything about it in the documentation. But if that is the case, is there any way at all to use the Automagic for the field modified? I can set the field value myswlf (in fact, right now that's what I do as a workaround), but if there is an automatic way, I would like to use it.

3
  • stackoverflow.com/questions/6893605/…
    – Kai
    Commented Nov 8, 2013 at 18:21
  • Try to prevent read(). You can easily make yourself a method that uses find() and save() or saveField().
    – mark
    Commented Nov 8, 2013 at 20:15
  • @Kai: That is the question I have read and quoted myself above, I am not sure what you are trying to tell me. Commented Nov 11, 2013 at 13:05

2 Answers 2

1

When you are using Model::read(), the result is still in the same CakePHP format of $array['Model']['field'] so you would have to do unset($record['Model']['modified']);

1
  • Apologies for being imprecise, the code example above was just a simplified copy of my actual code. I the actual code I already do unset($record['Model']['modified']);, but it doesn't work. Commented Nov 11, 2013 at 13:07
0

The answer is here: http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified

If you have created or modified data in your $this->data (e.g. from a Model::read or Model::set) before a Model::save() then the values will be taken from $this->data and not automagically updated. If you don’t want that you can use unset($this->data['Model']['modified']), etc.

Not the answer you're looking for? Browse other questions tagged or ask your own question.