How to load multiple Models in cakephp controller

You must be aware of function called “loadModel” in cakephp while working in CakePHP controller.

Just for refreshment about how to use loadModel in cakephp Controller

$this->loadModel('ModelA');

To load 2 models in controller, you must be writing

$this->loadModel('ModelA');
 
$this->loadModel('ModelB');

But in the article we are concerned with How to load more than 1 model in just 1 line code in Cakephp Controller.

To archive this, in your Controller just add this like after the starting PHP tag and before the Class keyword.

var $uses = array(‘ModelA’,’ModelB’);

Just for your reference the full phase code will be

<?php

Class AsController extends AppController{
  var $uses = array('ModelA','ModelB');
  public function index(){
    debug($this-&gt;ModelA-&gt;find('all'));
  }
}

Keep Coding 😛