Thursday 3 April 2014

How to develop CRUD apps using YII PHP Framework

YII has taken the PHP web development market like a storm. As we are into YII framework development from quite some time, we get a lot of queries from people about how can we develop CRUD apps using YII.  Today, we have tried to show how to do it in just few minutes. In order to development CRUD applications with YII, you should have Apache, PHP (5.1 or upper) and MySQL installed on your system. In case you don’t have these, we would recommend installing it. Now, log on to the official website of YII and download the latest version available there. The latest version of YII as of now is 1.1.13.

Now, you will have to extract the ZIP file to get the folder yii-1.1.13.e9e4a0 (this version identifier may differ depending upon the version you have downloaded) and rename it to yii, then move it to your web-accessible root directory. Here, it is C:\wamp\www so the location to the framework files will be C:\wamp\www\yii. Let’s call it <YiiRoot> throughout the write-up for better understanding.

Moving on, you should check which YII features will be supported by your system. You can go to http://localhost/yii/requirements/ in your browser and you can see the requirement details of the new framework. As we will be working with a MySQL database, please enable the MYSQL PDO extension.

Now, what I want you to understand here is every web app have a predefined directory structure. So YII web apps also need to maintain a hierarchical structure inside the web root. Now, to create a skeleton app with a suitable directory structure, we should use YII’d command line tool yii. Go to the web root and type the following:
<YiiRoot>frameworkyiic webapp yiitest

This will create a skeleton app called yii test with the minimum files required. Here, you will find index.php which will serve as an entry script; all it does is it accepts user requests and decides which controller should handle the request.

Here, we will develop a simple app where one can perform CRUD operations (create, retrieve, update and delete) on a blog post.

Step 1
First of all, create a MySQL database named with yiitest(Database name) and inside that database, create a table named posts. Majorly, the table will have just 3columns: id, title and content.
CREATE TABLE posts (
    id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(100),
    content TEXT
)

Now, open up your application’s config file (protected/config/main.php) and uncomment the following:
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive,
'emulatePrepare' => true,
'username' => 'root',                        //username of your database
'password' => '',
'charset' => 'utf8',
)

Step 2
Every database table in YII should have a corresponding model class of type CActiveRecord. In this way, we don’t have to deal with the database tables directly. So, we can keep on working with model objects that correspond to different rows of a table.

Now, in order to generate the model quickly, you can use YII’s web-based tool gii. You can use this tool to generate models, controllers, and forms for CRUD operations. To make a use of gii in your project, you will have to find the following in your application’s config file and uncomment it and then can add a password.
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>your password to access gii,
'ipFilters'=>array('127.0.0.1','::1'),
)

Now, you can access gii with the following URL: http://localhost/yiitest/index.php?r=gii. In case you’re using user friendly URLs, the URL is: http://localhost/yiitest/gii.

Step 3
Now click on CRUD Generator. Enter the model name as “Post”. The controller ID will be auto populated as “post”. This means a new controller will be generated under the name of PostController.php.
Now click on the CRUD Generator and enter the model name as “Post”. Here, the ID of the controller will be auto populated as “post”. This means that a new controller will be generated with the name of PostController.php.
Now, click on “Generate”. Hence, the controller necessary for the CRUD operations will be generated.

As you can see here, it is really very easy to develop CRUD apps by using YII framework. YII is very powerful and make a lot of things quite easier for you. With skilled YII developers, YiiPro is leading the world of Yii php framework development. If you have any queries or ideas, please feel free to discuss it.  

10 comments:

  1. Developing CRUD applications just became easy! We are definitely going to use YII framework for our next CRUD app. Thanks a lot for sharing this fruitful information step by step.

    ReplyDelete
  2. Replies
    1. Hi karnwal,friend could u send. The form to start working online with your company

      Delete
  3. Nice information about Web development services dear... keep it up

    Website development Company

    ReplyDelete
  4. Thanks to the author. I am a new at programming and this article imparted good knowledge. i am eagerly waiting for more articles.

    ReplyDelete
    Replies
    1. Hi Yamini,
      I'm very glad to hear you that this post is helpful for you.
      Thanks for dropping by and dropping your valuable feedback here.

      Delete
  5. I really appreciate you for all the valuable information that you are providing us through your blog.

    Hire Ruby on Rail Developer

    ReplyDelete
  6. Hi optisol biz,
    you are welcome to my blog!
    thanks for stopping by and dropping your valuable comment.

    ReplyDelete