Validate flag in CGRidView to display certain value

Hi everyone.

I have a CGridView :





<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'ordered-product-grid',

	'dataProvider'=>$orderedProducts,

        'cssFile'=>Yii::app()->baseUrl.'/css/admin/cgridview.css',

	'nullDisplay'=>'N/A',

	'columns'=>array(

		array('name'=>'pc_name',

                      'type'=>'html',

                      'header'=>'Category',

                      'value'=> 'CHtml::link(ProductCategory::model()->FindByPk($data->pc_id)->pc_name,array(\'productCategory/view\',\'id\'=>$data->pc_id))',


                ),

		array('name'=>'item_name',

                      'type'=>'html',

                      'header'=>'Category',

                      'value'=> 'CHtml::link(Coupon::model()->findByPk($data->item_id)->coupon_name,array(\'coupon/view\',\'id\'=>$data->item_id))',

                ),

		array('name'=>'merchant_name',

                      'type'=>'html',

                      'header'=>'Category',

                      'value'=> 'CHtml::link(Merchant::model()->FindByPk($data->merchant_id)->merchant_name,array(\'merchant/view\',\'id\'=>$data->merchant_id))',


                ),

		'qty_ordered',

                array(

			'class'=>'CButtonColumn',

                        'header' => 'Action',

		),

	),

)); ?>




So, from the above code, I retrieve the Coupon::model()->findByPk()… What I want to do is to validate the product category first (pc_id). If the pc_id == 1, then retrieve from Coupon, if 2, then retrieve from Merchandise. Can I do this using CGridView ? Thanks

sure you can , if the logic is too complex you can write a method to handle it :

in your model (the $data is an instance of your model class ) write an method to handle it :

       public function xxxx(&#036;productCate){


            //here you are  <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/laugh.gif' class='bbc_emoticon' alt=':lol:' /> 


            //because this is in your model class you can directly access the pc_id------&#036;this-&gt;pc_id


                 if(&#036;this-&gt;pc_id == 1){


                     return Coupon::xxx...


                 }


                 if(&#036;this-&gt;pc_id == 2){


                     return Merchandise::xxx...


                 }


        }

then : ‘value’=> 'CHtml::link($data->xxx(…)

you should understand what $data stand for :lol:

or you can write a static public function in any class then do like above , just consider how to pass the parameter :

                  'CHtml::link(MyClass::someMethod(&#036;param1,&#036;paramX....)......'

wow, great, thanks a lot :)