我刚刚开始使用 Yii 2 框架进行开发,并安装了官方的高级应用程序。
用户表的迁移脚本将“10”定义为“状态”列的默认值。
'status' => $this->smallInteger()->notNull()->defaultValue(10),
我只是想知道他们为什么使用这个?
过去我分别使用布尔值(true/false)0/1(在 mssql 中存储为 smallint 或 bit)。
我刚刚开始使用 Yii 2 框架进行开发,并安装了官方的高级应用程序。
用户表的迁移脚本将“10”定义为“状态”列的默认值。
'status' => $this->smallInteger()->notNull()->defaultValue(10),
我只是想知道他们为什么使用这个?
过去我分别使用布尔值(true/false)0/1(在 mssql 中存储为 smallint 或 bit)。
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.