Gii (powerful web based code generator) to automate code creation for certain tasks. We can create models, controllers, generate CRUD operation, modules using Gii. For using Gii we need to enable it. From the Create your first application using Yii Framework we know how to create an new application on Yii. I will work on this application. From the file protected => config => main.php remove the comment sign to unable it.
// uncomment the following to enable the Gii tool
‘password’=>’Enter Your Password Here’,
// If removed, Gii defaults to localhost only. Edit carefully to taste.
At the password option I put a password that will use later to validate on Gii. Gii for developing purpose so it better we don’t use it or enable it on live/production server.
Configure the database:
‘connectionString’ => ‘mysql:host=localhost;dbname=testdrive’,
‘emulatePrepare’ => true,
‘username’ => ‘root’,
‘password’ => ”,
‘charset’ => ‘utf8′,
I will use mysql databse in my application so I uncomment the mysql connection option. And put my server details. I have two tables on the database here is the sql for those tables:
CREATE TABLE studentInfo
id INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(50) NOT NULL,
email VARCHAR(50) NULL,
address VARCHAR(200) NULL
To access the Gii we need to go this url:
This screen will ask to enter our password. Put the password that we have entered on the Gii setting. A new screen will show from this screen we can create controller, views, models etc.
For create a model click on the model generate link then do the following:
- Enter the table name into the table name field (Our is studentInfo)
- Write the model class name. Model class name will automatically assign
- Click the preview to preview the file a table will show with the model file links. If we click the link (Code File) it will show the file.
- Click on generate the model will generate for us.
We can found the file under protected => models => Studentinfo.php as per our model name.
Create the CRUD functionality:
CRUD stands for Create, Read, Update and Delete. In computer programming create, read, update and delete (CRUD) are the four basic functions of database. This will generate necessary view files and one controller file. For create the CRUD click on the Crud Generator. On the screen:
- In the Model Class put the model name (our is Studentinfo)
- Controller id will automatically assign
- Click the preview to preview the file a table will show with the controller file and view files link. If we click the link (Code File) it will show the file.
- Click on generate controller and views will generate for us.
We can’t create model for those tables which have composite primary keys. Yii implements two kinds of models: Form models and active records. We can’t create Form Models from Gii.
To access the application our link will like that:
or (based on urlManager)
A module is a self-contained unit that consists of models, views, controllers and other supporting components. Module is organized as a directory and its name serves as its unique ID. For creating module click on the Module Generate:
- Put the module name
- Press the Preview
- It will show a table and the Generate button press the generate button to create the module
I put the model name as mymodule, to use this model we need some configuration in our config file (protected => main => main.php).
We can create model/controllers/CRUD under modules as previously explain. Just we have to mention the path. Examples:
We can create Forms, Controllers using Gii. Form can be based on form model or active record model. Hope this tutorial will help them who are going to start work on Gii.
** I use Yii Framework version was v1.1.10
** I am using windows 7 ultimate, xampp 1.7 for developing, which in my C drive.
** All Code are tested in real application.