যারা বাংলাদেশের ৩৬ তম Zend Certified Engineer এবং এই সাইট (w3programmers) এর স্বত্বাধিকারী এবং লেখক Masud Alam স্যার এর কাছে কাছে সপ্তাহে ২ দিন ক্লাস, ৬ দিন প্রাকটিস করতে চান তাদের জন্য আগামী ১ February 2018 (বৃহস্পতি এবং শুক্রবার সন্ধ্যা ৬:০০ টা রাত ৯:০০ টা) থেকে শুরু হতে যাচ্ছে ৫ মাস ব্যাপী Professional Web Design & Development With HTML, CSS, JAVASCRIPT, JQUERY, PHP & MySQL for Beginners কোর্স (এই কোর্স টি যারা ফ্রিল্যান্সিং এবং আউটসোর্সিং কাজে আগ্রহী বিশেষ ভাবে তাদের জন্য design করা হয়েছে। ), আগামী ৪ February 2018 ( রবি এবং মঙ্গলবার সন্ধ্যা ৬:০০ টা থেকে রাত ৯:০০ টা ) থেকে শুরু হতে যাচ্ছে ৪ মাস ব্যাপী Zend Certified PHP 7.1 Engineering কোর্স। যেটা Advance PHP কোর্স নামে পরিচিত । এবং ৫ February 2018 (সোম এবং বুধবার সন্ধ্যা ৬:০০ টা থেকে রাত ৯:০০ টা) থেকে শুরু হতে যাচ্ছে ৪ মাস ব্যাপী Laravel 5.5 Professional Beginning to Advance Course কোর্স ।এই কোর্স গুলোতে তিনি ছাত্রদের কে সপ্তাহে ২ দিন ৩ ঘন্টা করে ৬ ঘন্টা ক্লাস নিবেন , এবং শনি থেকে বৃহস্পতি সপ্তাহে ৬ দিন ই স্যার এর অফিসে বসে প্রাকটিস করার সুযোগ দিবেন ! এতে প্রাকটিস করা কালীন ছাত্র-ছাত্রীরা কোনো সমস্যায় পড়লে তাৎক্ষণিক স্যার এর সহযোগিতা পাবে . (উল্লেখ্য: Training গুলো স্যার তার techbeeo software company'r পাশাপাশি পরিচালনা করে থাকেন।) কোর্স গুলো সম্পর্কে বিস্তারিত জানতে পারবেন এই লিংকে : Course Details .কোর্স গুলোর প্রত্যেকটির ফী নির্ধারণ করা হয়েছে ৩০,০০০ টাকা। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করার জন্য অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল নম্বর : ০১৭২২ ৮১ ৭৫ ৯১

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.

Pre-requisite
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.

Preparation
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
--
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 –

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.

Bonus
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

Conclusion
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 *