yiiyii2yii2-advanced-appyii-migrations

Yii2 why using status constant 10 instead of 1?


I'm just beginning developing with the Yii 2 Framework, and installed the officially advanced app .

The migration script of the user table defines '10' as the default value for the column 'status'.

'status' => $this->smallInteger()->notNull()->defaultValue(10),

I'm just wondering why they use this?

In the past I have used boolean (true/false) respectively 0/1 (stored as smallint or bit in mssql).


Solution

  • Take a look at contents of common\modules\User class. There are two constants for statuses:

    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    

    The boolean type is not used because it assumes usage of multiple statuses (more than two), not limited to only deleted and active (in this case we can simply have boolean column is_active or something like that).

    The 0 and 10 is used as kind of boundary values, to add other constants in between in the future. Also it's kind of extreme states, while other are intermediate.

    The actual values of constants can vary, the more important thing is once it's declared and some data already exists, you can't simply change it to another value without data migration.

    But if you are not satisfied with these values, you can change it to 0, 1 and add others as 2, 3 and so on.

    You can also completely remove it and make column boolean and rename it to is_active as I said before.

    Remember - it's only a template, you can change it to fit your needs.