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?
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
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.
$(document).ready(function(){
var del = '<?php echo $delete; ?>';
if(del == 'exist')
{
alert('Dink Category cannot be deleted, it is related to Drink');
window.location.href = '<?php echo CController::createUrl('/Drinkcategory/index');?>';
}
});