Php 用于更改yii2中状态的控制器操作
我试图在userController中创建actionSuspend。 但是,当我点击按钮更新状态时,它会将我重定向到一个空白页面,或者我创建的函数不起作用。 这是我的控制器代码:Php 用于更改yii2中状态的控制器操作,php,yii2,status,Php,Yii2,Status,我试图在userController中创建actionSuspend。 但是,当我点击按钮更新状态时,它会将我重定向到一个空白页面,或者我创建的函数不起作用。 这是我的控制器代码: public function actionSuspend() { $user_model = \app\models\User::find() ->where(['social_worker'=>'1'])
public function actionSuspend()
{
$user_model = \app\models\User::find()
->where(['social_worker'=>'1'])
->one();
if($user_model == 'suspend'){
$user_model->social_worker = 'activate';
return $this->redirect(['index']);
}
}
我的视图代码是
'suspend' => function($url,$model){
if($model->social_worker ==1)
return Html::a('<span class="btn btn-xs btn-danger icon-remove bigger-80"style="margin-left:5px;"></span>', $url,['title'=>Yii::t('app','Revoke social worker')]);
},
'suspend'=>函数($url$model){
如果($model->社工==1)
返回Html::a(“”,$url,['title'=>Yii::t('app','Revoke socialworker'));
},
尝试使用
if($user_model->status == 'suspend'){
$user_model->social_worker = 'activate';
return $this->render(['index']);
}
试一试
if($user_model->status == 'suspend'){
$user_model->social_worker = 'activate';
return $this->render(['index']);
}
如果($user\u model=='suspend')
是错误的吗?你可以添加代码,围绕暂停和激活,以及它的一部分是什么?可以说的是,您的action方法肯定应该包含一个render调用。当然没有你的空白页。通常它类似于return$this->render('index')代码>。你需要保存你的模型。我想你应该让自己更熟悉Yii2,先读一读。我认为你需要更多的基础知识。如果($user\u model=='suspend')
是错误的吗?你可以添加代码,围绕暂停和激活,以及它的一部分是什么?可以说的是,您的action方法肯定应该包含一个render调用。当然没有你的空白页。通常它类似于return$this->render('index')代码>。你需要保存你的模型。我想你应该让自己更熟悉Yii2,先读一读。我认为您需要更多的基础知识。我尝试了这个方法,但得到了一个错误strncmp()希望参数1是字符串,数组给定公共函数actionSuspend(){$user\u model=\app\models\user::find()->where(['user\u is\u social\u worker'=>'1'])->one();if($user_model->user_is_social_worker=='suspend'){$user_model->social_worker='activate';$user_model->save();return$this->render(['index']);}}}仍然不工作…@MohammedIqbal不足为奇,它需要是$this->render('index'))
,没有大括号!看看…这个答案不是很有用…我的答案刚刚指出,你不能比较$model\u user,但你至少需要指定一个字段,我猜它是status
,然后你必须比较$model\u user->status。因为它在用户类上不可见,所以我想是这样。trying以精确指示用户模型的代码。我尝试了此操作,但得到一个错误strncmp()期望参数1为字符串,数组给定公共函数actionSuspend(){$User\u model=\app\models\User::find()->其中(['User\u is\u social\u worker'=>'1'])->一个();如果($user_model->user_is_social_worker=='suspend'){$user_model->social_worker='activate';$user_model->save();return$this->render(['index']);}}}仍然不工作…@MohammedIqbal不足为奇,它需要是$this->render('index'))
,没有大括号!看看…这个答案不是很有用…我的答案刚刚指出,你不能比较$model\u user,但你至少需要指定一个字段,我猜它是status
,然后你必须比较$model\u user->status。因为它在用户类上不可见,所以我想是这样。trying精确表示用户模型的代码。