In this recipe we will take a look at the graphical tool Gii. With the Gii tool, we can generate the following:
Controller
CRUD
Form
Model
Module
Set up a Yii environment and create a new web application. Set up the database connection for your database server.
We first need to create a database table.
Create a database table user with the following code. This will hold all the necessary information for user registration.
CREATE TABLE IF NOT EXISTS 'user' ( 'id' int(11) NOT NULL AUTO_INCREMENT, 'first_name' varchar(50) NOT NULL, 'last_name' varchar(50) NOT NULL, 'gender' enum('male','female') NOT NULL, 'dob' date NOT NULL, 'address_1' varchar(255) NOT NULL, 'address_2' varchar(255) DEFAULT NULL, 'city' varchar(50) NOT NULL, 'state' varchar(50) NOT NULL, 'country' varchar(50) NOT NULL, 'phone_number_1' varchar(50) NOT NULL, 'phone_number_2' varchar(50) DEFAULT NULL, 'email_1' varchar(255) NOT NULL, 'email_2' varchar(255) DEFAULT NULL, 'created' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 'modified' timestamp NOT NULL DEFAULT '0000-00-0000:00:00', PRIMARY KEY ('id') ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Next, enable the Gii tool. The Gii tool is related to the database system of our application. So, for security reasons this tool is disabled by default. We need to enable it from
main.php
.'modules'=>array( // uncomment the following to enable the Gii tool /* 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'root', // If removed, Gii defaults to localhost only. Editcarefully to taste. 'ipFilters'=>array('127.0.0.1','::1'), ), */ ),
Uncomment the block to enable the Gii tool and set your password in place, and then you are done. The
ipFilters
line is used to restrict the access of the Gii tool to certain IP addresses only; in this case, it'slocalhost (127.0.0.1 OR ::1)
.We need to create models and controllers. To log in to the Gii tool, enter
http://localhost/sampleapp/index.php?r=gii
in your address bar. You will be provided with the login screen. Enter your password to log in.
After you log in, you can see all the tasks that can be done with the help of the Gii tool, as shown in the following screenshot:
The tasks shown in the previous screenshot are explained here:
Controller Generator: It allows you to quickly generate a new controller class with one or more actions and the respective views. The default base class used is
Controller
.CRUD Generator: It generates a controller and views that implement CRUD operations for a specified model.
Form Generator: It can be used to generate a view with form elements for a specified model.
Model Generator: It generates a model class for a specified database table. The base class used is
CActiveRecord
.Module Generator: It provides a base code required by the Yii module
All these generators use the templates under the gii/generator
folder. You can always customize them or use your own templates to modify the code according to your requirements.
Let's generate a model class for our table user as shown in the following screenshot:
Fill in the Table Name field and the Model Class field will be automatically populated; change it if required. Now click on Preview. If the database connection is properly set and Yii finds the table user, it will create the preview of a model file.
Click on the filename to preview the file or click on the Generate button to generate the model class. The new class will be stored in the protected/models
folder.
The model class has some automatically generated functions such as tableName()
, which returns the name of the table. The function rules()
is used to specify the validation rules, the function relations()
is used to specify the relations between two or more model classes, the function attributeLabels
specifies the names for the attribute/columns in the database table, and the function search()
sets the criteria for searching through the table data.
In the same way, we can generate the CRUD (Create Retrieve Update Delete) operations for the model user. Type the name of the model class (User
) and click on the Preview button. A list of files to be created will appear, as shown in the following screenshot:
Click on the Generate button to generate files. UserController
will be generated under protected/controllers
and you can view the files under protected/view/user
.
The generated controller contains some important functions, such as accessRules()
, which is used to specify access control (user-level access) for each action in this controller, and filters()
, which specifies the code to be executed before and/or after action execution. filters()
can include access-control filters, performance filters, and other user-defined filters.
To know more about filters()
, visit the website http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter.