0
votes

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.

1
So, you can load(1), but you cannot load all collection? - sergio
First, check the var/log folder 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 and MasteringMagento/Example/Model/Event.php in your question. - Marius
@sergio that is correct. - bar1024
@Marius I have MasteringMagento/Example/Model/Resource/Event.php. I am missing MasteringMagento/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

1 Answers

4
votes

If you can:

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

But you can not

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

The main reason of this that you did not creat collection model in your module. Need to do something like this:

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