tags:

views:

71

answers:

2

How can I load a model? I have tried several times but it doesn't work.

My code is:

<?php
class NotesController extends AppController {
 var $name='Notes';

var $helpers = array('Html','Form','Ajax','Javascript');
var $uses = array('note');
var $components = array('ModelLoader');

function index(){

     $this->ModelLoader->setController($this); 
     $result = $this->params['url']['obj'];
     //print_r($result);
         $ee=$this->ModelLoader->load('note');

         $pass = $this->note->search($result);
+2  A: 

The model is automatically loaded and accessible at $this->ModelName in controller functions. Which model to load is determined by the name of the controller, NotesController automatically loads the Note model. Other models can be loaded via var $uses = array('Model');

class NotesController extends AppController {

    var $name='Notes';
    var $helpers = array('Html','Form','Ajax','Javascript');

    function index() {
        $this->Note->someaction();  // Accessing the model
    }
}

Maybe you should follow the tutorial first.

deceze
+3  A: 

If the other model/s are only being used one or twice within functions, isn't it better to use

$myModel = ClassRegistry::init('SomeModel');

I have heard about that model loader. but i have read posts where some recommend against using it.

Vicer
Looking at that ModelLoader component, it's 3 years old. Maybe it was of use back in the days, but it's surely nonsense in recent Cake versions. `ClassRegistry::init()` is enough.
deceze