$uses:
$uses is used to load model in a controller. It defines which models the controller has access to by default. We can load multiple models easily using $uses. But it is not a good practice to load all models at once. Best way is to load the model when it is needed.
Loading a single model in a controller:
|
var $uses = array('modelname');
|
Loading multiple model in a controller:
var $uses = array('modelname1','modelname2','modelname3');
$this->loadModel():
loadModel also used to load model but it loads model only where it is called and it is the best way if you only need to use the model for one action and not others. It is the convenient way to load models in controller and mostly preferred by programmers.
Example:
public function users() {
$this->loadModel('User');
$data = $this->User->find('all');
}
public function myCart() {
$this->loadModel('Cart');
$data = $this->myCart->find('all');
}
0 Comment(s)