3

I have added multiselect box attribute to cms page with following code

$fieldset->addField('counts', 'multiselect', array(
        'label'     => Mage::helper('cms')->__('Total Counts'),
        'name'      => 'counts',
        'values'    => array(

            array(
                'value'     => 0,
                'label'     => Mage::helper('cms')->__('One'),
            ),
            array(
                'value'     => 1,
                'label'     => Mage::helper('cms')->__('Two'),
            ),

            array(
                'value'     => 2,
                'label'     => Mage::helper('cms')->__('Three'),
            ),  
            array(
                'value'     => 3,
                'label'     => Mage::helper('cms')->__('FOur'),
            ),                                        
        ),
        'value' => $model->getCounts(),
    ));

When i save, its saved as "Array".Please anyone help me to save the option values to the database..

1 Answer 1

3

The problem is, you need to transform your array to a string before saving it to the database.

This can be done in an observer listening to cms_page_load_after/before to transform string to array and cms_page_save_before/after to transform it back. And after the saving it should be transformed back...

How to use event/observer: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method in the observer you get the $page = $observer->getPage() and then you set $page->setYourAttribute(implode(',', $page->getYourAttribute());

4
  • I just added foreach ($data as $key => $value) { if (is_array($value)) { $data[$key] = implode(',',$this->getRequest()->getParam($key)); } } after $data = $this->getRequest()->getPost() in saveAction(). Now its working fine.
    – Anu
    Commented May 28, 2014 at 8:56
  • 1
    there are a lot of reasons why this is a bad idea, if this is a core hack. Just google "magento core hack" Commented May 28, 2014 at 8:59
  • But i have done this through extending Mage_Adminhtml_Cms_PageController.
    – Anu
    Commented May 28, 2014 at 9:01
  • Then it is ok :-) Commented May 28, 2014 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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