Yii Framework Forum

Cgridview Call Model Function


(Dhanakumar M) #1

You can add column display the data generated by object method,

for example a method of your model.


public static function getRoleName($userId)

{

	$sql 	  = "select role.RM_NAME from XXX role 

						where role.RM_ID=(select qqq from ZZZ where XXX=".$userId.")";

	$command  = Yii::app()->db->createCommand($sql)->queryAll();

	$roleName = $command[0]['RM_NAME'];

	return $roleName;   	 

}

In gridview


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

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        ...

       array(

	'header'=>Yii::t('ui','role_name'),

	'name'=>'UM_ID',

	'value' => function($data){

		     return User::getRoleName($data->UM_ID);

		   }

       ),




(mbi) #2

what is your question?


(Abhishek2890) #3

now onwards put this type of stuffs in Tips, Snippets and Tutorials Section yii forum. :)

Some admin please move it to right section


(Codesutras) #4

This is something irreverent thing you have posted here.i dont thing anything is useful in this post.It’s just like a self practice.Which is also in a wrong direction.


(Attal Rakhi) #5

User::getRoleName($data->UM_ID); in place of this I want to Pass other model variable which are set above gridview

like User::getRoleName($model->client, $model->year, $data->id);

hows this possible ?? Please help


(hussain patel) #6

@RakhiKasat you can use php ‘use’ keyword passing variables to function.

check this Link
https://www.yiiframework.com/doc/api/1.1/CDataColumn#c9734