0

am new for YII. i custom table created displayed tables values while update the form. so i want to update the form. when i click to delete button the row wants to be delete. i wrote delete query which is

![i added screen shot. which shows del button. if click entire row wants to be delete][1]

In this query. entire tables gets deleted when am refresh page.. please suggest me suitable answer

 $id =$_REQUEST['id'];
        $id1=10;
         $command ="DELETE FROM holidays WHERE holiday_id='$id' and recipe_id='$id1'";
   $command = Yii::app()->db->createCommand($command);
      $command->execute();
1
  • please post your full controler code Commented Feb 20, 2014 at 7:35

1 Answer 1

1

You should do it the "Yii way".

// Load the entity
$holiday = Holiday::model()->findByPk($_REQUEST['id']);

// delete entity
$holiday->delete();

Read Working with Database for details.

A good example of deleting by AJAX is included in the gii-generated code. You can create a delete link by

Yii::app()->controller->createUrl("delete",array("id"=>$data->primaryKey))

and the controller then deletes the entity by this function

public function actionDelete($id) {
    if (Yii::app()->getRequest()->getIsPostRequest()) {
        $this->loadModel($id, 'Holiday')->delete();

        if (!Yii::app()->getRequest()->getIsAjaxRequest())
            $this->redirect(array('index'));
    } else
        throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
}
Sign up to request clarification or add additional context in comments.

2 Comments

how to pass the ajax value in script
how to add the ajax option <script> function change(x,y){ $.ajax({ url: <?php Yii::app()->controller->createUrl("holidays/holidaydelete",array("id"=>$data->primaryKey));?>, type: 'POST', // async: false, data: 'DELETE id from holidays where id=$id', }); } </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.