2

Synopsis

I am attempting to create a drop-down box with JavaScript where by I would like to dump my collection results through toOptionHash, however all i am getting is an array of null values.

I will be encoding to JSON and setting the response body however this is outside the scope of this question.

$result = Mage::getModel('cutting/plate_thickness')->getCollection()
    ->addFieldToFilter('main_table.spec_id', $specId);

Zend_Debug::dump( $result->toOptionHash('id', 'size') );

This outputs:

array(41) {
  [1] => NULL
  [2] => NULL
    ... snip ...
  [40] => NULL
  [41] => NULL
}

Although I could do the following - I want to make use of the toOptionHash which is what it exists for?

$result = Mage::getModel('cutting/plate_thickness')->getCollection()
    ->addFieldToFilter('main_table.spec_id', $specId);

$data = [];
foreach ($result as $record) {
    $data[$record->getData('id')] = $record->getData('size');
}
  • does Zend_Debug::dump($result->getData()); show the proper data, if so double check indexes, like id being entity_id, etc. – B00MER Aug 1 '14 at 16:34
  • The odd thing is those keys in my array are accurate to those of the id column in the table (also, they don't start from 0, which indicates this is no coincidence) yet the values (of the size field) are all NULL. – ash Aug 2 '14 at 10:25
  • disable community and local code pools and test, possibly something overriding it? – B00MER Aug 4 '14 at 15:47
2

El Conclusion

After further debugging something stood out like a sore thumb which originally I overlooked, that was the broken core functionality as per-usual:

Eventually Varien_Db_Data_Collection is inherited so when I execute toOptionArray or toOptionHash NO arguments are parsed thus why NULL is apparent

Solution 1

Is to ensure that you only use id as your id field and name as your value field.

Solution 2

To extend Mage_Core_Model_Resource_Db_Collection_Abstract implementing the toOptionHash and toOptionArray methods to override those defined within Varien_Db_Data_Collection:

public function toOptionArray($idField = 'id', $valueField = 'name', $additional = [])
{
    return $this->_toOptionArray($idField, $valueField, $additional);
}

public function toOptionHash($idField = 'id', $valueField = 'name', $additional = [])
{
    return $this->_toOptionHash($idField, $valueField, $additional);
}

This is the snippet from lib/Varien/Db/Data/Collection.php to highlight the issue discovered.

protected function _toOptionArray($valueField='id', $labelField='name', $additional=array())
{
    $res = array();
    $additional['value'] = $valueField;
    $additional['label'] = $labelField;

    foreach ($this as $item) {
        foreach ($additional as $code => $field) {
            $data[$code] = $item->getData($field);
        }
        $res[] = $data;
    }
    return $res;
}

public function toOptionArray()
{
    return $this->_toOptionArray();
}

public function toOptionHash()
{
    return $this->_toOptionHash();
}

protected function _toOptionHash($valueField='id', $labelField='name')
{
    $res = array();
    foreach ($this as $item) {
        $res[$item->getData($valueField)] = $item->getData($labelField);
    }
    return $res;
}

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.