Working with JSON
JSON is a very simple, compact, and therefore widely used format for AJAX application's data exchange. Yii has a few handy ways to work with it. Therefore, let's create a simple application that will show a news list and update it every two seconds.
Getting ready
Create a new application by using the
yiic webapptool.Create and set up a new database.
Add a table named
newswith at leastid,created_on, andtitlefields, as shown in the following code:CREATE TABLE `news` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT,`created_on` int(11) unsigned NOT NULL,`title` varchar(255) NOT NULL,PRIMARY KEY (`id`))
Generate a
Newsmodel using Gii.
How to do it...
Create a new controller named
protected/controllers/NewsController.phpas follows:<?php class NewsController extends Controller { public function filters() { return array( 'ajaxOnly + data', ); } public function actionIndex() { $this->render('index'); } public function actionData...