12.25 php saved
poLK
Tags add more
beforeSave, behavior and touch  
Note
Example behavior for cake 1.2, which implements automatic last_access field for model with touch() functionality
  1. <?php
  2. /**
  3. * TouchBehavior
  4. *
  5. * Usage in model:
  6. *
  7. * 1) make sure model's $actsAs array contains something like (those keys are
  8. * default values You don't need to specify)
  9. *
  10. * 'Touch' => array('field' => 'last_access', 'timestamp'=>'Y-m-d H:i:s')
  11. *
  12. * 2) You can call $this->ModelName->touch() or $this->ModelName->touch(3) in
  13. * controller
  14. *
  15. * 3) this behavior implements beforeSave() callback for updating value of last
  16. * access field ;)
  17. */
  18. class TouchBehavior extends ModelBehavior {
  19.     /**
  20.      * Default model settings
  21.      */
  22.     var $defaultSettings = array('field' => 'last_access', 'timestamp' => 'Y-m-d H:i:s');
  23.  
  24.     /**
  25.      * Prepare model settings
  26.      *
  27.      * Redefines parent method
  28.      */
  29.     function setup(&$model, $config = array()) {
  30.         $field = $this->defaultSettings['field'];
  31.  
  32.         if (!empty($config['field'])) {
  33.             $field = $config['field'];
  34.         }
  35.  
  36.         // conditional initialization of settings for this model
  37.         if ($model->hasField($field)) {
  38.             $timestamp = $this->defaultSettings['timestamp'];
  39.            
  40.             if (!empty($config['timestamp'])) {
  41.                 $timestamp = $config['timestamp'];
  42.             }
  43.  
  44.             $this->settings[$model->name] = array(
  45.                 'field' => $field,
  46.                 'timestamp' => $timestamp
  47.             );
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * Updates only field for last access information
  53.      */
  54.     function touch(&$model, $args = array()) {
  55.         if (isset($this->settings[$model->name])) {
  56.             if (!empty($args[0])) {
  57.                 $model->id = $args[0];
  58.             }
  59.  
  60.             if (!empty($model->id)) {
  61.                 $field = $this->settings[$model->name]['field'];
  62.  
  63.                 // uses $fieldList argument, so data can be prepared in beforeSave()
  64.                 return $model->save(null, false, array($field));
  65.             }
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Modify last access field on every save
  71.      *
  72.      * Redefines parent method
  73.      */
  74.     function beforeSave(&$model) {
  75.         if (isset($this->settings[$model->name])) {
  76.             $field = $this->settings[$model->name]['field'];
  77.             $timestamp = $this->settings[$model->name]['timestamp'];
  78.  
  79.             $model->data[$model->name][$field] = date($timestamp);
  80.         }
  81.     }
  82. }
  83. ?>
Parsed in 0.097 seconds, using GeSHi 1.0.7.14

Modify this Paste