Yii2唯一验证器加入错误

时间:2018-07-09 10:15:42

标签: yii2

我有一个用户模型。保存用户时,我想确保用户名是唯一的,因此我有一个规则[['username'],'unique']。用户属于办公室,属于实践,属于组织。因此,我有以下查询:

class UserQuery extends \yii\db\ActiveQuery
{
    public function init()
    {
        $this->joinWith('office');
        parent::init();
    }

}

class OfficeQuery extends \yii\db\ActiveQuery
{
    public function init()
    {
        $this->joinWith('practice');
        parent::init();
    }

}
class PracticeQuery extends \yii\db\ActiveQuery
{
    public function init()
    {
        $this->joinWith('org');
        if (!Yii::$app->user->can('Corporate') and isset(Yii::$app->session['practice_id'])) $this->andWhere(['practice.id'=>Yii::$app->session['practice_id']]);
        parent::init();
    }

}

class OrgQuery extends \yii\db\ActiveQuery
{
    public function init()
    {
        if (!Yii::$app->user->can('Xpress') and isset(Yii::$app->session['org_id'])) $this->andWhere(['org.id'=>Yii::$app->session['org_id']]);
        parent::init();
    }

}

但是,当我尝试保存用户模型时,在第494行的ActiveRelationTrait中的office_id上​​收到未定义的索引异常。在用户模型中确实有一个office_id值。

有什么想法吗?

用户模型中的规则

public function rules()
{
    return [
        [['gender', 'phone1_type', 'phone2_type','birth_date'], 'string'],
        [['gender','phone1_type','phone2_type'],'default'],
        [['opt_out_date', 'last_login', 'created_at', 'updated_at'], 'safe'],
        [['bounce_count', 'status_id', 'sort', 'image_id', 'type_id', 'office_id', 'practice_id', 'org_id'], 'integer'],
        [['lastname', 'firstname', 'auth_key'], 'string', 'max' => 32],
        [['nickname', 'middlename', 'phone1', 'phone2'], 'string', 'max' => 16],
        [['username', 'password_hash', 'addr1', 'addr2', 'position', 'permission'], 'string', 'max' => 64],
        [['prefix'], 'string', 'max' => 8],
        [['suffix'], 'string', 'max' => 24],
        [['email', 'work', 'work_url'], 'string', 'max' => 255],
        [['opt_out_reason', 'work_title'], 'string', 'max' => 128],
        [['zipcode'], 'string', 'max' => 10],
        [['username'], 'unique'],
        [['username','phone1_type','phone2_type'],'default'],
        [['email'],'required', 'on'=>'newUser'],
        [['org_id', 'practice_id', 'office_id', 'permission', 'email','username', 'position'],'required', 'on'=>'newUser'],
        ['userImages','safe'],
        ['tab','string'],
        ['relationship','integer'],
        ['responsible','boolean'],
        [['zipcode'], 'exist', 'skipOnError' => true, 'targetClass' => \app\models\Zip::className(), 'targetAttribute' => ['zipcode' => 'zipcode']],
        [['lastname','firstname'],'required'],
        [['responsible','gender','birth_date','zipcode'],'required','on'=>['patient']],
        ['relationship','required','on'=>['patientParty']],
    ];
}

保存操作之前和之后

public function beforeSave($insert)
{
    if (parent::beforeSave($insert)) {
        foreach(self::$dateFields as $date) if ($this->$date) $this->$date = date('Y-m-d',strtotime($this->$date)); else $this->$date = null;
        if (!$this->zipcode) $this->zipcode = null;
        if ($this->password) {
            $this->password_hash = Yii::$app->security->generatePasswordHash($this->password);
            $this->auth_key = Yii::$app->security->generateRandomString();
        }
        if (!$this->image_id and $this->images) $this->image_id = $this->images[0]->id;
        return true;
    } else {
        return false;
    }
}

public function afterSave($insert, $changedAttributes)
{
    $auth = Yii::$app->authManager;
    $auth->revokeAll($this->id);
    $role = $auth->getRole($this->permission);
    if ($role) $auth->assign($role,$this->id);
    parent::afterSave($insert, $changedAttributes);
}

1 个答案:

答案 0 :(得分:-1)

您可以在模型中定义属性(变量)

public $office_id;