Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: auto-set model create and update dates

Is there a way to configure CakePHP models to automatically set timestamps, such as created_on and updated_on, when saving the object?

I'm thinking of an equivalent to Django's DateTimeField's auto_now and auto_now_add options.

I've heard this is a controversial practice in some frameworks, but I want to do it anyway.

If you can't do this, do people normally set the timestamps in the beforeSave() handlers, perhaps by checking if the model data already contains an id field and setting a creation data accordingly?

Thanks, and apologies if I've missed the relevant bit in the docs.

like image 776
Mikesname Avatar asked Dec 13 '10 02:12

Mikesname


2 Answers

Yes, CakePHP does this automagically for the fields created and modified.

In MySQL, the type for these fields should be DATETIME (or DATE) and NULLable.

like image 71
RabidFire Avatar answered Sep 21 '22 23:09

RabidFire


If you name your fields created and modified and make them of type datetime, Cake will handle the rest. http://book.cakephp.org/complete/1000/Models#created-and-modified-1015

like image 44
deceze Avatar answered Sep 22 '22 23:09

deceze