CakePHP : Load Other Model In Controller

Hello Friends, These days I am totally giving tricks and ideas relates to cakePHP and in this article I will also tell you how to load other model in other controller to use instantly. There are two ways to make available other model in controller, One is $uses variable in controller and other is loadModel function. $uses way is not efficient to make available or to include other model in controller So there is second way by which we can load other model in controller is loadModel function.

I am always using loadModel function to load model in controller but I am giving overview of both ways.

First I am explaining $uses variable method. Whenever you want to include or make available other model in controller than you can use $uses variable at start of controller like this
<?php
class RecipesController extends AppController {
var $name = 'Recipes';
var $uses = array('Product', 'User');
}
?>

As you see in above code by $uses variable I have included or make available two models Product and User, So If you want to make available other model in controller you can use $uses variable and than write your model name in that array.

Second way loadModel function, It is efficient way to make available model or load model in controller. Syntax of loadModel function :
$this->loadModel('User');
$users= $this->User->find('all');

In this way you can load model, Just pass model name in loadModel function and than you can use that model for Db interaction.

Subscribe to PHP Freelancer

Enter your email address: