Yii 1.1 : DropDown, Pagination, GridView, DetailsView

on

The most amazing this I noticed in Yii 1.1 framework is there build in module gii. By this module, anyone can generate a CRUD (Create, read, update and delete). Although it create most of the components like model, view, and controller but also we need to do some modification for better user experiences. Here I will discuss about those things:

DropDown with model data:

Normally when i generate any CRUD by gii in Yii, it just shows the text fields. If anyone want to change it to drop down list, and want to fetch data from another table, then we have to do this:

<?php 
echo $form->dropDownList($model,’user_type_id’,
CHtml::listData(UserType::model()->findAll(), ‘id’, ‘type’),
array(’empty’=>’Select Type’));
?>

Here:

  • $form->dropDownList is a widget.
  • UserType is a model.
  • CHtml::listData is a function which will generate a select list from model data.
  • Here in listData function 2nd parameter is $valueField and 3rd parameter is $textField, which are equivalent to  HTMLselect options value and name.

Pagination with model:

When I generate CURD, the default pagination size is 10. If I want to change this pagination, there is a way for changing this from corresponding model. In model, we have to change the search function.

public function search()

By default this search function return a CActiveDataProvider object like this:

return new CActiveDataProvider($this, array(
‘criteria’=>$criteria,
));

We have to change the this CActiveDataProvider object for pagination.

return new CActiveDataProvider($this, array(
‘criteria’=>$criteria,
‘pagination’ => array(
                        ‘pageSize’ => 20,
                    ),
));

Get name from related data model and display in CGridView:

Here i have two model one is Main Category and another is Sub Category.

I have one to many relation between these two tables in database.

I have main category id in sub category table.

At first i generate code by gii.

Then if i want to show corresponding main category name in sub category CGridView:

<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'columns'=>array(
        'id',
            array(
                'name'=>'main_id',
                'value'=>'CategoryGroupMain::model()->catMainGetName($data->main_id)',
                ),            
        'name',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); 
?>

And in CategoryGroupMain model class i have to implement this function catMainGetName.

<?php
public function catMainGetName($pk)
    {
        $categoryGroupMainOb = CategoryGroupMain::model()->findByPk($pk);
        return $categoryGroupMainOb->name;
    }
?>

 

Get name from related data model and display in CDetailView:

In my previous example: I have explained how to get corresponding name/data from other model when we know only the id of that model current scope.

Now if i want to show the name in view page also then i just have to call that function only.

<?php
$this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'id',
        'main_id',
        array(
            'name'=>'main_id',
            'value'=>CategoryGroupMain::model()->catMainGetName($model->main_id),
        ),
        'name',
    ),
    ));
?>

I just add the value parameter and use that function.

Normally when i generate any CRUD by gii in Yii, its just shows the text fields. If any one want to change it to drop down list, and want to fetch data from another table, then we have to do this:

<?php 
    echo $form->dropDownList($model,'user_type_id', 
            CHtml::listData(UserType::model()->findAll(), 'id', 'type'), 
            array('empty'=>'Select Type'));
?>

Here:

  • $form->dropDownList is a widget.
  • UserType is a model.
  • CHtml::listData is a function which will genarate a select list from model data.
  • Here in listData function 2nd parameter is $valueField and 3rd parameter is $textField, which are equivalent to  HTMLselect options value and name.

Leave a Reply

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