Saturday, May 24, 2014

Re: load 2 models in controller

You have a few options really. In your controller, if you feel you will use the Teacher model in majority of actions/methods you can add this property:

public $uses = array('Teacher');

If you think you will use the Teacher model several times in a single action, you can use the following:

$this->loadModel('Teacher');

And as Advantage+ put it, if you only need to use the Teacher model one single time, it may be better to do the following:

ClassRegistry::init('Teacher')->find('all');
(This is the same as $this->Teacher->find('all'), just a single instance)

Finally there is one other option which hasn't really been discussed which is to use requestAction from within your view, what this does is it calls a specified controller action and retrieves the result sending it to your view, but it isn't always a great idea as it can carry some overhead which may slow down your application.

In your Teachers controller

public function list() {
    if(empty($this->request->params['requested'])) {
        throw new ForbiddenException(__('You cannot access this method directly'));
    }
    return $this->Teacher->find('all');
}

In any view you want to access the teacher list

$teachers = $this->requestAction('/teachers/list');

http://book.cakephp.org/2.0/en/controllers.html#Controller::$uses
http://book.cakephp.org/2.0/en/controllers.html#Controller::loadModel
https://groups.google.com/forum/#!topic/cake-php/E3xXtOsBAxc
http://book.cakephp.org/2.0/en/controllers.html#Controller::requestAction
http://mark-story.com/posts/view/reducing-requestaction-use-in-your-cakephp-sites-with-fat-models

If none of these help, you may have an issue with your model not being found (which I think may be the issue already, check the filename, model name, extends etc that this is correct)


On 24 May 2014 08:28, Andrew Barry <jagguy999@gmail.com> wrote:
I dont think it should be this complicated.
You are just loading a model in a controller and i see many examples of ths but i cant get it to work.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards
 Stephen Speakman

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: