2011-06-13 2 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** 

어떤 아이디어가 발생할 수 있습니까? 은 $ name 변수는 컨트롤러의 이름이 아닌 이름 컨트롤러가 사용하는 모델입니다

+0

테이블 비디오를 제거하면 "오류 : 모델 비디오의 데이터베이스 테이블 비디오를 찾을 수 없습니다."오류가 발생합니다. –

답변

1

당신의 코드가 올바른 것으로 보인다하려고하지만, CakePHP의 모델을로드하려고하지 않는 것처럼 보인다 올바른 장소에서. 컨트롤러에서 $ uses 변수를 사용하여로드 할 모델을 지정할 수 있습니다.

플러그인을 사용 중이므로 모델 이름 앞에 플러그인 이름을 추가해야합니다.

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

CakePHP는이 문제를 자체적으로 처리해야하지만 일부 이전 버전의 CakePHP에서는 버그가있어 제대로 작동하지 않습니다. 1.3.10에서는 수정 된 것으로 보입니다.

$ uses 변수에 대한 자세한 내용은 http://book.cakephp.org/view/961/components-helpers-and-uses을 참조하십시오.

+0

나는 이것을 체크하고 트리플을 체크했다. 나는 또한 캐시 디렉토리를 삭제하고 디버깅을했다. 모델 파일을 삭제하고 동일한 동작을 얻었습니다. 있는 파일입니다 :( –

+0

은? 당신은 당신의 응용 프로그램 폴더의 전체 경로 상대를 제공 할 수 있습니까? /plugins/forum/controllers/videos_controller.php /plugins/forum/models/video.php –

+0

내가 비슷한 모델을 가지고 나는 기본적으로 복사/붙여 넣기. 테이블의 이름입니다 : 비디오 –

0

,

$uses = "Videos" 
+0

CakePHP는 컨트롤러의 이름에 따라 자동으로 $ uses 변수를 설정합니다. 기본적으로 "컨트롤러"접미어를 제거하고 기본 $ 값을 사용하도록 만듭니다. 더 많거나 다른 모델을로드하고 싶거나 전혀로드하지 않으려는 경우에만 변경하려는 경우가 있습니다. –