Loading app models inside a plugin controller in CakePHP

I recently ran into a problem while writing a plugin in CakePHP. I wanted to do some authentication with one of the models in my app. I added this in the beforeFilter of my AppController. In there I was using $this->loadModel to load the model on the fly. This works fine inside the app. However, as soon as you move to a plugin things break down. It will try to load the model inside your plugin. Since that model doesn’t exist in the plugin, it will fail. You can specify a plugin in the loadModel call, like ‘Plugin.Model’, but you can’t specify no plugin. The way to get it to load correctly is the $uses variable. That will load the correct model when initializing the controller. Then you can use it as normal with $this->Model. This has the side effect of always loading the model on every controller. In my case that was perfectly fine since its used for auth on all actions. I can’t see any viable way to use loadModel to load a model outside of your plugin.

One Comment

  1. Mario

    I’m having the oposite problem.

    When I issue loadModel like $this->loadModel(‘Franchises.Franchise’) Cake creates the model on the fly instead of using the Franchise Model inside the current plugin.

    Any advice?

Leave a Reply

Your email address will not be published. Required fields are marked *