Question

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...

Was it helpful?

Solution

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

            $this->addColumn('state', array(
            'header' => Mage::helper('modulename')->__('State'),
            'index' => 'state',
            'type' => 'options',
            'options'=> Mage::getModel('modulename/customerstate')->getStates()
            ));

Model/Customerstate.php

  public function getStates() {

        $statesArray = array();
        foreach($this->getCollection() as $state){
            $statesArray[$state->getId()] = $state->getState();

        }
        return $statesArray;

    }

OTHER TIPS

At a first glance you seam to be on the right track.
The problem, as the error suggests, is this:

Mage::getSingleton('my/modulename')

Most probably this returns null.
From the code you posted I can conclude that My is your namespace and Module is the module name (Duh!).
I this the code above should be:

Mage::getSingleton('module/[entity_name_here]');

where [entity_name_here] should be the alias for your region model

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top