user3532274
user3532274

Reputation: 29

cakephp: blog tutorial tutorial extended

I am trying to extend the simple blog tutorial found in the documentation.

I have an index.ctp on /app/View/Users/index.ctp with all users being shown with a link on each username.... When i click on the username, I expect it to go to the view.ctp and show each user's blog posts and the number of posts each user has done.

I get an error here. Do I need to write a component for this? If so, how do I go about it?

class UsersController extends AppController {
  public function view($id = null) {
   $this->set('allposts',$this->Post->findByUserId($id));
  }
}

So I get an error at this point because it cannot see the Post model in the user model. How do I go around this?

Thanks....

Upvotes: 0

Views: 68

Answers (1)

Nunser
Nunser

Reputation: 4522

No, you don't need a component for that. If you have linked your models correctly (that's it, set belongsTos and hasMany(es) etc etc), then you have to access the post by concatenating.

$this->set('allposts',$this->User->Post->findByUserId($id));

Please see other questions regarding this.

Now, extending this a little bit since you seem a bit confused on what Components actually do. Components are a way to extending or adding functionalities to Controllers. If you want to, say, create a new Paginator, or call Facebook from your controller to get info, and there's no definite place to put that (users can have facebook info, but if you have another controller named "Friends", you might have to put some facebook functions there too).

Normally, if you think you need a component to have calls from one model to another, that's a sign you are organizing things wrongly (not a general rule, I'm open to examples on when this isn't true).

Upvotes: 1

Related Questions