The Yii Framework offers what is called Database Migrations.
Database Migrations is a term used whenever there are any database changes that occur. This could be any database changes from table creations, additional fields, dropping fields to adding keys.
Database Migrations are especially helpful whenever people work in a team. Most developers won’t have the time to inform all the other developers or some might not be very attentive to any database changes. Having Database Migrations would be very helpful.
To start, go to you protected and create a migrations folder if it isn’t already there.
Now, you are ready to create migration files:
yiic migrate create migration_name
Migration names aren’t unique like in Ruby On Rails. You would be able to create multiple migrations with the same name.
Create Table Migration
Creating a table in Yii is done using the migrate command. Below is a run through of table creation:
php yiic.php migrate create create_products_table
Yii Migration Tool v1.0 (based on Yii v1.1.15)
Create new migration '/Library/WebServer/Documents/dealsite/protected/migrations/m140810_064319_create_products_table.php'? (yes|no) [no]:yes
To create a table, you will need the following function:
'id' => 'pk',
'name' => 'varchar NOT NULL'
Running the migration
Running the migration would be done by just one simple command:
php yiic.php migrate
Yii Migration Tool v1.0 (based on Yii v1.1.15)
Creating migration history table "tbl_migration"...done.
Total 1 new migration to be applied:
Apply the above migration? (yes|no) [no]:yes
*** applying m140810_060412_create_products_table
> create table deals ... done (time: 0.003s)
*** applied m140810_060412_create_products_table (time: 0.036s)
Migrated up successfully.
Aside from creating the table, this will create the tbl_migrations table if there isn’t one. This will also insert any information regarding the migration as records inside it.
Error: The migration directory does not exist: application.migrations
This commonly occurs whenever we try to use the yiic of the framework folder. Remember the one we used to create the application?
To fix this, we should use the yiic file inside the protected folder:
Successfully running migration commands should output the following:
php yiic.php migrate create create_deals_table
Yii Migration Tool v1.0 (based on Yii v1.1.15)
Create new migration '/Library/WebServer/Documents/webapplication/protected/migrations/m140810_060412_create_products_table.php'? (yes|no) [no]:yes
New migration created successfully.
Things to Remember
- You need to execute the yiic inside the protected directory to use on unix machines
- There should be an yiic.bat inside the protected directory if you are on a windows machine
- The migrations directory inside the protected directory must be available and is writable
As PHP Developers we want to be able to write applications as fast as we can and in the easiest way possible.
PHP is no exception. We have CakePHP, CodeIgniter, Laravel, FuelPHP and many many more. This book is about the Yii Framework, a high-performance component-based web framework designed for developing web 2.0 applications.
What can the Yii Framework do for you?
The Yii Framework allows developers make use of code that is well-tested by hundreds and probably thousands of developers in real-world applications. Most developers won’t have the need to write code because a big percentage of the functionalities that developers would be needing would already be available in the framework.
The Yii Framework has been tested by hundreds and a lot of things have been developed in a way to make applications developed with Yii secure.
Write code for you
The Yii Framework comes with built-in code generators to make life easier. CRUD can readily be generated with information provided. Ruby on Rails is known for its ability to create scaffolds.
As with other frameworks like CakePHP and Laravel, the Yii Framework makes use of its code generators to create Controllers, Models and many more components.
Not all kinds of libraries and classes will be included in the Yii Framework. Email classes and Image manipulation libraries are some examples of classes and libraries that aren’t included. However, we could easily include these within the Yii Framework as extensions.
Use of GridViews
The above items can be said with any of the other frameworks. Most frameworks helps in securing your application, are stable enough and allows code reuse. The Yii Framework, however, allows you to use its built-in GridViews. It allows you to generate a sortable, paginated table ala datatable
We all want the best. Developers like to work with the best. This has plagued thousands of developers around the world. Sometimes this has caused analysis paralysis.
In the previous chapter, we discussed that there are other PHP frameworks out there. Yii wasn’t the first PHP framework.
One of the earliest frameworks along with Symfony. CakePHP adheres to a convention over configuration approach just as Ruby on Rails. The convention over configuration approach basically means that although conventions within the framework are overridable, these conventions preferably should be followed.
CakePHP is stricter when it comes to conventions than say CodeIgniter or the Yii framework. Depending on the developer, stricter conventions is a big plus or minus.
CodeIgniter is a lightweight framework. It has come to be one of the fastest framework and has always been tested against newer frameworks because of its speed. CodeIgniter has less conventions and configurations than CakePHP or Yii. It is a preferable framework if you are new to PHP and frameworks altogether because of its extremely useful documentation.
Kohana started as the PHP5 version of CodeIgniter. It’s definitely slower than CodeIgniter but makes use of better OOP principles than CodeIgniter.
Kohana introduced the concept of HMVC, a variation of Model–view–controller (MVC) and Cascading File System.
The Cascading File System is a very interesting concept where in you can override configuration and logic by creating files higher up the hierarchical ladder. In Kohana, the hierarchical ladder is:
- Application Path – Your main application files
- Modules Path – Module files
- System Path – Kohana files
What Framework should you choose
There is no best framework. You can see thousands of comparisons between microframeworks to the full-fledged battery-included frameworks:
The truth is, none of these matter since you have to choose what framework you are comfortable with. Some frameworks like the Yii Framework has jQuery built-in. Some may consider that a plus or a minus. Some frameworks like Silex have very few things and you would need to create or include what you need for your projects.
As you can see, installing the Yii Framework is very easy. Now let’s take a look at the files we have.
The initial files and folders we can see are:
- protected — This holds your application files. You will spend most of your time working on files here.
- themes — Yii comes with a themes feature. Theme files will be located here.
- index.php — The boostrap file or the front controller. This file will handle all requests.
The index.php file is the most important file. It acts as the front controller which will initially handle inclusion of the other files. The protected folder is where most of your development will be.
There are more file and folders. If you opened the protected files, you will be able to see a lot more folders which will seem familiar if you worked with other PHP frameworks.
- config — Your application config files will stored here
- controllers — Controllers will be here
- data — If using SQLite, your database file will be stored here.
- extensions — Third-party extensions, not included in the core, will be stored here.
- migrations — Database migrations for database changes will be included here.
- models — This folder would include models
- tests — Unit tests will be stored here
- views — This folder contains your application views
The Main Config File
The config folder includes the main.php file which includes most configuration. Let’s explore the many configurations present:
The Yii Framework follows the MVC software design pattern. Programming in the Model View Controller pattern separates your application into three major parts.
The Model layer represents the part of your application that implements business logic. It is also usually taken as representation data by many primarily databases, but also XML, JSON and many others. Most interactions with data is carried here like processing, validating and other tasks.
The View Layer represents the part of your application that is viewable by your users. In the view, information is presented to the user via text, images, and forms usually in HTML format. Views, ideally should not have programming logic.
The Controller Layer accepts requests. It also is responsible for presenting responses back to the user with the help of the Model and View layers.
Read more about Yii Framework Controllers
Why use MVC in the first place? The Model View Controller is a very well tested and proven architectural design pattern that makes applications built on it become more maintainable and modular. Separating concerns into models, views and controllers make development a breeze. Adding functionalities are easier.
This makes for the Yii Framework become a rapid application development PHP framework.