KeepLearning Posted September 7, 2015 Share Posted September 7, 2015 I'm not sure how the second part of this example is related to the first, and where it belongs. I am quoting from page 174 of YiiBook2: # models/Page.php public function getUser() { return $this->hasOne(User::className(), ['id' => 'user_id']); } This means you can fetch every page with every page author in one step: $pages = Page::find()->with('user')->all(); I'm guessing that with('user') calls the getUser method. Is that right? But in which file does the second part of the example belong? In the view file? Link to comment Share on other sites More sharing options...
Larry Posted September 7, 2015 Share Posted September 7, 2015 Apologies that this isn't more clear. Yes, the with('user') ends up calling the getUser() method. As for where you'd do that--retrieve Page instances, you might do it in a model, you shouldn't do it in a view (it's too much code execution for a view), but most commonly it'd be within a controller. 1 Link to comment Share on other sites More sharing options...
Recommended Posts