Yii Framework Forum: Accessing relational attribute in a CGridView - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Accessing relational attribute in a CGridView Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 22 July 2010 - 05:10 AM

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.
0

#2 User is offline   betelgeuse 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 12-January 09

Posted 22 July 2010 - 05:28 AM

try

'value'=>$data->supplier_region->region,
a code snippet says more than a thousand words
1

#3 User is offline   GDzyne 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-June 09

Posted 22 July 2010 - 05:36 AM

The value needs to be a string expression too.

array(
        'name'=>'supplier_region',
        'value'=>'$data->supplier_region->region',
),

1

#4 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 22 July 2010 - 05:37 AM

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?
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 22 July 2010 - 07:00 AM

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);

0

#6 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 22 July 2010 - 07:10 AM

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)
0

#7 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 22 July 2010 - 07:48 AM

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!
0

#8 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 22 July 2010 - 08:00 AM

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?
0

#9 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 22 July 2010 - 08:21 AM

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.
0

#10 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 22 July 2010 - 08:30 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users