How to access non-child models from other controller in CakePHP?
Question
I'm having trouble finding information about how to use a model that doesn't belong to the current controller.
For example: I have an ajax controller that sends only JSON data to application. This controller needs to access different models depending on request.
How do I do this?
Solution
Check out the Manual section titled $components, $helpers and $uses, you are looking for $uses
:
<?php
class RecipesController extends AppController {
var $name = 'Recipes';
var $uses = array('Recipe', 'User'); // both models will be available
var $helpers = array('Ajax');
var $components = array('Email');
}
?>
OTHER TIPS
$uses causes performance hit. Better way is:
$User = ClassRegistry::init('User');
$User->find(...);
If the Recipe and User models are related you could do something like:
$this->Recipe->User->find(....);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow