Run the Yii 2 migrate command on Mac to install and initialize the database:
You must have the PHP system environment variable before you can execute the Yii command line. Enter php yii in the terminal to list all available Yii commands.
Enter php yii migrate, which will automatically list available migrate files. There is only one file, which will automatically go to the/console/migrations/folder to find the migrate database migration file.
When we enter yes, this operation is executed. The migrate file is applied and the data table is created. As shown in the following figure:
Migrate migration File: migrations/m130524_201442_init.php. The code for this file is as follows:
The code is as follows: |
Copy code |
<? Php Use yii \ db \ Schema; Use yii \ db \ Migration; Class m130524_201442_init extends Migration { Public function up () { $ TableOptions = null; If ($ this-> db-> driverName = 'mysql '){ $ TableOptions = 'character SET utf8 COLLATE utf8_unicode_ci ENGINE = innodb '; } $ This-> createTable ('{{% user }}',[ 'Id' => Schema: TYPE_PK, 'Username' => Schema: TYPE_STRING. 'Not Null ', 'Auth _ key' => Schema: TYPE_STRING. '(32) NOT Null ', 'Password _ hash' => Schema: TYPE_STRING. 'NOT Null ', 'Password _ reset_token '=> Schema: TYPE_STRING, 'Email '=> Schema: TYPE_STRING.' NOT Null ', 'Status' => Schema: TYPE_SMALLINT. 'Not null default 10 ', 'Created _ at' => Schema: TYPE_INTEGER. 'Not Null ', 'Updated _ at' => Schema: TYPE_INTEGER. 'Not Null ', ], $ TableOptions ); } Public function down () { $ This-> dropTable ('{{% user }}'); } } |
It can be seen that both the up and down methods are required for the migrate file of yii. As the name suggests, one is installation, and the other is deletion of data tables.
A good development specification is to write the database structure into a migrate file, including initialization data, which is extremely convenient during Database deployment or migration, that is, it is troublesome and refreshing.