yii2: The default scenario of Model can't be overridden.

What steps will reproduce the problem?

class MyForm extends Model
{
    const SCENARIO_DEFAULT = self::SCENARIO_CREATE;
    const SCENARIO_CREATE = 'create';
    const SCENARIO_UPDATE = 'update';
}

$form = new MyForm();
echo $form->scenario;

What is the expected result?

create

What do you get instead?

default

Additional info

I can’t write:

public function fields()
{
   // PHP Notice – yii\base\ErrorException Undefined index: default
   return $fields = $this->scenarios()[$this->scenario];
}

That why:

// \yii\base\Model
private $_scenario = self::SCENARIO_DEFAULT;

To fix it I can rewrite init() method - IMHO: not elegantly.

Q A
Yii version 2.0.9
PHP version > 7

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (10 by maintainers)

Most upvoted comments

If you’re just wanting to use the const SCENARIO_CREATE why not inherit it from SCENARIO_DEFAULT

class MyForm extends Model
{
    const SCENARIO_CREATE = self::SCENARIO_DEFAULT;
    const SCENARIO_UPDATE = 'update';

   public function scenarios()
    {
        return [
            self::SCENARIO_CREATE => ['name', 'imageFile'],
            self::SCENARIO_UPDATE => ['name', 'imageFile', 'clearImage'],
        ];
     }
}

I want to be able to define my scenarios once, and be able to set the default as needed.

Say, I have a ModelB that extends ModelA, the default scenario for ModelA is SCENARIO_DRAFT, but the default for ModelB should be SCENARIO_REFURBISHED. Currently this is not possible without redefining several scenario constants in ModelB — setting SCENARIO_REFURBISHED to SCENARIO_DEFAULT, and then setting SCENARIO_DRAFT to something and crossing my fingers, that ModelA uses static::SCENARIO_DRAFT everywhere. And that nothing uses scenario constants of the parent, ModelA, when interacting with the child, ModelB (which is a very common practice in OOP).

And what if ModelC exends from ModelB and needs to have yet another default scenario?

I can create the PR for this.

You could change scenario in init() or __construct().

@Alex-Code I can, but it’s litehack…like all of Yii2