AlexBrand
AlexBrand

Reputation: 12429

cakePHP - modified field not updating

Any ideas on how to debug a modified field that is not being updated? The created field works fine.

I'm not sure what code you would need to look at so let me know!

Thanks

Upvotes: 12

Views: 8634

Answers (7)

daerentis
daerentis

Reputation: 33

I got the same problem today and found out the modified field is only updated if any changes have been made, otherwise the field will not be touched.

Upvotes: 1

Shivek Parmar
Shivek Parmar

Reputation: 2993

In AppModel.php, just write the following code.

public function beforeSave($options = array())  {
    parent::beforeSave();
    if (isset($this->data[$this->alias]['modified'])) {
        unset($this->data[$this->alias]['modified']);
    }
    if (isset($this->data[$this->alias]['created'])) {
        unset($this->data[$this->alias]['created']);
    }

}

This will work for all models. But we can do that for individual MODELS too.

Since we have used parent::beforeSave(); as our first line therefore this can be overwritten in each individual MODEL. Thanks to inheritance.

Upvotes: 0

Jorge Ramirez
Jorge Ramirez

Reputation: 91

Most likely you are overwriting the field, unset the field from the object before saving it like this:

unset($user['User']['modified']);

Now you can save it:

$this->User->save($user);

Upvotes: 6

tadasZ
tadasZ

Reputation: 109

You should try checking if you didn't use

 $this->ModelName->read(NULL, $id); 

before saving your data, if you think you didn't - double check it

Upvotes: 5

JJJ
JJJ

Reputation: 33153

The problem might also arise if you use manual update queries. The field is updated only when you use Cake's own functions.

Upvotes: 0

Charles Sprayberry
Charles Sprayberry

Reputation: 7853

Like Anh Pham said, the modified and created field need to be DATETIME and they also need to default to NULL, as explained in the CakePHP book about the subject.

Upvotes: 8

Anh Pham
Anh Pham

Reputation: 5481

That field should be named 'modified' with 'datetime' type. Cake should update the field automatically for you. If you want to check, just query it, or look into the database.

Upvotes: 1

Related Questions