PHP Professional এবং Zend Certified PHP Engineer (ZCPE ) Course এ সীমিত সংখ্যক আসন বাকি আছে। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করার জন্য অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল নম্বর : ০১৭২২ ৮১ ৭৫ ৯১

CRUD with Yii Framework

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.

Step 1
Uncomment the code segments to enable Gii tool and URLs in path format, from htdocs>yiicrud>protected>config>main.php file.

Enagle Gii and URLs path format

If everything is OK, you will find the following screen in http://localhost:8080/yiicrud/index.php/gii location.

localhost gii location

Step 2
Create a database from the following code segment.

-- Database: yiicrud
-- --------------------------------------------------------

-- Select: yiicrud
USE yiicrud;

-- Table structure for table products
  ssid varchar(10) NOT NULL,
  name varchar(50) NOT NULL,
  price float NOT NULL,
  size varchar(100) NOT NULL,

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.

    'connectionString' => 'mysql:host=localhost;dbname=yiicrud',
    'emulatePrepare' => true,
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',

And comment the SQLite configuration code segment.

    'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

The code will be look like as follows –

Configure database for CRUD

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.

Gii code generator for model

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

Model Generator window

Then click Generate button. It will generate a Products.php file in our htdocs>yiicrud>protected>models folder.

Products model class

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.

Crud Generator window

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.

product window

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 –

Username: admin
Password: admin

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.

Create products screen

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.

R for Read
To read the products click on the List Products link.
Read products

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.

Update products

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.

Delete product

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.

Five product list

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.

Manage product search

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 🙂

Hej, I’m from Bangladesh. Learning programming is one of the freaking decisions I have taken in my life. Because, it makes me and my life crazy. I have great weakness on open source technologies. Perhaps, that’s why I do not know any closed source language. I fall in love with programming, when I started my undergraduate in East West University. Till now, I can not live without it.

6 comments on “CRUD with Yii Framework

Leave a Reply

Your email address will not be published. Required fields are marked *