Get Data From Linked Table

I’ll try to be as explicit as possible:<br class=""> then, I have the table RATING and the table MUSIC , table RATING has a field related to the MUSIC table:

i.imgur.com/3hFnj.png

Model Rating:


	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(

			'music' => array(self::BELONGS_TO, 'Music', 'musicid'),

		);

	}

Model Music:


	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(

			'gen' => array(self::BELONGS_TO, 'Genres', 'genid'),

			'user' => array(self::BELONGS_TO, 'YumUser', 'userid'),

        	'ratings' => array(self::HAS_MANY, 'Rating', 'musicid'),

		);

	}

and I am getting the data in the controller as follows:


		$dataProvider=new CActiveDataProvider('Music', array(

			'pagination'=>array(

				'pageSize'=>Yii::app()->params['musicpp'],

			),

		));

and then after that will be read in CListView, and so far so good, in the other relationships, in _view I can get such $data->gen->name, but when I try to do the same with the rating, such $data->ratings->rate, give me this error [color="#8B0000"][b]Trying to get property of non-object

[/b][/color][color="#8B0000"][color="#000000"]Which basically i want is a way to get the ratings of the current record from the table music (yes because a music can have various ratings)

[/color][/color][color="#8B0000"][color="#000000"]Someone can help me with this? This is my project to start learning Yii, but I’m a little lost, I already read the documentation but I understand better with practical examples[/color][/color]

Dear Friend

Since


$data->ratings

gives an array of objects.

You have to iterate throuh the array to fetch the values.




foreach($data->ratings as $rating)

  { 

    echo $rating->someAttribute;

    echo "</br>";

    echo $rating->someOtherAttribute;

  }



Regards.

[b]Yes, this works (:

Thanks dude.

Just one more thing, it’s right to do like this?

[/b]

Yes, it’s totally right :) YII-way.

yeah, like YII-way :P

deam, solving my problems is so easy with YII, i just need to learn a little bit ^^