2014-01-10 1 views
0

Magento 백엔드 작업 방법을 배우는 중입니다. 모델 클래스 example/event과 해당 리소스를 만들었습니다. example_event을 작성한 DB 테이블에 레코드를 추가 할 수 있습니다 (Magento 관리 양식을 통해). 테이블에서 레코드를 검색하는 데 문제가 있습니다. My Gird 클래스는 다음과 같습니다.Grid의 Magento getCollection()이 false를 반환합니다.

class MasteringMagento_Example_Block_Adminhtml_Event_Grid extends Mage_Adminhtml_Block_Widget_Grid { 

    protected function _prepareCollection() { 
     $collection = Mage::getModel('example/event')->getCollection(); 
     var_dump($collection); // bool(false) 

     //$record = Mage::getModel('example/event')->load(1); //id from example_event table 
     //var_dump($record); // returns object record as expected 

     $this->setCollection($collection); 

     return parent::_prepareCollection(); 
    } 

    protected function _prepareColumns() { 
     $this->addColumn('name', array(
       'type'=>'text', 
       'index'=>'name', 
       'header'=>$this->__('Name') 
     )); 

     $this->addColumn('start', array(
       'type'=>'date', 
       'index'=>'start', 
       'header'=>$this->__('Start Date') 
     )); 

     $this->addColumn('end', array(
       'type'=> 'date', 
       'index'=>'end', 
       'header'=>$this->__('End Date') 
     )); 

     return $this; 
    } 

} 

$collection = Mage::getModel('example/event')->getCollection()은 false를 반환합니다. 그러나 $record = Mage::getModel('example/event')->load(1);을 사용하여 데이터베이스 테이블에서 하나의 레코드를 검색 할 수 있습니다. 여기서 1은 레코드의 ID입니다 (이것은 적어도 내가 작성한 내용이 DB와 ​​통신 할 수 있는지 확인하기위한 위생 검사입니다). 목표는 _prepareColumns() 함수로 작성된 표에 콜렉션을 렌더링하는 것입니다.

다시 Magento 백엔드에서 프로그래밍 할 때 새로운 점이 있지만 계속해서 코드를 반복적으로 읽었는데 왜 내 컬렉션 객체가 비어 있는지 파악할 수 없습니다.

+0

그래서 (1)을로드 할 수 있지만 모든 컬렉션을로드 할 수 없습니까? – sergio

+0

먼저 'var/log' 폴더에서 오류를 확인하십시오. 그런 다음이 파일이 있는지 확인하십시오 :'MasteringMagento/Example/Model/Resource/Event/Collection.php'. 그렇다면 config.xml과'MasteringMagento/Example/Model/Event.php'의 내용을 질문에 게시하십시오. – Marius

+0

@sergio가 맞습니다. – bar1024

답변

4

당신이 할 수있는 경우 :

$record = Mage::getModel('example/event')->load(1) 

하지만 당신은 할 수 없습니다

Mage::getModel('example/event')->getCollection(); 

이 당신 did not creat collection model in your module의 주된 이유. 다음과 같이해야합니다.

class MasteringMagento_Example_Model_Resource_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {  
    protected function _construct() { 
     $this->_init('example/event'); 
    }  
} 
+0

및 @marius 감사합니다. 감사합니다. 그거였다. 'MasteringMagento/Example/Model/Resource/Event/Collection.php'에'MasteringMagento_Example_Model_Resource_Event_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract' 클래스가 필요했습니다. Magento에서 모델을 사용하는 방법에 대해 더 많이 이해하고 있습니다. 놀랍게도 내가 작업하고있는 자습서는 Collection Abstract에 대한 언급이 없습니다. 새로운 튜토리얼이 필요합니다. – bar1024

+0

큰 도움이됩니다. – sergio

관련 문제