search CGridView on static values and dynamic values

I generated a CURD on my table user_profiles.

I have id,email,created_by and some other columns here, created_by column refers to the same table id i.e user_profiles->id. It will tell you this user has been created by some x user.

In my table i have values like

id email, created_by

1 admin@xyz.com 0

2 ftpadmin@xyz.com 1

3 unixadmin@xyz.com 2

when i am showing in Grid instead of created_by value i am showing email. I have an user with id 1&2 but not 0.

If my created_by value is 0 then i am showing it as System which you can find the same below. But value 0 is not in my table.

My CGridView params are like this




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

	'id'=>'user-profile-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'email',

		'realname',

                array(

                    'name'=>'status',

                    'filter'=>array(0=>"Enabled",1=>"Disabled"),

                    'value'=>'@$data->status ? "Disabled" : "Enabled"',

                ),

                array(

                    'name'=>'role_id',

                    'filter'=>CHtml::listData(Roles::model()->findAll(array('order'=>'name')),'id','name'),

                    'value'=>'$data->rolename->name'

                ),

                array(

                    'name'=>'created_on',

                    'value'=>'date("M j, Y", $data->created_on)',

                ),

                array(

                    'name'=>'created_by',

                    'value'=>'@$data->created_by ? $data->createdUser->email : "System"',

                ),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



My Model class search function is like below.




public function search()

	{

		$criteria=new CDbCriteria;

		$criteria->compare('t.email',$this->email,true);

		$criteria->compare('t.realname',$this->realname,true);

		$criteria->compare('t.created_on',$this->created_on);

		$criteria->compare('t.lastvisit',$this->lastvisit);

                $criteria->with=array('createdUser');

                $criteria->compare('createdUser.email',$this->created_by, true);

		$criteria->compare('t.role_id',$this->role_id);

		$criteria->compare('t.status',$this->status);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}



Now i would like to search with value ‘System’ even in my search filter of CGridView.

How can i do this.

Not tested, but it might be …




public function search()

	{

		$criteria=new CDbCriteria;

		$criteria->compare('t.email',$this->email,true);

		$criteria->compare('t.realname',$this->realname,true);

		$criteria->compare('t.created_on',$this->created_on);

		$criteria->compare('t.lastvisit',$this->lastvisit);

                $criteria->with=array('createdUser');

                if ($this->create_by == 'System')

                        $criteria->compare('t.created_by', '0');

                else 

                        $criteria->compare('createdUser.email',$this->created_by, true);

		$criteria->compare('t.role_id',$this->role_id);

		$criteria->compare('t.status',$this->status);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}



Solution worked perfect. I modified a little bit code, its considering case sensitive on search so some code change




if (strtolower($this->created_by) == 'system')

     $criteria->compare('t.created_by', '0');

else

     $criteria->compare('createdUser.email',$this->created_by, true);