Setting the default model value depending on the scenario

I cannot figure out why setting the default value for me does not work.

Here is my controller:

class QueryController extends ActiveController { public $modelClass = 'app\models\Query'; public $createScenario = 'restCreate'; public $updateScenario = 'restUpdate'; 

I do not override the default createAction .

Model:

  class Query extends ActiveRecord {public function rules() { return [ [['userId', 'name', 'created', 'isOneTime', 'isArchived', 'settings', 'engine'], 'required'], [['userId'], 'integer'], [['name', 'settings', 'schedule'], 'string'], [['created', 'lastUpdate'], 'safe'], [['isOneTime', 'isArchived', 'isApi', 'valid'], 'boolean'], [['state', 'engine'], 'string', 'max' => 160], [['isApi'], 'default', 'value'=> false], [['isApi'], 'default', 'value'=> true, 'on' => 'restCreate'] ]; } public function scenarios() { $scenarios = parent::scenarios(); $scenarios['restCreate'] = ['name', 'state', 'isApi', 'isOneTime', 'settings', 'schedule']; $scenarios['restUpdate'] = ['name', 'state', 'isOneTime', 'settings', 'schedule' ]; return $scenarios; } 

When I create a new record through the Rest controller, it should set isApi = true by default, but it does not work that way. How to assign default model values ​​depending on the scenario?

+6
source share
3 answers

You must change these rules:
[['isApi'], 'default', 'value'=> false]
[['isApi'], 'default', 'value'=> true, 'on' => 'restCreate']

You must do this because DefaultValueValidator uses only one time for one field. This validator is only executed if the field value is empty. There is such a check: \ yii \ validators \ DefaultValueValidator :: validateAttribute ()

The model runs the validators one by one. [['isApi'], 'default', 'value'=> false] sets the value for the false field. [['isApi'], 'default', 'value'=> true, 'on' => 'restCreate'] to see that the value is not empty and skipped.

+1
source

Yii uses only the fields from the current script. By default, it is set to \yii\base\Model::SCENARIO_DEFAULT ( default ). By default, the script contains all the fields from the rules without the on attribute.

If you want to use your own script, you must install it. You can do this in two ways:

  • By constructor.
    $model = new Query(['scenario'=>'restCreate']);

  • Using setter.
    $model = new Query();
    $mosel->setScenario('restCreate');

For more on scripting see this link .

PS Good practice is to use constants as script names. This will help you make fewer mistakes in the name of the scripts. For instance:

 class Query extends ActiveRecord { const SCENARIO_REST_CREATE = 'restCreate'; const SCENARIO_REST_UPDATE = 'restUpdate'; public function scenarios() { $scenarios = parent::scenarios(); $scenarios[static::SCENARIO_REST_CREATE ] = [/*...*/]; $scenarios[static::SCENARIO_REST_UPDATE ] = [/*...*/]; return $scenarios; } } 
+1
source

In your action:

 public function ActionCreate(){ $model->scenario = 'restCreate';// it will set your scenario first . //remaining code for write here } 
0
source

All Articles