Delete Action Not Working

Hi,

I have a 3 tiered table structure for some config settings in my database

Object table - the parent of the settings that describes the settings and holds multiple "sets" of values

Sets table - a set of values that belong to an object

Values table - multiple value records that belong to a set

In my admin page I have the following code to generate the delete button in the gridview




		array(

                    'class'=>'CButtonColumn',

                    'template'=>'{view}{update}{delete}', //($data->drawroundcount > 0 ? "{view}" : "{view}{update}{delete})',

                    'viewButtonUrl'=>'Yii::app()->createUrl("Settingconfigobj/view", array("id"=>$data->id))',

                    'updateButtonUrl'=>'Yii::app()->createUrl("Settingconfigobj/update", array("id"=>$data->id))',

                    'deleteButtonUrl'=>'Yii::app()->createUrl("Settingconfigobj/delete", array("id"=>$data->id))',

                ),



In the same grid I have an option to copy an Object along with the sets and values it contains which does call the copysettings action, here is the code for that:




/* COLUMN CODE FROM GRIDVIEW */

                    'class'=>'CButtonColumn',

                    'template'=>'{copy}',

                    'buttons'=>array(

                        'copy' => array( 

                            'label' => 'COPY', 

                            'url' => 'Yii::app()->createUrl("/settingconfigobj/copysettings", array("id"=>$data->id))', 

                            'options'=>array('class'=>'to-copy'),

                        ),


/* JQUERY TO LOAD INTO PAGE FOR COPY FUNCTION */

$confirm = 'jQuery("#settingconfigobj-grid a.to-copy").live("click",function() {

        if(!confirm("Are you sure you want copy this item?")) return false;

        var url = $(this).attr("href");

        //  do your post request here

        $.post(url,function(res){

        //     alert(res);

         });

        return false;

});';

$cs = Yii::app()->clientScript;

$cs->registerScript('copyobj', $confirm, CClientScript::POS_READY);



Here is the controller action that should be getting called for the Delete action:




	public function actionDelete($id)

	{

		$model = $this->loadModel($id);


                $orgid = $model->orgid;

                

                if($model->compcount > 0)

                {

                    Yii::app()->user->setFlash('error', "Cannot delete Config Settings that is attached to a comp");

                }

                else {

                    

                    try {

                        

                        $transaction = $model->dbConnection->beginTransaction();


                        //delete all the values in each set first

                        $sets = Settingconfigset::model()->findAll('settingconfigid=:id', array(':id'=>$id));

                        foreach($sets as $set) {

                            Settingconfigvalue::model()->deleteAll('settingconfigsetid=:id', array(':id'=>$set->id));

                        }


                        //now delete the sets

                        Settingconfigset::model()->deleteAll('settingconfigid=:id', array(':id'=>$id));

        

                        //lastly, delete the actual ojbject

                        $model->delete();


                        $transaction->commit;


                        Yii::app()->user->setFlash('success', 'Deleted "'.$model->configname.'" successfully');


                    } 

                    catch (Exception $ex) {

                        

                        $transaction->rollback();

                        

                        Yii::app()->user->setFlash('error', 'could not 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', 'id'=>$orgid));

	}



The page loads the default delete jquery code which works fine on other pages.

When I click the delete icon, I am prompted with the text from the jquery delete function, so this part is working. It doesnt seem to fire the actionDelete function from the controller tho. I dont see any flash messaging either.

Any help would be greatly appreciated.

Regards

Greg J

Hi,

By default yii accepts the delete action only through the POST request. Please check your filter in your controller for postonly…

Please don’t allow delete action in get method… its not safer…

Cheers!

Thanks for this,

I understand whats happening now much better.

I ended up changing the relationship between tables to support cascade delete and reverted to the default code for the controller action function.

So it seems to me that testing any custom code in the actionDelete function fired from a CGridView is almost impossible to debug.

Am I right in saying that I would need to get my actionDelete function working via non-ajax mode first?

Regards

Greg J