CakePHP: auto-set model create and update dates

Mikesname picture Mikesname · Dec 13, 2010 · Viewed 9.3k times · Source

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.

Answer

RabidFire picture RabidFire · Dec 13, 2010

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.