I can't sort out why setting default value doesn't work for me.
Here is my controller:
class QueryController extends ActiveController
{
public $modelClass = 'app\models\Query';
public $createScenario = 'restCreate';
public $updateScenario = 'restUpdate';
I don't override the default createAction
method.
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 entry via a Rest controller, it should set isApi = true
by default, but it doesn't work in a such way. How can I assign a default model values depending on scenario?
Yii uses only fields from current scenario. By default it sets in \yii\base\Model::SCENARIO_DEFAULT
(default
). Default scenario contains all fields from rules without on
attribute.
If you want to use custom scenario, you must set it. You can do this by two ways:
By constructor.$model = new Query(['scenario'=>'restCreate']);
By setter.$model = new Query();
$mosel->setScenario('restCreate');
See this link for more information about scenarios.
P.S. Good practice is using constants as scenario names. It help you make fewer mistakes in names of scenarios. For example:
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With