0

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....

  • Btw, for future questions, include the exact error you get, stacktraces, notices, warnings, etc. – Nunser Apr 25 '14 at 16:19

1 Answers1

1

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).

Community
  • 1
  • 1
Nunser
  • 4,512
  • 8
  • 25
  • 37
  • if the models are not linked, you could also load the `Post` model and query it. Either using `loadModel`, `App::uses` or `$this->uses=array('Post', 'User')` – cornelb Apr 25 '14 at 16:34
  • Very true, though I don't encourage `App::uses` for performance reasons. `loadModel` and `ClassRegistry::init()` are the options. Didn't add them to the answer because I thought the OP just needed a little push in the right direction, not the whole cake. See also [this answer](http://stackoverflow.com/questions/980556/can-i-use-one-model-inside-of-a-different-model-in-cakephp) for further info. Thanks for pointing it out, @cornelb :) – Nunser Apr 25 '14 at 16:40
  • `App::uses` is for lazy loading http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#loading-classes You just need to create the object with `$Post = new Post();` There should not be any performance issue – cornelb Apr 25 '14 at 17:00
  • You could have 20 models to load, and you could be "lazy-loading" them for an action that just sums up two numbers. Granted, I haven't done the benchmarking myself, so I could be just spreading bad bad rumors, but from what I've read (and apparently can't find that much references right now), it's the least recommended option. Ah, [one ref](http://www.dereuromark.de/2012/02/13/what-really-speeds-up-your-cakephp-app/) – Nunser Apr 25 '14 at 17:05