Run the YII 2 MIGRATE command on MAC to operate the database.

Source: Internet
Author: User
Tags character set yii

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.