12.25
php
saved
poLK
Note
Example behavior for cake 1.2, which implements automatic last_access field for model with touch() functionality
Example behavior for cake 1.2, which implements automatic last_access field for model with touch() functionality
- <?php
- /**
- * TouchBehavior
- *
- * Usage in model:
- *
- * 1) make sure model's $actsAs array contains something like (those keys are
- * default values You don't need to specify)
- *
- * 'Touch' => array('field' => 'last_access', 'timestamp'=>'Y-m-d H:i:s')
- *
- * 2) You can call $this->ModelName->touch() or $this->ModelName->touch(3) in
- * controller
- *
- * 3) this behavior implements beforeSave() callback for updating value of last
- * access field ;)
- */
- class TouchBehavior extends ModelBehavior {
- /**
- * Default model settings
- */
- /**
- * Prepare model settings
- *
- * Redefines parent method
- */
- $field = $this->defaultSettings['field'];
- $field = $config['field'];
- }
- // conditional initialization of settings for this model
- if ($model->hasField($field)) {
- $timestamp = $this->defaultSettings['timestamp'];
- $timestamp = $config['timestamp'];
- }
- 'field' => $field,
- 'timestamp' => $timestamp
- );
- }
- }
- /**
- * Updates only field for last access information
- */
- $model->id = $args[0];
- }
- $field = $this->settings[$model->name]['field'];
- // uses $fieldList argument, so data can be prepared in beforeSave()
- }
- }
- }
- /**
- * Modify last access field on every save
- *
- * Redefines parent method
- */
- function beforeSave(&$model) {
- $field = $this->settings[$model->name]['field'];
- $timestamp = $this->settings[$model->name]['timestamp'];
- }
- }
- }
- ?>
Parsed in 0.097 seconds, using GeSHi 1.0.7.14