Magento 1.9 Admin Form – Multiselect Column Not Saving in Database

adminformadminhtmlgridmagento-1.9

I have created my own module in magento with admin grids.In that admin grid form i have added multiselect options column like,

$fieldset->addField('category_id', 'multiselect', array(
        'name'     => 'category_id[]',
        'label'    => Mage::helper('bcpl_blog')->__('Category Name'),
        'title'    => Mage::helper('bcpl_blog')->__('Category Name'),
        'required' => true,
        'values'    => Mage::getModel("bcpl_blog/category")->getCollection()->toArrayCustom()
    )); 

my collection looks like,

public function toArrayCustom(){
    $data = $this->getData();
    $items = array();
    if(count($data)){
        foreach($data as $item){
             $items[$item['category_id']] = array('label'=>$item['category_name'],'value'=>$item['category_id']);  

        }
    }

    return $items;
}

now 'category_id' is not saving for multiselect.

Best Answer

Grid form inputs don't necessarily auto-save. This is the case with "custom" inputs where the values are derived specially, as in this case.

When you save this form, it goes to the save action, yes? In that save action method, you will need to parse/modify/clean/etc. the category_id POST data.

In your controller save action

$categoryIds = $this->getRequest()->getPost('category_id') // input from the form
// Process $categoryIds
$processedCatIds = implode(',', $categoryIds); // JUST AN EXAMPLE; PROCESS ACCORDINGLY
$object->setCategoryId($processedCatIds);
Related Topic