Jump to content
Larry Ullman's Book Forums

Calling The Getuser Function: Where Does With('user') Belong?


KeepLearning
 Share

Recommended Posts

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

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. 

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...