0

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

  • 1
    You need to add the method toOptionArray in your region model – Marius Nov 4 '13 at 11:27
  • 1
    you are calling getState() instead of getStates() to fill in options. Also in some place you say modulename and someplace mdmt. Post proper code to get proper answer. – Kalpesh Nov 8 '13 at 13:08
  • @Kalpesh Mehta thanks for your reply please see edited code – user1704524 Nov 8 '13 at 15:33
2

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;

    }
1

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

  • Thanks for the reply, the error has now gone! However, there are no values being shown in the dropdown its empty... 'values' => Mage::getSingleton('modulename/region'), – user1704524 Nov 4 '13 at 8:52
  • since the values are being retrieved would that indicate that I need to do a join on one of the tables? – user1704524 Nov 4 '13 at 9:54
  • @user2157 Add the model code in the question. – Marius Nov 4 '13 at 11:19
  • Thanks for your patience, I've add the model and collections code... Hope we can solve this – user1704524 Nov 4 '13 at 11:27
  • Fatal error: Call to a member function toOptionArray() on a non-object – user1704524 Nov 4 '13 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.