Jump to content
Larry Ullman's Book Forums
KeepLearning

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

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?
 
 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...