2011-06-13 107 views
2

控制器:CakePHP的 - 查詢模型函數的名稱

<?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'); 
    } 

} 

?> 

型號:

<?php 
class Video extends ForumAppModel { 

    public $name = 'Video'; 

    function getVideos() { 

     $vids = $this->find('all', array (
      'order'  => array('Video.id DESC') 
     )); 

     return $vids; 
    } 

} 

?> 

我得到一個錯誤:

Notice (8): Undefined property: VideosController::$Video [CORE/plugins/forum/controllers/videos_controller.php, line 13] 

如果我做

$this->loadModel('video'); 

我得到一個錯誤:

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** 

任何想法可能會導致什麼呢?

+0

如果我刪除表格視頻,我收到一個錯誤「錯誤:找不到模型視頻的數據庫表視頻。」 – 2011-06-13 05:22:17

回答

1

您的代碼似乎是正確的,但它似乎像CakePHP的是不是要加載模型從正確的地方。您可以使用控制器中的$ uses變量指定要加載的模型。

因爲您使用的是插件,所以您需要在模型前添加插件的名稱。

$uses = array('Forum.Video'); 

CakePHP的應該處理這個在它自己的,但CakePHP中的一些舊版本了會阻止這種正常工作的錯誤。它似乎在1.3.10中得到修復。

有關$ uses變量的更多詳細信息,請參閱http://book.cakephp.org/view/961/components-helpers-and-uses

+0

我檢查並三重檢查了這一點......我也刪除了緩存目錄並啓動了調試。我刪除了模型文件並得到相同的行爲。 :( – 2011-06-13 05:15:36

+0

是在哪裏找到這些文件?你能到你的應用程序文件夾提供完整的相對路徑? – 2011-06-13 05:18:47

+0

/plugins/forum/controllers/videos_controller.php /plugins/forum/models/video.php 我有一個類似的模型我的表格名稱是:videos – 2011-06-13 05:20:37

0

$ name變量是控制器的名稱,而不是模型名控制器使用,

試試這個

$uses = "Videos" 
+0

CakePHP根據控制器的名稱自動爲您設置$ uses變量。它基本上刪除了「控制器」後綴並使其成爲默認的$ uses值。如果您想要加載更多或不同的模型,或者根本不需要,您唯一想要更改的時間會使用。 – 2011-06-13 05:06:07