Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Can't Get Related Table's Data

$
0
0
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:
User
			'places' => array(self::HAS_MANY, 'Place', 'created_by'),
			'events' => array(self::HAS_MANY, 'Event', 'user'),


Place
			'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'),

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'=>'event.id 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` ) 
		ORDER BY t.id DESC 
		LIMIT 10


Can you please enlighten me?

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>