I'm currently in the process of learning how to work with the Magento backend. I have created a model class example/event and a corresponding resource. I am able to add records (via a Magento admin form) to a DB table I have created example_event. I am having trouble however retrieving records from my table. My Gird class is as follows:
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;
}
}
As noted in the code, $collection = Mage::getModel('example/event')->getCollection() returns false. However I am able to retrieve a single record from my database table by using $record = Mage::getModel('example/event')->load(1); where 1 is the ID of the record (this is simply a sanity check to ensure that at least something I wrote can talk to the DB). The goal is to render the collection to the grid built by the _prepareColumns() function.
Again, I am brand new to programming in the Magento backend, but I have been through my code over and over and cannot seem to figure out why my collection object is empty.
var/logfolder for errors. Then, make sure that this file exists:MasteringMagento/Example/Model/Resource/Event/Collection.php. If it does, then post the contents of that file and the contents of config.xml andMasteringMagento/Example/Model/Event.phpin your question. - MariusMasteringMagento/Example/Model/Resource/Event.php. I am missingMasteringMagento/Example/Model/Resource/Event/Collection.php. The guide I am following did not make any mention of such a class, so perhaps that is the issue. I will look further into that. As I said, I'm brand new to it, so thank you for pointing me in the right direction. - bar1024