I have created a module that is using admin Grid.php. I have created three tables to add and retrieve data but I am unable to get the data from one my models to show inside the dropdown select of Grid.php file.
The problem I have is that for some reason I cannot get the data from on table to show in a dropdown select.
Please see code below:
$this->addColumn("region", array(
"header" => Mage::helper("modulename")->__("Region"),
"type" => 'options',
'options'=> Mage::getModel('modulename/region')->getRegions()
));
The above adds a Region column to the grid and gets data from my model
Model/Region.php
protected function _construct(){
$this->_init("modulename/region");
}
public function getRegions() {
$regionsArray = array();
foreach($this->getCollection() as $region){
$regionsArray[$region->getId()] = $region->getTitle();
}
return $regionsArray;
}
However the following column is what is causing the issue
$this->addColumn('state', array(
'header' => Mage::helper('modulename')->__('State'),
'index' => 'state',
'type' => 'options',
'options'=> Mage::getModel('modulename/customerstate')->getState()
));
I can't seem to get the data from my model to show inside the options
protected function _construct(){
$this->_init("modulename/customerstate");
}
public function getStates() {
$statesArray = array();
foreach($this->getCollection() as $state){
$statesArray[$state->getId()] = $state->getState();
}
return $statesArray;
}
Customerstate DB Table
+-------+----------+
| s_id | state |
+-------+----------+
| 1 | pending |
| 2 | active |
+-------+----------+
I have no idea why this is happening and could do with some advice to help solve this as I have been going round in circles for hours – I'm not getting an error just an empty select option on the grid…
Best Answer
Thanks to @Kalpesh Mehta and @marius for their contributions,
I solved the error with below code as mentioned in the earlier post I was calling
->getState()
instead of->getStates()
Grid.php
Model/Customerstate.php