Accessing relational attribute in a CGridView

I have the following relationship set up:

Supplier Model:


'supplier_region'=>array(self::BELONGS_TO, 'Town', 'supplier_town'),

supplier_town is an integer value, which will link to Town.id so that I can retreive Town.region

In my controller I have:


$model=new Supplier('search');

$model->with('supplier_region');

But I’m having trouble accessing the relation in a CGridView, I tried the following:


array(

	'name'=>'supplier_region',

	'value'=>$model->supplier_region->region,

),

But it did not seem to work. I’m not sure if I have done this correctly so could do with some assistance.

try

‘value’=>$data->supplier_region->region,

The value needs to be a string expression too.




array(

        'name'=>'supplier_region',

        'value'=>'$data->supplier_region->region',

),



Cheers guys, I’m sure I tried that but it seems to be working now!

Have you got any idea how I can make this attribute searchable on the filters?

You have to:

create a parameter in the model for the attribute


public $supplier_region

make it safe on search (in rules)

set the property ‘name’ in CDataColumn to supplier_region

So you will finally achive a textbox in filter that fill the newly created parameter $supplier_region

For make the search effective, in search() you have to:




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

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



Cheers zaccaria, I just had a go at that but I get an error. Surely if we define public $supplier_region that will conflict with my relation ‘supplier_region’? As when I try to access it by doing $this->supplier_region it will not know whether it’s referring to the relation or the attribute?

This is what gives me the error (in my model):




public function getRegion()

{

	return Enquiry::model()->Regions[$this->supplier_region->region];

}



(trying to get property of a non object)

You are right, I mistook.

You should use a different name for the property.

Just understand the philosophy: the property is needed for collect the user input, so you can name as you want.

The property should be safe, should be the name of the colunm and should be used for the search.

Of corse you cannot name as the relation, sorry!

I’ve just tried that as well but no luck I’m afraid. I don’t get the error but the search does not function. Can you post up an amended code solution in case I’ve done something incorrectly?

What is your problem?

Does the value of search appear after you submitted? If not means that the "name" is mistook or that the property is not safe.

If the value ther is after submission, there is some error in the search function.

Post your code (model and CGridView), maybe I can find your error.

Here you go mate:

Model:




public $region;


-- rules --


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


-- relation --


'supplier_region'=>array(self::BELONGS_TO, 'Town', 'supplier_town'),


-- search() function --


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

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



View file:




array(

	'name'=>'region',

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

),



EDIT: it’s working now - my ‘region’ field stores an integer value rather than a string. I created a dropdown filter instead and now it works fine.