4,249
社区成员




数据表
category: category_id category_title
subcategory:subcategory_id subcategory_category(与category_id关联)
在删除category表的数据时,要确保category_id在subcategory_category中不存在
控制器:
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
$model=$this->loadModel($id);
$results = Category::getSubCategorySet($id);
//查找当前category_id是否在subcategory_category中存在
//存在的话,就要提醒用户
//我这里是通过抛异常来提醒用户的,但我不想要错误编号的出现
//或者你们有更好的方法可以告诉我
if($results!=0){
throw new CHttpException(400,'This title is use on sub-category management');
}else{
$model = $this->loadModel($id);
$model->category_status = 'D';
if ($model->validate()) {
$model->save();
}
// $model->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
}
SiteController.php
public function actionError()
{
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest)
echo $error['message'];
//去掉这个就不会显示错误信息 但错误编号还在 Error:400
else
$this->render('error', $error);
}
}
site视图下有 error.php 整个删掉都没影响