blog中使用CGridView删除 进行了POST和GET,怎么实现的?求解 [ 新手入门 ]
[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]
共 0 条回复
没有找到数据。
newyii
注册时间:2012-08-14
最后登录:2013-07-25
在线时长:0小时0分
最后登录:2013-07-25
在线时长:0小时0分
- 粉丝0
- 金钱150
- 威望0
- 积分150