Access property, gridview, relation


i’ve been searching around, but cannot find a suitable example or help how to access a property of a relations relation.

I’ve got the following structure:


$model = Offer::model()->with('offerCategories')


               				->before(date('Y-m-d H:i:s'))

               				->after(date('Y-m-d H:i:s'))




                                        	'select' => 'id,title'



    	$dpLatestOffers = new CArrayDataProvider($model);


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(

                	'user' => array(self::BELONGS_TO, 'User', 'user_id'),

                	'radius' => array(self::BELONGS_TO, 'Radius', 'radius_id'),

                	'offerCategories' => array(











                	'users' => array(self::MANY_MANY, 'User', 'user_rating(offer_id, user_id)'),



I want to access the property name in the category relation.

I tried this in my view:









But i get only Trying to get property of non-object.

Can anyone help?

I suppose that your view is using CGridView?

As each Offer may have more categories, $data->offerCategories may be an array of AR-objects (categories). Have you tried to make a var_dump of $dataProvider->getData()?

Another problem may be that you are using CArrayDataProvider and not CActiveRecordProvider. I rarely use the first one, but I think this returns arrays and not AR-objects. Again a $dataProvider->getData() could help to understand this.

Last, I would use a many-many relation between your Offer_table and Category_Table, with a link_table(offerId,categoryId) to connect the two, but I think that is optional, and what is best depends on your DB-structure.

Hope that will help. Good luck,


Thank you for the help. I did not try


, maybe I should do that. :)

You are right with each offer can have more than one category, so there is an array inside. I would prefer ARs instead of array, so I would user CActiveDataProvider, when you tell me how I can pass my model to it?! :)

I tried

$dataProvier = new CActiveDataProvier($model);

but that did not work.

I build an extra function that iterates thorugh the array and gives me the category names as string. Maybe not the best solution, but it does it for no.

And there is a link table between offer and category, the table offerCategory (that is the link table holding an offer_id with a category_id. Look at my relations ;)

Be careful with the spelling: "CActiveDataProvier" should be "CActiveDataProvider". And you can find a good example of the use in CActiveDataProvider.

If that solution works, I think it is ok as well. You would anyway have to iterate through the AR’s as well.

Yes, the DB-structure is fine, but you can make the relation more direct using MANY_MANY relation. Here is an example:

'Targetgroups' => array(self::MANY_MANY, 'LuTargetgroup', 'tbl_th_targetgroup(th_id, targetgroup_id)','index'=>'targetgroup_id'),

More info in

I know it should be CActiveDataProvider, sorry for misspelling. Isn’t there a simple solution to push a model into a CActiveDataProvider, like

$dp=new CActiveDataProvider($model);


Ok, if i have to iterate throug an array of object or through the AR is similar.

I will try this!

thanks for the help sune!!

Isn’t there a simple solution to push a model into a CActiveDataProvider, like

$dp=new CActiveDataProvider($model);


Look at the __constructor for CActiveDataProvider. There are two options for $modelClass, but you can’t put in a single model like you wrote it above, which makes sense, as the CActiveDataProvider is working with a list/array of models.

Good luck. Btw, I encourage you to get familiar with the Yii API. You can learn a lot by studying this (and sometimes the code behind if necessary (The api links to the code)

Best regards, Sune