How To Tell Cgridview Deletion Went Wrong?

hi,

I have two models: Drink and DrinkCategory. I want to prevent non-empty categories from being deleted. So I have a DrinkCategory::beforeDelete() which returns true/false.

This is my delete action in the controller:


	public function actionDelete($id)

	{

		$this->loadModel($id)->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'));

	}

How do I display some kind of an error when user wants to delete a non-empty category in CGridView widget?

I think here is the solution that you need:

http://www.yiiframework.com/wiki/205/how-to-show-ajax-delete-status-in-cgridview-like-flash-messages/

worked with me :D

Hi

First You want to delete drink category id from the DrinkCategory table then after you delete the Drink data because it’s relational ship from DrinkCategory table

Best Regards,

Ankit Modi

Gustavo: thanks, it seems to be what I need.

Ankit: I don’t want to delete Drinks, I want to prevent non-empty DrinkCategory from being deleted and somehow let the user know that it did not happen. The page Gustavo linked to tells me what I need to do.

Hi,

you can try this code.

    $model= new Drinkcategory();


if (!Yii::app()->getRequest()->getIsPostRequest()) {


	$coupon_record = Drink::model()->findAll("drink_cat_id='".$id."'");


	$flag = 0;


	if(isset($coupon_record) && count($coupon_record) > 0)


	{


		$flag = 1;


	}


if($flag == 0){


		$this->loadModel($id, 'Drinkcategory')->delete();


		$this->redirect(CController::createUrl('Drinkcategory/index'));


	}else{


              $this->redirect(CController::createUrl('Drinkcategory/index?del=exist'));


		Yii::app()->end;


	}





	//}


	





} else


throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));

}

then after yor view file

if(isset($_GET[‘del’]) && !empty($_GET[‘del’]))

{

$delete = $_GET['del'];

}

?>

<script type="text/javascript">

&#036;(document).ready(function(){


	var del = '&lt;?php echo &#036;delete; ?&gt;';


	if(del == 'exist')


	{


		alert('Dink Category cannot be deleted, it is related to Drink');


		window.location.href = '&lt;?php echo CController::createUrl('/Drinkcategory/index');?&gt;';


	}


});

</script>

Best Regards,

Ankit Modi

Thanks, I already have it working.