0

我在 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('*/*/');
}
4

1 回答 1

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
        }
    }
于 2013-10-03T11:02:24.310 回答