Create, Read, Update and Delete (CRUD) are the important functions to learn any web framework. In this tutorial, I will show you how to do CRUD operations with Yii framework.
I assume you know, how to configure and use GiiModule in Yii framework. We will use this module to learn CRUD operations. If you don’t know about Gii, read previous tutorial.
I have set my Gii password to Password. So let’s start now.
Create a Yii project with yiic application called yiicrud, in your htdocs folder.
Uncomment the code segments to enable Gii tool and URLs in path format, from htdocs>yiicrud>protected>config>main.php file.
If everything is OK, you will find the following screen in http://localhost:8080/yiicrud/index.php/gii location.
Create a database from the following code segment.
-- -- Database: yiicrud -- CREATE DATABASE yiicrud; -- -------------------------------------------------------- -- -- Select: yiicrud -- USE yiicrud; -- -- Table structure for table products -- CREATE TABLE IF NOT EXISTS products ( id int(11) NOT NULL AUTO_INCREMENT, ssid varchar(10) NOT NULL, name varchar(50) NOT NULL, price float NOT NULL, size varchar(100) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
This code will create a database called yiicrud and a table called products. The products table contains 5 fields –
id – ID no of a product
ssid – Service Set Identification number
name – Name of the product
price – Product Price
size – Size of the product
Now we need to configure our application to connect the database. Open htdocs>yiicrud>protected>config>main.php with your editor. In this file, by default SQLite database configuration is enabled. We will disable it and enable MySQL configuration code. Find the following code segment and uncomment it. Rewrite the connection string; give the user name and password.
'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=yiicrud', 'emulatePrepare' => true, 'username' => 'root', 'password' => '', 'charset' => 'utf8', ),
And comment the SQLite configuration code segment.
/* 'db'=>array( 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db', ), */
The code will be look like as follows –
Now we are ready to start our CRUD operations.
We already know about the MVC structure. In this structure, Model works for all kinds of communication with database, View is for presenting our data and Controller is responsible for the interaction between Model and View.
We will create a Model class first, then we will use yii CRUD generator for our purpose.
Login to the GiiModule from http://localhost:8080/yiicrud/index.php/gii. Click on the Model Generator link.
It will open Model Generator page. Fill the Table Name field with our database table name. Give a Model Class name for our model, which will interact with database. Then click Preview button.
Table Name: products
Model Class: Products
Then click Generate button. It will generate a Products.php file in our htdocs>yiicrud>protected>models folder.
Now toggle back to the yii code generator window from the browser. Click on the Crud Generator link.
It will open Crud Generator page. Fill the Model Class field with our model class name. Controller ID will be populated automatically. For convenience, keep controller id as default.
Model Class: Products
Controller ID: products
Now click on Preview button. It will show, the list of files created in the controllers and view folder.
Now, click on Generate button to physically create these files. Immediately all the necessary files will be created in controllers and views folders. And try it now link will be show off. Click on that link. It will open a new window as follows.
We will start our CRUD operations with this page.
C for Create or insert
Click Create Products link to start our insert option.
It will take us to the login page. Fill the fields with –
Click on Login button.
A new page will appear where we can input our all database fields. Fill the information fields and click on Create button.
One new page will come with one inserted product. You can do this insertion by clicking on Create Products link again and again.
Hurrah.. We have done with create.
It will open a page with all lists of products. As, we have only one product item, one product has displayed.
Hmm.. Read complete as well.
U for Update
Go to http://localhost:8080/yiicrud/index.php/products/1. It will display the product details, which has the id no 1. Click on the Update Products link.
This will open a new page to update information. Let’s say we do not have Medium size T-Shirt. So, delete Medium. And click Save.
Walla.. We have done Update.
D for Delete
Again, go to http://localhost:8080/yiicrud/index.php/products/1. Click on Delete Products link.
It will ask your permission to delete the data. Click OK to delete.
Yahoo… we have done CRUD.
I like to show you little bit more. This is a bonus part. Let’s create 5 products using Create Products option, as the way I have shown you. After creating all these items, go to http://localhost:8080/yiicrud/index.php/products/index. The following page will appear. Click Manage Products.
A new page will open.
Let’s say, we want to know all the products, which has small size. Write small in the size search field. It will show 4 products. Because, one product does not have small size.
In the title, I mentioned this tutorial is for CRUD. But, I have shown you how to be SCRUD. Don’t be sap SCRUD means, Search Create Read Update and Delete.
Happy Coding 🙂