先在protected\components中建立DeleteAction.php,内容如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | classDeleteActionextendsCAction { public$pk='id'; public$redirectTo='index'; public$modelClass; functionrun() { if(empty($_GET[$this->pk])) thrownewCHttpException(404); $model= CActiveRecord::model($this->modelClass)->findByPk($_GET[$this->pk]); if(!$model) thrownewCHttpException(404); if($model->delete()) $this->redirect($this->redirectTo); thrownewCHttpException(500); } } |
在要用到此Action的地方加上:
| 1 2 3 4 5 6 7 8 9 | functionactions() { returnarray( 'delete'=>array( 'class'=>'DeleteAction', 'modelClass'=>'User',//model名字 ); ); } |