Filtering Data With Cgridview

i have 2 models assignment and student related via belongs to relation, i tried to display the full name of the student in the cgridview and make a filter based on it.

the full name is displayed successfully but the filtering is not. why? can anyone tell me the error.

there is my code

in assignment model:




class Assignment extends CActiveRecord

{

	 public $fullname;

	 

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Assignment the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'assignment';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			

			array('studentID, status', 'length', 'max'=>15),

			

			array('id, fullname', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'student' => array(self::BELONGS_TO, 'Student', 'studentID'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'Assign ID',

			

			'studentID' => 'Student',

			

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

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

		

		$criteria->compare( 'student.fullname', $this->fullname );

		

		$criteria->compare('id',$this->id);

		

		$criteria->compare('studentID',$this->studentID);

		


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

	



in student model




public function getFullName()

	{

		//$model = new Student();

		return $this->E_Child_Name . " " . $this->E_Father_Name . " " . $this->E_Family_Name;

	}



in assignment/admin.php




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

	'id'=>'assignment-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		

		array(

			'name'=>'studentID',

			'type'=>'raw',

			'value'=>'CHtml::encode(Student::model()->findByPk($data->studentID)->fullname)',

		),



Hi you can write a custom query on filter and write a query on model search function.

for e.g




  array(

                        'name'=>'studentID',

                        'type'=>'raw',

                        'value'=>'CHtml::encode(Student::model()->findByPk($data->studentID)->fullname)',

                        'filter'=>GxHtml::listDataEx(Student::model()->findAll('studentID=\'data->studentID\''), 'id', 'fullname'),

                ),

what if there is one value not list of values, how write it?

thank you for your fast and valuable reply

i used the same to filter the type




array(

			'name'=>'BehavType',

			'type'=>'raw',

			'value'=>' Yii::app()->params[\'bType\'][$data->BehavType]',

			//'value'=>'CHtml::encode(Behaviorgroup::model()->findByPk($data->behavGroupID)->E_BGName)',

			'filter'=>Yii::app()->params['bType'],


		),



2 options appear positive and negative, when i select positive the filter is not working

is there any thing to do

here is my full code attached

nothing is filtered from the cgridview unless the id, the date and the behavior if i write its id not its name

kindly help me

I’ve just taken a quick look, I didn’t see you compare BehavType in your search method?

Hi freshyiiuser,

I think the solution can be like this:

I have two models with same relation as you




Location

- id

- inserted


LocationLanguage

- location_id

- language_id

- title

- slug



So if I want to filter locations by "fulltitle" which can be concatenated string of "title" and "slug" I should do this:

Language model class:




class Location extends CActiveRecord {


  public $fullTitleSearch;


  ...


  /**

   * Retrieves fulltitle (used in gridview as value)

   */

  public function getFullTitle()

  {

    return $this->title . '-' . $this->slug;

  }


  ...


  public function rules()

  {

    ...

      array('fullTitleSearch', 'safe', 'on' => 'search'),

    ...

  }


  public function search()

  {

    $criteria = new CDbCriteria;


    if ($this->fullTitleSearch)

    {

      $criteria->with = array(

        // this is relation name between Location and LocationLanguage

        'currentLanguage' => array(

          'together' => true,

         ),

      );


      $criteria->addSearchCondition('CONCAT(currentLanguage.title, currentLanguage.slug)', $this->fullTitleSearch, true, 'AND', 'LIKE');

    }


    ...

  }




Than in your GridView widgets you must specify column in this way:




  ...

  array(

    'name' => 'fullTitleSearch',

    'value' => '$data->fullTitle',

  ),

  ...



Hope it helped you.