Magento在自定义模型上保存多重选择值

时间:2013-10-03 10:33:54

标签: magento

我在Magento中创建了一个自定义模型,可以在管理员中进行编辑。但是我在处理数组方面遇到了麻烦。当我去保存模型时,文本字段保存得很好,但是多选字段只保存为“数组”,然后我无法进行编辑。

我需要知道如何在数据库中保存单独的行而不是逗号分隔。

有人可以帮忙吗?任何帮助非常感谢!!!

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

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

                // 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.'magentothem/vendorlist'.DS ;
                $uploader->save($path, $_FILES['image']['name'] );

            } catch (Exception $e) {

            }

            //this way the name is saved in DB
            $basepath=Mage::getBaseUrl().'media/magentothem/vendorlist/';
            $basepath=str_replace("index.php/","",$basepath);
            $data['image'] = '<img src="'.$basepath.$_FILES['image']['name'].'" width="150" height="100px" alt="" />';
        }

        **$data['productid'] = join("," ,$_POST['productid']);**

        $model = Mage::getModel('vendorlist/vendorlist');       
        $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();
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('vendorlist')->__('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('vendorlist')->__('Unable to find item to save'));
    $this->_redirect('*/*/');
}

1 个答案:

答案 0 :(得分:0)

如果你想在表中使用单独的行,我假设id列不是PK 在这种情况下,您可以尝试:

    $model = Mage::getModel('vendorlist/vendorlist');     
    $productIds = Mage::app()->getRequest()->getParam('productid');
    foreach ($productIds as $productId) {
        $model->setData($data)->setProductid($productId)->setId($this->getRequest()->getParam('id'));

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

            $model->save();
            //remove what is after, put it after the try/catch
        } catch (Exception $e) {
            //handle exception
        }
    }