Model events
- beforeValidate() - event will be executed before data validation
- afterValidate() - event will be executed after data validation
- beforeSave() - event will be executed before saving of data
- afterSave() - event will be executed after saving of data
- beforeDelete() - event will be executed before deleting of data
- afterDelete() - event will be executed after deleting of data
- afterConstruct() - event will be executed after execute constructor
- afterFind() - event will be executed after find of data
- beforeFind() - event will be executed before find of data
Example:
<?php
class Posts extends CActiveRecord {
//code ...
/**
* Event before data validation
*
* @return boolean
*/
protected function beforeValidate() {
//If validation of parents return the true
if (parent::beforeValidate()) {
//If new record
if ($this->isNewRecord) {
$this->createDate = time();
}
return true;
} else {
retrun false;
}
}
//code ...
}
One more example:
<?php
class Posts extends CActiveRecord {
//code
/**
* Event after find of data
*
* @return boolean
*/
protected function afterFind() {
$this->createDate = data('d.m.Y', $this->createDate);
return parent::afterFind();
}
//code
}
Controller events (events of actions)
- beforeAction() - event will be executed before any action of controller
- afterAction() - event will be executed after any action of controller
- beforeRender() - event will be executed before rendering of view
- afterRender() - event will be executed after rendering of view
Example:
<?php
class PostsController extends Controller {
//code
/**
* Will be called after executed any action of controller
*
* @return boolean
*/
protected function afterAction() {
//logging
Loger::saveLog($this->action->id, $this->id);
return parent::afterAction();
}
//code
}
Class CWebUser
- afterLogin() - event will be executed after login of user
- beforeLogin() - event will be executed before login of user
- afterLogout() - event will be executed after logout of user
Example:
<?php
class MyWebUser extends CWebUser{
public function afterLogin($fromCookie) {
parent::afterLogin($fromCookie);
//code
}
}