Find us on facebook

Oct 2, 2013

CRUD operations with Yii

CRUD means create, read, update and delete.

Step 1: Turn on the php pdo extension to use database feature.

Step 2: Create connection to the database. I'm using MySQL.

To use MySQL uncomment the following code which is in     D:/wamp/www/testyii/protected/config/main.php

'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testyii',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),

Step 3: Use Gii (Gii is a powerfull code generator)

To use Gii uncomment following code which is in D:/wamp/www/testyii/protected/config/main.php

'modules'=>array(

'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'testyii',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
),

),
Here 'password'=>'testyii' you can set any password you like. I'm using "testyii" as my password.

Step 4: Access the URL http://localhost/testyii/index.php?r=gii



Step 5: Go to Model Genertor.

Give table name and Model Class as below. It will Generate model class for User.


Step 6: Then go to Crud Genertor.



Add Model class same as before. For me it is  "User". Controller ID will be user.

Step 7: Then access URL http://localhost/testyii/index.php?r=user
You can add users, update and delete users. Also you can view user list without writing a single code.


No comments:

Post a Comment