TouchBehavior

By Jitka Koukalova aka "jitka"
Would You like to have method touch() for some models, which updates only one field in database table and leaves 'created' and 'modified' (and all other) fields untouched?

Installation


...is simple. If You're using CakePHP 1.2, then

  1. download (or copy && paste) TouchBehavior and put it into file app/models/behaviors/touch.php
  2. customize some database table so it contains last_access datetime field
  3. in related model define Download code
        var $actsAs = array(
            'Touch' => array()
        );



Usage



If Your model has initialized $id property (after read/insert/update), then call in controller (for touch of current record)
Download code
$this->ModelName->touch();




If You want to touch some other record with known id, then call in controller
Download code
$this->ModelName->touch($someId);



As You can see in code of TouchBehavior, You can use any field name - just define $acstAs property in Your model like
Download code
    var $actsAs = array(
        'Touch' => array('field' => 'my_own_field_name')
    );




Field 'last_access' (or custom one) will be also properly filled up in every insert/save, but beware: if You're using argument $fieldList in model's save() calls then don't omit Your 'last_access' field in this array.



Behavior Class:

Download code <?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
     */
    
var $defaultSettings = array('field' => 'last_access''timestamp' => 'Y-m-d H:i:s');

    
/**
     * Prepare model settings
     * 
     * Redefines parent method
     */
    
function setup(&$model$config = array()) {
        
$field $this->defaultSettings['field'];

        if (!empty(
$config['field'])) {
            
$field $config['field'];
        }

        
// conditional initialization of settings for this model
        
if ($model->hasField($field)) {
            
$timestamp $this->defaultSettings['timestamp'];
            
            if (!empty(
$config['timestamp'])) {
                
$timestamp $config['timestamp'];
            }

            
$this->settings[$model->name] = array(
                
'field' => $field
                
'timestamp' => $timestamp
            
);
        }
    }

    
/**
     * Updates only field for last access information
     */
    
function touch(&$model$id null) {
        if (isset(
$this->settings[$model->name])) {
            if (!empty(
$id)) {
                
$model->id $id
            }

            if (!empty(
$model->id)) {
                
$field $this->settings[$model->name]['field'];

                
// uses $fieldList argument, so data can be prepared in beforeSave()
                
return $model->save(nullfalse, array($field));
            }
        }
        
        return 
false;
    }

    
/**
     * Modify last access field on every save
     * 
     * Redefines parent method
     */
    
function beforeSave(&$model) {
        if (isset(
$this->settings[$model->name])) {
            
$field $this->settings[$model->name]['field'];
            
$timestamp $this->settings[$model->name]['timestamp'];

            
$model->data[$model->name][$field] = date($timestamp);
        }
    }
}
?>

Comments 198

CakePHP team comments Author comments

Comment

1 Thanks

Thanks for the article - just the thing I was looking for.
posted Wed, Jan 10th 2007, 05:46 by Dr. Tarique Sani

Comment

2 Good job

Good job figuring out the behavior API with no documentation. This is pretty much exactly how they should be implemented.
posted Wed, Dec 31st 1969, 18:00 by Nate

Comment

3 Many thanks

to You, Nate. Originally it was only a fast example for someone in IRC channel ;) poLK
posted Wed, Dec 31st 1969, 18:00 by Jitka Koukalova

Comment

4 Great

Just what I needed. Thanks a million!
posted Wed, Dec 31st 1969, 18:00 by Mladen Mihajlovic

Login to Submit a Comment