Problem with relational CGridView filter

Hello everyone,

I’m trying to get a filter on a relational field in the CGridView.

But the filter wont work i get an error on $this->_country = $this->products->Country;

The error is: Trying to get property of non-object

So $this->products->Country must be undefined.

I got this code from some other questions on this forum. Can anyone tell me what is it I’m missing?

In the model:




private $_country = null;

	

public function getCountry(){

	if($this->_country === null && $this->products_Id !== null) {

		$this->_country = $this->products->Country;

	}

	return $this->_country;

}

	

public function setCountry($value){

	$this->_country = $value;

}


public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;

		$criteria->with = array('products'=>array('alias'=>'products'));

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

		$criteria->compare('source',$this->source,true);

		$criteria->compare('Uploaded',$this->Uploaded,true);

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

		$criteria->compare('products.Country',$this->country);

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



In the view:




<?php

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

    'id'=>'images-grid',

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

    'filter'=>$model,

    'columns'=>array(

        'source',

		'Uploaded',

		'products.EAN',

		'products.Productname',

		array(

		    'header'=>'Merk',

		    'name'=>'products.brands.Brand',

			'filter'=>CHtml::listData(

                Brands::model()->findAll(array('order' => 'Brand')),

	            'Brand','Brand'

	        ),

		    'value'=>'$data->products->brands->Brand',

		),

		array(

		    'header'=>'Landcode',

		    'name'=>'Country',

			'filter'=>CHtml::listData(Products::model()->findAll(array('order'=>'Country')),'Id','Country'),

		    'value'=>'$data->products->Country',

		),

        'products_Id',

        array(

            'class'=>'CButtonColumn',

        ),

    ),

));

?>



Can you post your relations() method?

That says to me that $this->products is undefined, which is different.

[size="1"]Edit: formatting.[/size]

Of course here it is:




	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(

			'products' => array(self::BELONGS_TO, 'Products', 'products_Id'),

		);

	}



Try




  ...

  $criteria->together = true;



/Tommy

Shouldn’t it be $this->products->country (with a small c)?

I’ve tried but it does not work I’m afraid.

Country is with capital C in the products model.

From products model:




public function attributeLabels()

{

	return array(

		'Id' => 'ID',

		'EAN' => 'Ean',

		'Productname' => 'Productname',

		'Country' => 'Country',

		'Brands_Id' => 'Brands',

	);

}



Got it I removed the products_Id from the CGridView which I don’t need anyway and now it works! This is the foreign key for the products table. Any ideas why this didn’t worked?

Hi all,

I have problem with relational CGridView filter.

I have this in my view/admin.php




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

	'id'=>'mskaumsaleh-grid',

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

	'filter'=>$model,

	'columns'=>array(

                ..

		array(

			  'name' => 'test.NoHall',

            'filter' => CHtml::listData(Mshall::model()->findAll(), 'RecordID', 'NoHall'), // fields from country table

//            'value' => 'Mshall::Model()->FindByPk($model->HallID)->NoHall',

		),

        ..

	),



and I have this in my models search() function




                $criteria=new CDbCriteria;

		...

		$criteria->compare('test.NoHall',$this->test->NoHall,true);

                ...



this way, my column is populated with data but the drop down list on the filter doesn’t appear. pls adv.

I’m very new both in PHP and Yii’s frame work and this is my first project. Thx.

It’s hard to tell because you haven’t posted your entire search() method, but it looks like you might be missing


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

If that’s not it, I always point people towards the Wiki article here: http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/. Start over with your relation and work through the steps there and come back if you still have problems, but it seems to solve problems for many people.

Joshua,

thank you very much for your prompt reply. I went through the URL you gave me and it solves my problem. Thank you again.

BR.

JH

Glad to hear it!