ActiveRecord的生命周期,通过方法重写和插入我们需要的业务逻辑来达到我们对程序的控制。
示例:
1,beforeSave()
public function beforeSave($insert)
{
if(parent::beforeSave($insert))
{
if($insert)
{
$this->create_time = time();
$this->update_time = time();
}
else
{
$this->update_time = time();
}
return true;
}
else
{
return false;
}
}
2,afterFind()
public function afterFind()
{
parent::afterFind();
$this->_oldTags = $this->tags;
}
3,afterSave()
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
Tag::updateFrequency($this->_oldTags, $this->tags);
}
4, afterDelete()
public function afterDelete()
{
parent::afterDelete();
Tag::updateFrequency($this->tags, '');
}