Yii Relational Databank Objects and Gii

Yii is great in creating objects/models for your tables and their relations.
Here I’ll show how you can generate objects/models with gii using a mysql databank.

Lets assume you have created the hello world project using ‘yiic’

Step 1 – Enable Databank Connection

Under appname/protected/config/main.php db array should look like below example

Step 2 – Enable Gii

Under appname/protected/config/main.php uncomment gii module, should look like below example

Step 3 – Create Tables in mysql

Step 4 – Generate objects in Gii

In your Browser visit http://localhost/appname/index.php?r=gii or http://localhost/index.php?r=gii if your app is in root folder

  1. Enter your password defined in main.php and to the left click the model generator
  2. After that fill the tablename and modelclass will be auto filled
  3. Finally press Preview and then Generate


Make sure the folder(s) under your yii app have write permissions or gii will fail in creating the model files
After generating the models you should restore the original permissions

Recent Posts