Magento 1.8 – Save Multiselect Box Value to CMS Table

attributescmsmagento-1.8

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

Best Answer

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());

Related Topic