Can't Get Related Table's Data

Hi there,

I’ve worked through tons of tutorials, but I cannot access data of a related table.

I’ve got three tables: User, Place, Event

They are related like this:


			'places' => array(self::HAS_MANY, 'Place', 'created_by'),

			'events' => array(self::HAS_MANY, 'Event', 'user'),


			'parent' => array(self::BELONGS_TO, 'Place', 'parent'),

			'places' => array(self::HAS_MANY, 'Place', 'parent'),

			'createdBy' => array(self::BELONGS_TO, 'User', 'created_by'),

			'events' => array(self::HAS_MANY, 'Event', 'event'),


			'place' => array(self::BELONGS_TO, 'Place', 'place'),

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

I just want to do the following: I want to get the latest 10 Posts, including the Name of the User and the Name of the event. I do not want to use lazy loading.

So I tried these:

$latest_events = Event::model()->with('place', 'user')->findAll(array('order'=>' DESC', 'limit'=>'10'));

$latest_events = Event::model()->findAll(array('with'=>array('place', 'user')));

$latest_events = Event::model()->findAllByAttributes(

			array(), // $attributes

			array('with'=>array('place', 'user'))


Whichever code I try, I cannot do like this in the view:

foreach ($latest_events as $event) { $event->place->name }

This will return null.

I know, the latter two codes neglect that I want the 10 latest posts - wanted to keep it simple for searching the error.

This is, what I would like to have (in SQL), but available through the model’s structure:

SELECT `t`.id, `t`.text, `place`.`name`,  `user`.`name`

		FROM  `event` `t` 

		LEFT OUTER JOIN  `place`  `place` ON (  `t`.`place` =  `place`.`id` ) 

		LEFT OUTER JOIN  `user`  `user` ON (  `t`.`user` =  `user`.`id` ) 


		LIMIT 10

Can you please enlighten me?

For everyone who may get this problem in the future, here is the solution.

Event has a column "place" with an id. Place has the corresponding "name". However, the relation also had the name "place". See here:

'place' => array(self::BELONGS_TO, 'Place', 'place'),

Thus, when calling $event->place, I did get the id, which is in But using $event->place->name, Yii seems to not know how it should resolve the relation.

The solution was to rename the relation to "fkplace":

'fkplace' => array(self::BELONGS_TO, 'Place', 'place'),

Then to use in the controller:

$latest_events = Event::model()->findAll(array('with'=>array('fkplace', 'user')));

And in view:



Edit: of course, this shortened solution misses fkuser and limit 10 and such, but I think that is no problem anymore now :).