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’
Under appname/protected/config/main.php db array should look like below example
1 2 3 4 5 6 7 8 9 |
'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=jlool', 'emulatePrepare' => true, 'username' => 'my_db_user', //or root.. 'password' => 'mypass', //if no pass enter '' 'charset' => 'utf8', ), |
Under appname/protected/config/main.php uncomment gii module, should look like below example
1 2 3 4 5 6 7 8 9 10 |
... 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'mypassword', // If removed, Gii defaults to localhost only. Edit carefully to taste. 'ipFilters'=>array('127.0.0.1','::1'), ), ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
CREATE TABLE JUser( userid int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, username varchar(40) DEFAULT NULL, email varchar(40) DEFAULT NULL ) CREATE TABLE JProfile( profileid int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(40) DEFAULT NULL, surname varchar(40) DEFAULT NULL, fk_userid int(11) NOT NULL, FOREIGN KEY (fk_userid) references JUser(userid) ) |
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
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