In this post I will try to explain what is Gii tool, and how to use it to speed up your development.

You decided to develop your web application using Yii framework. You have already designed your database and you have created database tables and relations. This is a good starting point, but what is the next step.

The first thing you have to enable is Gii tool. Gii tool is separate module in Yii framework which enables your to automatically generate database models, CRUD operations, modules, and controllers.

Here is definition of every term:

Models – Model is a PHP class that maps data from a database into PHP objects. This enables you to perform select, insert, update and delete sql queries in object oriented way.

CRUD – Create Read Update Delete  – In few clicks you can create all this actions for every database table. Remember how much time you would spend doing this things, manually, and here you can do it in few clicks.

Modules – A module is a self-contained software unit that consists of models, views, controllers and other supporting components. In many aspects, a module resembles to an application.

To enable Gii module, open your main.php configuration file (protected/config/main.php), and uncomment the following lines:

In the code above you can define a Gii password, which you will need in order to access Gii functionalities.

If you are working on your local machine, then you can leave ipFilters field as is, but if you want to enable someone to access Gii on your server then you can define multiple IP addresses (‘192.168.1.2′,’192.168.1.2′), or IP address with wildcard to represent network segment (‘192.168.1.*’). If you want to disable ipFilter, then set this property to false.

To run Gii tool, run one of the following links (the exact format depends on Yii’s URL configuration and your localhost path):

  • http://localhost/?r=gii
  • http://localhost/index.php/gii
  • http://localhost/gii

When you start Gii tool, you should the following page should be displayed:

Gii start page

When you enter your Gii’s password, you will be redirected to the Gii’s dashboard:

Gii dashboard

To create a new model, just click Model Generator option, and new page will appear.

New model

In Table Name field enter a name of a database table, and click preview. Gii will show you which files is going to be created. Confirm by clicking on Generate button, and hoa, your first model file is generated.

Generated module class can be used to retreive data from database, or to store data to database. If you have generated User model, from the database table called user, then you can do something like this:

Hope I helped.

 

What is Gii and how to use itAmir DuranPHPProgrammingTutorialsUncategorizedGii,PHP,Yii framework
In this post I will try to explain what is Gii tool, and how to use it to speed up your development. You decided to develop your web application using Yii framework. You have already designed your database and you have created database tables and relations. This is a good...