Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2设定器不工作_Php_Model_Yii2_Setter - Fatal编程技术网

Php Yii2设定器不工作

Php Yii2设定器不工作,php,model,yii2,setter,Php,Model,Yii2,Setter,我对yii2 setter有一个问题,我在另一个表中有一个名为“role”的字段,该字段的用户和角色与表“user\u role”中的user\u id链接,getter可以查找,但setter不工作,yii2不调用setter函数,我尝试了太多的东西,但“规则”中的字段“角色”功能是安全的,但它没有帮助,我在用户类中编写了$role作为公共变量,我尝试将其作为私有变量,但也不起作用,我无法找出错误在哪里 class User extends BaseUser { public $given

我对yii2 setter有一个问题,我在另一个表中有一个名为“role”的字段,该字段的用户和角色与表“user\u role”中的user\u id链接,getter可以查找,但setter不工作,yii2不调用setter函数,我尝试了太多的东西,但“规则”中的字段“角色”功能是安全的,但它没有帮助,我在用户类中编写了$role作为公共变量,我尝试将其作为私有变量,但也不起作用,我无法找出错误在哪里

class User extends BaseUser
{  
public $givenname;

public static function tableName()
{
    return 'user';
}

/**
 * @inheritdoc
 */
public function rules()
{

    return [
        [['role'], 'safe'],
    ];
}


public function getRole()
{
    $sql = 'SELECT item_name FROM "user_rules" WHERE user_id = :userId';
    $command = Yii::$app->db->createCommand($sql);
    $command->bindValue(':userId', (int)$this->id, PDO::PARAM_INT);

    return $command->queryScalar();
}

public function setRole($newRole)
{
die("SetRole has been called now " );
// Save role in the DB
         .................
}

/**
 * @return type array list of the system roles
 */
public static function getSystemRoles()
{
    return array(
        'sysadmin' => 'sysadmin',
        'admin' => 'admin',
        'editor' => 'editor',
        'member' => 'member',
        'register' => 'register'
    );
}
在我的控制器中,这里是更新操作:

/**
 * Updates an existing User model.
 * @param  integer $id
 * @return mixed
 */
public function actionUpdate($id)
{
    Url::remember('', 'actions-redirect');
    $user = $this->findModel($id);
    $user->scenario = 'update';

    $this->performAjaxValidation($user);

    if ($user->load(Yii::$app->request->post()) && $user->save()) {
        Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Account details have been updated'));
        return $this->refresh();
    }

    return $this->render('_account', [
        'user'    => $user,
    ]);
}
//--------------------------
在我的视图文件中:

<?= $form->field($user, 'role')->dropDownList(User::getSystemRoles(), ['class' => 'form-control col-sm-2']); ?>

你知道吗?你能帮帮我吗

问候
Wael

setter方法不会被load方法调用,它就是不这样工作。您创建表的方式似乎是一种多对多关系,因此您需要在视图中执行以下操作:

<?= $form->field($user, 'role_id')->dropDownList(User::getSystemRoles(), ['class' => 'form-control col-sm-2']); ?>
然后在afterSave中:

public function afterSave($insert, $changedAttributes) {
    if($this->role_id !== null)
    {
        // Save role in the DB
    }
    parent::afterSave($insert, $changedAttributes);
}
为了完成,我们在使用更新表单时获得了正确的角色,我们在afterFind中执行以下操作:

public function afterFind()
{
    $this->role_id = $this->role;
    parent::afterFind();
}

谢谢你的回答,但我没有真正解决我的问题。。我发现我应该对scenarios()函数进行过度编写并编辑更新。。。所以我添加了$newScenario=['update'=>['username'、'email'、'password'、'userrole'];然后我发现我不应该为我的变量指定用户名(角色),因为它可能是为Yii保留的。不过还是要谢谢你的帮助。
public function afterFind()
{
    $this->role_id = $this->role;
    parent::afterFind();
}