Take an example from the documentation :
namespace app\models; use yii\db\ActiveRecord; class User extends ActiveRecord { const SCENARIO_LOGIN = 'login'; const SCENARIO_REGISTER = 'register'; public function scenarios() { return [ self::SCENARIO_LOGIN => ['username', 'password'], self::SCENARIO_REGISTER => ['username', 'email', 'password'], ]; } } In the example above, the scripts are defined as constants:
const SCENARIO_LOGIN = 'login'; const SCENARIO_REGISTER = 'register'; We initialize them in one of the following ways:
// scenario is set as a property $model = new User; $model->scenario = User::SCENARIO_LOGIN; // scenario is set through configuration $model = new User(['scenario' => User::SCENARIO_LOGIN]); Question
Why not do so ...
Scenarios are defined as regular strings:
namespace app\models; use yii\db\ActiveRecord; class User extends ActiveRecord { public function scenarios() { return [ 'login' => ['username', 'password'], 'register' => ['username', 'email', 'password'], ]; } } We initialize them accordingly:
// scenario is set as a property $model = new User; $model->scenario = 'login'; // scenario is set through configuration $model = new User(['scenario' => 'login');