I have created custom collection for my data and now I want to display the data in the admin grid using collection.Can you please help me out ?
Here is my code.
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "Select * from TABLE_NAME";
$rows = $connection->fetchAll($sql);
$data = new Varien_Object();
$data->setData($rows);
$collection_of_things = new Varien_Data_Collection();
$collection_of_things->addItem($data);
$this->setCollection($collection_of_things);
return parent::_prepareCollection();
I am getting the blank admin grid for it.
Here is my _prepareColumns function
protected function _prepareColumns() {
$this->addColumn('Field 1', array(
'header' => 'ID',
'align' =>'center',
'width' => '10px',
'index' => 'field_1'
));
$this->addColumn('field_2', array(
'header' => 'Field 2',
'align' =>'left',
'width' => '50px',
'index' => 'field_2'
));
$this->addColumn('field_3', array(
'header' => 'Field 3',
'align' =>'left',
'width' => '50px',
'index' => 'field_3'
));
$this->addColumn('field_4', array(
'header' => 'Field 4',
'align' =>'left',
'width' => '50px',
'index' => 'field_4',
'filter_index' => 'field_4',
'type' => 'datetime'
));
$this->addColumn('field_5', array(
'header' => 'Field 5',
'align' =>'center',
'width' => '50px',
'index' => null,
'renderer' => 'Namespace_MyModule_Block_Adminhtml_Template_Grid_Renderer_Image',
'sortable' => false,
'filter' => false,
'is_system' => true
));
$this->addExportType('*/*/exportCsv', 'CSV');
return parent::_prepareColumns();
}
Best Answer
This is not how you populate a collection. It might work if you actually add each row from your query result as item:
But grids are designed to work with
Varien_Data_Db_Collection
s that were loaded directly from database (and allow filter functionality etc.), so you should follow the Magento practice of defining model, resource model and collection for your custom table and not using plain SQL anywhere.