Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between $uses and $this->loadModel() in CakePHP

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.60k
    Comment on it

     $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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: