CakePHP - query is name of model function [closed]
Controller:
<?php
class VideosController extends ForumAppController {
/**
* Controller Name
* @access public
* @var string
*/
public $name = 'Videos';
public function index() {
$videos = $this->Video->getVideos();
$this->set('videos', $videos);
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('*');
if (isset($this->params['admin'])) {
$this->Toolbar->verifyAdmin();
$this->layout = 'admin';
}
开发者_开发百科 $this->Security->validatePost = false;
$this->set('menuTab', 'videos');
}
}
?>
Model:
<?php
class Video extends ForumAppModel {
public $name = 'Video';
function getVideos() {
$vids = $this->find('all', array (
'order' => array('Video.id DESC')
));
return $vids;
}
}
?>
I get an error:
Notice (8): Undefined property: VideosController::$Video [CORE/plugins/forum/controllers/videos_controller.php, line 13]
If I do
$this->loadModel('video');
I get an error:
Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'getVideos' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 549]
**Query: getVideos**
Any ideas what might be causing this?
You code appears to be correct but it seems like CakePHP isn't trying to load the model from the right place. You can specify the model to load using the $uses variable in your controller.
Because you're using a plugin, you need to add the name of the plugin in front of the model.
$uses = array('Forum.Video');
CakePHP should handle this on it's own but some older version of CakePHP had a bug that prevented this from working properly. It appears to be fixed in 1.3.10.
For more details about the $uses variable, see http://book.cakephp.org/view/961/components-helpers-and-uses.
The $name variable is controller name, not model name controller uses,
try this
$uses = "Videos"
精彩评论