Reputation: 1433
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);
Upvotes: 1
Views: 692
Reputation: 522109
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.
Upvotes: 2
Reputation: 1034
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.
Upvotes: 3