Using created_at and updated_at with CakePHP


/ Published in: PHP
Save to your folder(s)

Ruby uses created_at and updated_at. Here cake will do the same thing.
based on http://book.cakephp.org/view/1015/created-and-modified


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class AppModel extends Model {
  4.  
  5. function save($data = null, $validate = true, $fieldList = array()) {
  6. $now = date('Y-m-d H:i:s');
  7. // set created_at field before creation
  8. if (!isset($this->data[$this->name]) || !$this->data[$this->name]['id']) {
  9. $data[$this->name]['created_at'] = $now;
  10. }
  11. // set updated_at field value before each save
  12. $data[$this->name]['updated_at'] = $now;
  13. return parent::save($data, $validate, $fieldList);
  14. }
  15.  
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.