在保存Yii2休息之前更改变量

时间:2014-12-21 00:51:36

标签: rest yii2

通过其余的api添加条目时如何在保存值之前更改变量?

以下是控制器代码的一部分:

class RestusersController extends ActiveController
{
    public $modelClass = 'app\models\User';
    public function actions()
    {
        $actions = parent::actions();

        $actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];

        return $actions;
    }

    public function prepareDataProvider()
    {
        return new ActiveDataProvider([
            'query' => User::find()->where(['status_id'=>'1']),
            'pagination' => false,
        ]);
    }
}

1 个答案:

答案 0 :(得分:0)

例如更改变量$ this-> status_id = 1;

public function beforeSave($insert)
{
    $this->status_id = 1;
    return parent::beforeSave($insert);
}