class User extends CActiveRecord { protected function beforeDelete() { // Remove all related comments Comment::model()->deleteAllByAttributes(array('user_id'=>$this->id)); return parent::beforeDelete(); } }
class Post extends CActiveRecord { protected function beforeDelete() { if ($this->status == 'published') { throw new CHttpException(500,'Cannot delete a published post'); } return parent::beforeDelete(); } }In this example, the beforeDelete function is used to prevent the deletion of a post if it is already published. If the post is published, an exception is thrown, preventing deletion. The CActiveRecord class is part of the Yii PHP framework. It is a package library that provides a set of classes for managing database records as objects. It offers a simple way to interact with the database and perform CRUD operations on database records.