tags:

views:

56

answers:

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

A: 

To start:

  • You've declared the input as "brand_id[]", so you are looking for $data['brand_id'], which should be an array. In the commented code, you appear to use that data, but your current code only looks for $data['expert_id'], which doesn't seem to be the same thing.

  • Even if you change that line to $data['brand_id'], keep in mind that it is an array, so you'll need to be cognizant of that in queries.

  • You've got a bunch of logic in the controller that doesn't belong there. In an MVC app (of which Magento is a canonical example), most of that SQL logic (as well as the createdtime/updatedtime stuff) belongs in the model (probably expertbrand/expertbrand).

  • It's not clear how you defined expertbrand/expertbrand. Is this an EAV model? What is its definition? This is most likely the root of your problem, as you have to tell Magento about your EAV model for it to save like that.

Please fix those things (and clarify on the model code) and we can debug further if necessary.

Thanks, Joe

Joseph Mastey
A: 

its clear to me that the data is sent in an array. In order to fix that i created function that rewrites the array.

$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;

the DATA saved in the database would look something like this:

104 , 106 , 107 , 108

i do this cos i read somewhere that this was necessary to do so.

How ever when i open my edit field the only one which shows :

selected="selected" is the brand with the value 108 (the last one)

this is a problem because i need all 4 of them to be shown as selected.

Has this something to do in the way how i save this data ...not sure if the data should be saved as an array to show all selected="selected" fields in the edit

by the way the expert_id is something else and not an issue here i know the sql should be somewhere else but since i am still learning magento this was a quick a dirty method to fix a problem i had earlier...

all i want to know is how to store the array to show all values as an selected="selected" field in the edit form...Txs....

Ok i fixed the problem the data should be saved as

106,108,114,99

now everything is selected in the edit field strange nothing of this is to be found here on the internet hopefully it will be helpfull to other people dealing with the same problem

Paul de Zwaan