How to display Relational Data

Hi dears,

I have the data from a relational object. I want to display it using DataProvide but it is not working.

Any help?

I am getting the data as

		$dataProvider=new CActiveDataProvider(










Where as in index.php, when I try to get the attribute or value of the object of the relation, it gives the error.

my code is as

	<b><?php echo CHtml::encode($data->getAttributeLabel('section_id')); ?>:</b> // works fine

    <?php echo CHtml::encode($data->section_id); ?> // gives error that the object does not have that property defined

How to fix it?


  foreach($dataProvider->getData() as $data)

   echo CHtml::encode($data->section_id); 


(should work)


Thanks dear tri.

But your code did not work.

It gives the same error.

Any help?

How did you declare the models attributes and relationships.

I also noticed you already have $data defined in <?php echo CHtml::encode($data->section_id); ?> (and you told us it works). Something is missing from the view code.


Hi tri,

I am supplying the data from the CActiveDataProvider with Relational data to the ClistView.

when I tries to get the attribute of the relational data it works fine as

 echo CHtml::encode($data->getAttributeLabel('status'));

But When tries to fetch the value of the relational data it gives the error that Categories.section_id is not defined property of the Object Categories. as

echo CHtml::encode($data->section_id);

I tried to bring it to work by having it as


When I try the following code it displays the data


But when I tried to get it as

echo $section->section_id; 


echo $section['section_id']; 

It gives error.

Any help?

Try checking if the value exists before you try and echo it. Because your are looping through results if one field in one row is missing you will get that error.

Try using

if(isset($section->section_id)) echo $section->section_id; 

Dear Angelo,

I know that these exists in the requested data.

Any help?

It’s obvious that it isn’t in the model.

What is the schema for your table?


there are three tables that are currently being accessed by this query

Sections with PK section_id

Categories with PK category_id

Section_category_relation which have the PKs of section and category tables.

The relations are as fellow

in Sections

categories -> array(self::has_many,categories,section_category_relation(section_id,category_id))

in Categories

sections -> array(self::has_many,‘sections’,section_category_relation(category_id,section_id))

and In section_category_relation

section -> array(self::belongs_to,‘sections’,section_id)

category ->array(self::belongs_to,‘categories’,‘category_id’)

So, by accessing the data of the categories, it will also fetch the data of the sections too. But it will not display it as should.

The relations HAS_MANY is a bit odd to me… you are creating them like they are MANY_MANY…

Check this thread, it can give you more ideas -

echo $section[0];


I will check it.