A fusion of technology, music, and geekyness.

CakePHP: Including Models inside your Controller

There are currently three methods for including and using models inside your controller. I am referring to the case when your controller does not belong to a model or or the model is not related to the model inside the controller you are working in. Although this case should be extremely rare, it seems to happen to me a lot. This post is basically a re-summary of Gwoo’s response in the CakePHP Google Groups discussion.

1. Controller::loadModel();
This method actually calls ClassRegistry::init() and saves the instance of the model as a parameter in the controller.

Example:

Controller::loadModel('Car');
$cars = $this->Car->find('all');

2. ClassRegistry::init()
This method creates an instance of the Model and returns it for use.

Example:

$car = ClassRegistry::init('Car');
$cars = $car->find('all');

3. App:import() (not recommended)
This method only includes the file. This requires you to create an instance each time.

Example:

App::import('Car');
$car = new Car();
$cars = $car->find('all');

As you can see this is really simple and basic stuff. However, I felt the need to re-post this information. If for nothing else, I will be using this as a reference point for myself :)

Update 5/27/09: Miles Johnson has a nice write up on this on his blog

9 Responses to “CakePHP: Including Models inside your Controller”

  1. Miles J says:

    Controller::loadModel(‘Car’) can also be $this->loadModel(‘Car’);

  2. jesse says:

    @Miles J: Cool. Thanks for posting it.

  3. michas says:

    Controller::loadModel is deprecated.

  4. jesse says:

    @michas: “Yes the old loadModel() function from basics was deprecated.
    However the loadModel() method in Controller is still used.
    http://api.cakephp.org/class/controller#method-ControllerloadModel

    From the Gwoo man himself

  5. Joel Perras says:

    As is noted in the documentation, Controller::loadModel functions in such a way so that when Controller::$persistModel is set to true, the model you specify will also be persisted. This is the recommended method for loading additional models into a controller, but should only be used when the specified model is otherwise unreachable through normal means (e.g. associations).

    Really, Controller::loadModel is a specialized wrapper for ClassRegistry::init, which is itself a factory-based wrapper for App::import, which is a class-loader wrapper for the PHP native ‘require’ construct. Each additional layer of abstraction performs a slightly different role; it’s up to you to determine which is most appropriate.

  6. [...] article reprend les conseils déjà rabachés à divers endroits en langue anglaise, et également les explications de gwoo sur le Google group anglais… en essayant de [...]

  7. prabha says:

    I have added this in users controller
    Controller::loadModel(‘User’);

    and added the following in Model
    function afterSave($created)
    {

    $t['data'] = serialize($this->data);
    $this->changes->save($t);
    return true;
    }

    when call this in controller i got the following error,
    Missing Database Table

    Error: Database table Array for model User was not found.

    Please help.
    Thanks,
    Prabhakar

  8. Devasish Ghosh says:

    Thanks……

Leave a Reply

Powered by Wordpress | Designed by Elegant Themes