In my application . Before delete a record,program will check this record is used by another record or not.Such as:
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
//$this->loadModel($id)->delete();
$shipments = Shipment::model()->findAllByAttributes(array('product_id'=>$id));
if(count($shipments) > 0 ) {
[color="#FF0000"]//Show a message { Can not delete!}[/color]
}
else { $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'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
When a record can not delete,how to show a message to user .
My native language is not english.I try my best to wrote this.
if you not use the CGridView but a normal html link tag . just do it using ajax ; or CHtml::ajaxLink CHtml::ajaxButton … the underline use the jquery 's ajax method so see the jquery doc for more info .
a simple example :
<a href="xxx/delete/id/33" class="delete" >deleteItem</a>
<script type="javascript/text">
$(function(){
$(".delete").on("click",function(){
if(!confirm("do you really want to delete it?")) return false ;
var url = $(this).attr("href");
$.post(url,function(response){
alert(response);
}
,"json" // comment this line to see the response
);
return false;
});
});
</script>
the php end is relax , you can send every thing to ajax .