yiif的Behavior事例
<?phpclass 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; }}
?
model:
public function behaviors(){ return array( 'AutoTimestampBehavior' => array( 'class' => 'application.components.AutoTimestampBehavior', //You can optionally set the field name options here ) );}?