Tuesday 11 February 2014

How to set the country, state relationship in Yii




My Country table:
================
id
name


My state table:
===============
id
country_id
name



Here i need to refer the country from state using the country_id relationship

So first i need to generate a Relationship in my State Model like below


public function relations()
{

return array(
'country'=>array(self::BELONGS_TO, 'Country', 'country_id'),
);

}

Country here is the country model
country_id is the reference key in state table to refer the country table


Ok so in CGrid view use the below format

'country.name',


===========================================================================

And if we need filtering for countries in state admin?

Just use the compare setup like below

public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name);
$criteria->with = array('country');
$criteria->compare('country.id',$this->country_id, true);

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}


And in your Cgridview area have it like below


array(
'name'=>'country_id',
'header'=>'Country',
'value'=>'$data->country->name',
'filter'=>CHtml::listData(Country::model()->findall(),'id','name' ),
),

Wednesday 5 February 2014

How to set the active and inactive status for 1,0 values in YII CGridView

In Yii normal CGridView will have the below structure


'columns'=>array(
'id',
'title',
'sstatus',

So to set the 0 as Inactive and 1 as Active 

we need to edit the 'status' as

array(
'header'=>'status', //for to set the title for the table column, we can remove this if we need to show the default value
'name'=>'sstatus' // where sstatus is my database column name
'value'=>'($data->sstatus==1)? "Active" : "Inactive"'
),


Once set we can see 0 as inactive and 1 as active. 


But how about filtering? 
The filtering in Yii will work when we input 1 and not as active, so just replace this filter area with dropdown list in the same area where we updated our above code, just add filter array like below



array(
'header'=>'status', //for to set the title for the table column, we can remove this if we need to show the default value
'name'=>'sstatus' // where sstatus is my database column name
'value'=>'($data->sstatus==1)? "Active" : "Inactive"',

'filter'=>CHtml::dropDownList('Slider[sstatus]', 'sstatus',  
                array(
                    ''=>'All',
                    '1'=>'Active',
                    '0'=>'Inactive',
                )
            ),
        ),

),



in the above Slider is my model name and sstatus is my database table column... thats it.......


But i faced some problems like auto select and select all is not working, so we can set the filter directly like the below code


  array(
           'name' => 'sstatus',
           'filter' => array(0 => 'In Active', 1 => 'Active'),
'value' => '($data->sstatus == 1) ? "Active" : "Inactive"',
       ),