newyii 2012-08-15 10:40:11 3892次浏览 0条回复 0 0 0

[attach]844[/attach]

将鼠标放到删除按钮 显示了一个带有id的url 感觉是GET传值,但是看控制器里面的delete动作,应该是post传值,可能是不太熟悉ajax, 不清楚到底是什么传值的,看调试器里面还有一个admin的GET动作,是为了清楚搜索框里面的值而进行的吗?这个CGridView是怎么实现两个动作的? 打开zii.widgets.grid.CGridView 看不懂 求解答

public function actionDelete($id)
{
	if(Yii::app()->request->isPostRequest)
	{
		// we only allow deletion via POST request
		$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.');
}

public function actionAdmin()
{
	$model=new Post('search');
	$model->unsetAttributes();  // clear any default values
	if(isset($_GET['Post']))
		$model->attributes=$_GET['Post'];

	$this->render('admin',array(
		'model'=>$model,
	));
}

按照博客教程将delete修改为

public function actionDelete()
{
    if(Yii::app()->request->isPostRequest)
    {
        // we only allow deletion via POST request
        $this->loadModel()->delete();
 
        if(!isset($_POST['ajax']))
            $this->redirect(array('index'));
    }
    else
        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}


public function loadModel()
{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
			{
				if(Yii::app()->user->isGuest)
					$condition='status='.Post::STATUS_PUBLISHED
					.' OR status='.Post::STATUS_ARCHIVED;
				else
					$condition='';
				$this->_model=Post::model()->findByPk($_GET['id'], $condition);
			}
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
}

感觉这个loadmodel是GET接收值的 以为是无法成功删除的,结果不但删除了调试框中执行了三次动作,怎么回事??? [attach]845[/attach]

    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册