Difference between #1 and #2 of AutoTimestampBehavior

unchanged
Title
AutoTimestampBehavior
unchanged
Category
Tutorials
unchanged
Tags
changed
Content
This behavior will automatically set timestamp fields to the row creation and
modification times.

~~~
[php]
<?php
class AutoTimestampBehavior extends CActiveRecordBehavior {

	/**
	* The field that stores the creation time
	*/
	public $created = 'created';
	/**
	* The field that stores the modification time
	*/
	public $modified = 'modified';
	
	
	public function beforeValidate($on) {
		if ($this->Owner->isNewRecord)
			$this->Owner->{$this->created} = new CDbExpression('NOW()');
		else
			$this->Owner->{$this->modified} = new CDbExpression('NOW()');
			
		return true;	
	}
}
~~~

### Installing to a model
Drop the following into your model to install it:
~~~
[php]
public function behaviors(){
	return array(
		'AutoTimestampBehavior' => array(
			'class' => 'application.components.AutoTimestampBehavior',
			//You can optionally set the field name options here
		)
	);
}
~~~

### Links

[Russian Version](http://dbhelp.ru/yii-auto-timestamp-behavior/page/)
Write new article
  • Written by: jonah
  • Updated by: Yang He
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +2 / -2
  • Viewed: 10,444 times
  • Created on: Feb 19, 2009
  • Last updated: Jun 29, 2012