Friday, September 17, 2010

Yii - data grid view with some customization of cell content format

Lets start by creating a simple table

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(128) NOT NULL,
  `activationKey` varchar(128) NOT NULL DEFAULT '',
  `createtime` int(10) NOT NULL DEFAULT '0',
  `lastvisit` int(10) NOT NULL DEFAULT '0',
  `superuser` int(1) NOT NULL DEFAULT '0',
  `status` int(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  KEY `status` (`status`),
  KEY `superuser` (`superuser`)

Configure the database connection as shown in one of the earlier posts.

Generate the corresponding model for the table in your database by executing the following commands
>protected\yiic shell

>> model *

Now, I want to create a data grid view to show the details of the table.

To show a link to the new "users" page(with a data grid) on navigation bar, open the file ..\mytestapp\protected\views\layouts\main.php

and add the following entry, in the "mainmenu" div.

array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'Users', 'url'=>array('/site/users')),  //####### new entry
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
)); ?>

The site controller needs information about where to render the new "users" page.
This can be done by adding the following method in the ..\mytestapp\protected\controllers\SiteController.php file

public function actionUsers()
    $dataProvider=new CActiveDataProvider('Users');

As you would notice that these methods should begin with "action" prefix.

Create a new file "users.php" at this location

The contents of this file would look something like this-

$this->pageTitle=Yii::app()->name . ' - Users';


beginWidget('CActiveForm', array(
)); ?>

widget('zii.widgets.grid.CGridView', array(
    'value'=>'CHtml::encode(date(\'Y-m-d\', $data->createtime))'  //this will format the unix timestamp to a custom date format
     'value'=>'CHtml::encode(date(\'Y-m-d\', $data->lastvisit))'      // format the cell with date format
     'value'=>'CHtml::encode($data->status==1 ? \'Active\': \'Inactive\' )' 
// to render the gridview cell with a particular value
)); ?>
endWidget(); ?>

The page should be hosted at

depending on the url manager setting in the config file.

Thursday, September 9, 2010

Setup and debug an Yii app in Netbeans IDE

After using Eclipse for a while I switched to Netbeans IDE for Yii development. It seems faster and easier to setup. Following is my experience of setting up the development environment.

Create a project stub using the following command -
>> yii-1.1.3.r2247\framework\yiic webapp mytestapp

Open Netbeans IDE and proceed with the following steps -
1. Click on File-> New Project
    This will open up a dialog box. Select "PHP" under "Categories" and under "Projects" select "PHP Applications with Existing Sources".

    Click Next.
2. In the "Sources Folder", input the path of "mytestapp" folder on your machine.
    Assign a project name. "mytestapp" for my case.
    Select a PHP version.
    Click Next
3. The options in this form should be set up automatically like below.

    Click Finish
    This should set up your Yii project in Netbeans IDE.
4. Go to "Debug" menu on the top bar on Netbeans IDE. Select "Debug Project(mytestapp)" and you should be able to debug your project.

5. "Continue(F5)" option in Debug can be used to resume a stopped debug session.