Wednesday, December 28, 2011

How to Use Model Generator (Gii Tool - Yii Code Generator)

Model Generator, generates a model class for the specified database table. In the application of Gii Tool - Model Generator you need a database to connect. Follow the steps below:

1. Create a simple MySQL database. For example I use yiicode as database name and user as table. See the attributes as follows:

Field Type Lenght/Values Exrta Primary
id int - auto_increment Yes
name varchar 60 - -
address varchar 200 - -
website varchar 100 - -
email varchar 50 - -

To make it, open http://localhost/xampp/, click on phpMyAdmin, click Databases, on the columns Create a new database typing demo, and click create. After that click on the SQL and create a user table by entering the following SQL command:
CREATE TABLE user
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(60) NOT NULL,
address VARCHAR(200) NOT NULL,
website VARCHAR(100) NOT NULL,
email VARCHAR(50) NOT NULL
);

2. After that you please login to Gii - http://localhost/webappname/index.php?r=gii/default/login.
Typing in the Table Name * is tbl_user and in the Model Class * is User


You can see PHP code by click Preview button and the models\User.php. This code will be automatically stored in the xampp \ htdocs \ yourappname \ protected \ models \ User.php.
After it, click the Generate button

If successful will appear in the following:
The code has been generated successfully.
Generating code using template “xampp\htdocs\yii\framework\gii\generators\model\templates\default”…
generated models\User.php
done!
That means you have successfully created a model to perform CRUD operations.
Yii Framework Tutorials

Saturday, December 10, 2011

Configuration Gii Tool (Yii Code Generator)

Gii (automatic code generation) is a Web-based code generator tool of Yii to develop a web application development productivity. Gii is implemented as a module and should be used in the Yii application. To use Gii, you have to do some Gii configuration first. The following configuration steps Gii (producer of automatic code - Code Generator):

  1. Have you open the protected / main.config.php
  2. Find the following code
    // uncomment the following to enable the Gii tool
     /*
     'gii'=>array(
      'class'=>'system.gii.GiiModule',
      'password'=>'admin',
       // If removed, Gii defaults to localhost only. Edit carefully to taste.
      'ipFilters'=>array('127.0.0.1','::1'),
     ),
     */
    ),
  3. Remove the tag comment / * and * /, so like this
    // enable Gii tool
     'gii'=>array(
      'class'=>'system.gii.GiiModule',
      'password'=>'Enter Your Password Here',
       // If removed, Gii defaults to localhost only. Edit carefully to taste.
      'ipFilters'=>array('127.0.0.1','::1'),
     ),
    ),
  4. Change Your password in Enter Your Password Here, for example 123456
    // enable Gii tool
     'gii'=>array(
      'class'=>'system.gii.GiiModule',
      'password'=>'123456',
       // If removed, Gii defaults to localhost only. Edit carefully to taste.
      'ipFilters'=>array('127.0.0.1','::1'),
     ),
    ),
  5. Now you have to login to Gii tool by open a web browser and type the http://localhost/yiicode/index.php?r=gii/default/login Changes yiicode with your application name.
    Before login Gii (Yii Code Generator)
    After login Gii (Yii Code Generator)
Nb: Gii is mainly provided as a development tool. Therefore, it should only be installed on a development machine. Because it can generate new PHP script files in the application, we should pay sufficient attention to its security measures (e.g. password, IP filters).