Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Yii Project Blueprints
  • Table Of Contents Toc
Yii Project Blueprints

Yii Project Blueprints

By : Charles R. Portwood ll
3.5 (2)
close
close
Yii Project Blueprints

Yii Project Blueprints

3.5 (2)
By: Charles R. Portwood ll

Overview of this book

This book is for intermediate-to-advanced level Yii developers who want to master the Yii framework and develop real-world applications. You should have experience of working with Yii, PHP 5, HTML, CSS, and JavaScript.
Table of Contents (10 chapters)
close
close
9
Index

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.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Yii Project Blueprints
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon