Optimize Scenarios for yii2

Comparing #1 with #2

Revision #2 was created by AndroideLP AndroideLP on Apr 12, 2018, 1:23:10 PM.

update save error

Yii version

2.0

Tags

model, model validation, model,validation rules

Content

[...]
One way to avoid this disorder is to encapsulate the information defined for the scenarios and to have a single point of customization.

For this we need to create constants for each scenario, note: once you define a scenario, you will need to use scenarios for any database edition that uses this model.

**In model**

```php

class MyModel extends \yii\db\ActiveRecord
{
[...]
**In Controller**


```php

public function actionIndex()
{
[...]
if ($model->load(\Yii::$app->request->post())){
       // get all scenarios
 
        $allscenarios=$model->getCustomScenarios();
// force my columns if($model->save(true, $allscenarios[$this->scenario])){
//return true
}
}
}
```
[...]