Magento News

 

Magento saving multiselect in controller ?

$fieldset->addField('brand_id', 'multiselect', array(
    'label'     => Mage::helper('expertbrand')->__('Merk:'),
    'name'      => 'brand_id[]',
    'values'    => $aOptionsMerk,
));

I have this multiselect box with some 600 options. I would like to know how to save this in the controller?

I have tried everything and worked on this problem for 3 days now. Also on the internet I cannot find a correct anwser to this problem. Hoping someone here is able to help me because I’d really like to know!

My controller code:

public function saveAction() {
    if ($data = $this->getRequest()->getPost()) {

        if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
            try {
                /* Starting upload */
                $uploader = new Varien_File_Uploader('filename');

                // Any extention would work
                $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                $uploader->setAllowRenameFiles(false);

                // Set the file upload mode
                // false -> get the file directly in the specified folder
                // true -> get the file in the product like folders
                // (file.jpg will go in something like /media/f/i/file.jpg)
                $uploader->setFilesDispersion(false);

                // We set media as the upload dir
                $path = Mage::getBaseDir('media') . DS ;
                $uploader->save($path, $_FILES['filename']['name'] );
            } catch (Exception $e) {

            }

            //this way the name is saved in DB
            $data['filename'] = $_FILES['filename']['name'];
        }

        /*
        $brands=$data['brand_id'];
        $t=count($brands);
        $inhoud="";
        $i=1;
        foreach ($brands as $brand){
            if ($t == $i){
                $inhoud.=$brand;
            } else {
                $inhoud.=$brand." , ";
            }
            $i++;
        }
        //echo $inhoud;
        // $br=array('brand_id');
        $br=$inhoud;
        $data['brand_id']=$br;
        */
        //hier moet de loop komen

        $id= $data['expert_id'];
        $db1 = Mage::getSingleton('core/resource')->getConnection('core_write');
        $result = $db1->query("SELECT name FROM expert where expert_id=$id");
        $rows = $result->fetch(PDO::FETCH_ASSOC);
        $data['name']=$rows['name'];
        //$data['brand_id']=$_POST['brand_id'];
        $model = Mage::getModel('expertbrand/expertbrand');
        $model->setData($data)
            ->setId($this->getRequest()->getParam('id'));

        try {
            if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
                $model->setCreatedTime(now())
                ->setUpdateTime(now());
            } else {
                $model->setUpdateTime(now());
            } 

            $model->save();

            //hier is het einde van de loop..
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('expertbrand')->__('Item was successfully saved'));
            Mage::getSingleton('adminhtml/session')->setFormData(false);

            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('id' => $model->getId()));
                return;
            }

            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setFormData($data);
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
    }

    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('expertbrand')->__('Unable to find item to save'));
    $this->_redirect('*/*/');
}

How should I save the optionsArray that is sent through the $_POST? Thanks in advance.

Magento saving multiselect in controller ?

Possibly Related Posts:


 

Leave a Reply