CakePHP doens't support load models?
I use CakePHP 1.3.9 but I can't use other Models in a Controller.
I use $this->loadMod开发者_Go百科el('ModelName);
and $this->ModelName->find('all')
- always empty.
The variable $uses
also doesn't work.
Why is it not working for me?
I used i18n and must set $locale...
Do you mean the data set is empty? If the model is not loaded, you shouldn't be able to call $this->ModelName->find()
as $this->ModelName
would be null. Does it throw an error? Your usage is correct, as stated in the manual : http://book.cakephp.org/view/992/loadModel
You can also do
App::import('Model', 'ModelName');
$model = new Model();
But I'm guessing that your current resultset is returning empty rather than the model itself not being set.
Have you tried looking at what $this->ModelName
actually contains? Do the following and post it here
pr($this->ModelName)
It's considered bad practice to put (un-associated) models in your $uses
array.
Depending what you are trying to do, you may be able to make use of containable behaviour.
$this->User->Post->find('all');
If not, you should be able to use loadModel
:
$this->loadModel('Article');
$recentArticles = $this->Article->find('all', array('limit' => 5));
To quote Cake:
The loadModel function comes handy when you need to use a model which is not the controller's default model or its associated model.
JohnP and Ross are correct. Controller::loadModel() is clearly working and not your problem if pr($this->ModelName)
is working for you.
As they mentioned, you're probably having trouble because the data simply isn't in the database. Or maybe there's something wrong with your query. Have you tried checking the query that's produced by CakePHP and trying to query the database directly through the MySQL command line (assuming you're using MySQL)?
Or is there any chance you've overloaded the Model::find() method?
精彩评论