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.

3 comments:

  1. Thanks, very nice & easy to learn .. :)

    ReplyDelete
  2. Hai,

    Is it possible to store into our live databases by yii. I mean i want to store from data dynamically into our live databases(based on ipaddress). and i have to write query like insert into or update directly not by Gii.

    So please forward if u have any queries or links to sk.ijaz@gmail.com.

    Thanks

    ReplyDelete
  3. Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. free nintendo eshop codes

    ReplyDelete

Yii Framework Tutorials