在yii中使用ajax删除函数

时间:2014-02-20 07:29:58

标签: ajax yii

是YII的新手。我更新表单时自定义表创建显示的表值。所以我想更新表格。当我点击删除按钮时,该行想要删除。我写了删除查询

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

在此查询中。刷新页面时整个表格被删除..请建议我合适的答案

 $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 个答案:

答案 0 :(得分:1)

你应该以“Yii方式”来做。

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

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

阅读Working with Database 了解详情。

通过AJAX删除的一个很好的例子包含在gii生成的代码中。您可以通过

创建删除链接
Yii::app()->controller->createUrl("delete",array("id"=>$data->primaryKey))

然后控制器通过此函数删除实体

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.'));
}