Problem With Delete Check

Hi all !

I got a problem when delete a record.

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.

Thanks for help!

see here

in the afterDelete method firstly alert the "data" to see what it is :




array(

    class'=>'CButtonColumn',

    'afterDelete'=>'function(link,success,data){ if(success) alert(data); }',

 ),



if the data is passed from php end . then you can use this to transfer info and decide what to display , it depends you .

note if the data format is json you can use jquery 's jQuery.parseJSON(data) to parse it :lol:

thanks for your help!

How can I transfer info to [link,success,data] params from controller?

And ,CButtonColumn is in the CGridView, I also used a delete link to do this.How to add a afterDelete function to a link ?


$this->menu=array(


	array('label'=>'delete product','icon'=>'reed-delete-small', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure?')),

);

?>

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 .

echo xxxx ; echo CJSON::encode(array(‘status’=>true)); die();

it depend you . :lol: