官术网_书友最值得收藏!

Creating the database with migrations

Now that our application can be bootstrapped, we can create our database. To do this, we are going to create a migration. Migrations are a feature of Yii that allow the creation and modification of your database to be a part of your application. Rather than creating schema modifications in pure SQL, we can use migrations to grow our database as a part of our application. In addition to acting as a revision system for our database schema, migrations also have the added benefit of allowing us to transmit our database with our application without having to worry about sharing data that would be stored in our database.

To create our database, open up your command-line interface of choice, navigate to your tasks directory, and run the following command:

$ php protected/yiic.php migrate create tasks

The yiic command will then prompt you to confirm the creation of the new migration:

Yii Migration Tool v1.0 (based on Yii v1.1.14)

Create new migration '/var/www/tasks/protected/migrations/m131213_013354_tasks.php'? (yes|no) [no]:yes
New migration created successfully.

Tip

To prevent naming conflicts with migrations, yiic will create the migration with the following naming structure: m<timestamp>_<name>. This has the added benefit of allowing us to sequentially apply or remove specific migrations based upon the order in which they were added. The exact name of your migration will be slightly different than the one listed in the preceding command.

After confirming the creation of the migration, a new file will be created in the protected/migrations folder of our application. Open up the file, and add the following to the up method:

$this->createTable('tasks', array(
   'id' => 'INTEGER PRIMARY KEY',
   'title' => 'TEXT',
   'data' => 'TEXT',
   'project_id' => 'INTEGER',
   'completed' => 'INTEGER',
   'due_date' => 'INTEGER',
   'created' => 'INTEGER',
   'updated' => 'INTEGER'
));

$this->createTable('projects', array(
   'id' => 'INTEGER PRIMARY KEY',
   'name' => 'TEXT',
   'completed' => 'INTEGER',
   'due_date' => 'INTEGER',
   'created' => 'INTEGER',
   'updated' => 'INTEGER'
));

Notice that our database structure matches the schema that we identified earlier in the chapter.

Next, replace the contents of the down method with instructions to drop the database table if we call migrate down from the yiic command. Have a look at the following code:

$this->dropTable('projects');
$this->dropTable('tasks');

Now that the migration has been created, run migrate up from the command line to create the database and apply our migration. Run the following commands:

$ php protected/yiic.php migrate up
Yii Migration Tool v1.0 (based on Yii v1.1.14)
Total 1 new migration to be applied:
 m131213_013354_tasks

Apply the above migration? (yes|no) [no]:yes
*** applying m131213_013354_tasks
*** applied m131213_013354_tasks (time: 0.009s)
Migrated up successfully.

Now, if you navigate to protected/data/, you will see a new file called tasks.db, the SQLite database that was created by our migrations.

Tip

Migration commands can be run non-interactively by appending --interactive=0 to the migrate command. This can be useful if you want to automate deployments of your code to remote systems or if you run your code through an automated testing service.

主站蜘蛛池模板: 拉孜县| 元江| 雷州市| 宕昌县| 温泉县| 江油市| 禹城市| 香格里拉县| 邓州市| 从化市| 靖边县| 五寨县| 电白县| 衢州市| 石楼县| 龙海市| 浦江县| 汉沽区| 纳雍县| 辽中县| 都安| 郧西县| 贵州省| 武强县| 驻马店市| 洪洞县| 吴江市| 琼海市| 积石山| 鲁山县| 阿拉善左旗| 那曲县| 裕民县| 平阳县| 菏泽市| 定边县| 湘阴县| 鄂州市| 沾益县| 墨竹工卡县| 和平区|