Magento图像上传文件名不保存在数据库中

时间:2013-08-06 23:08:15

标签: php mysql database magento

我正在尝试将其他图片上传字段添加到图库扩展程序中。扩展控制器的代码如下。我在数据库中添加了一个名为galpic_popup_image的额外列,其值与现有的galpic_image列相同。

我设法让上传工作,因此图像保存在服务器上,但是它没有将文件名保存在数据库中,因此我无法在前端调用它。

class ParadoxLabs_Gallery_Adminhtml_GalleryController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->_setActiveMenu('cms/gallery');
        $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Gallery'), Mage::helper('adminhtml')->__('Gallery'));

        $this->renderLayout();
    }

    public function editAction()
    {
        $this->loadLayout();
        $this->_setActiveMenu('cms/gallery');
        $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Gallery'), Mage::helper('adminhtml')->__('Gallery'));

        $this->_addContent($this->getLayout()->createBlock('gallery/adminhtml_gallery_edit'));
        $this->renderLayout();
    }

    public function newAction()
    {
        $this->editAction();
    }

    public function saveAction()
    {
        if ( $this->getRequest()->getPost() ) {
            // Image uploading code modified from https://magento2.atlassian.net/wiki/spaces/m1wiki/pages/14024884/How+to+create+an+image+or+video+uploader+for+the+Magento+Admin+Panel
            if(isset($_FILES['image']['name']) && (file_exists($_FILES['image']['tmp_name'])))
            {
                try{
                    $uploader = new Varien_File_Uploader('image');
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    $path = Mage::getBaseDir('media') . DS . 'p_gallery' . DS ;
                    $uploader->save($path, $_FILES['image']['name']);
                    $data['image'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'p_gallery/'. $_FILES['image']['name'];
                }catch(Exception $e){}
            }
            else
            {
                if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1){                   
                    $data['image_main'] = '';
                }
                else{
                    unset($data['image']);
                }
            }

            if(isset($_FILES['popup_image']['name']) && (file_exists($_FILES['popup_image']['tmp_name'])))
            {
                try{
                    $uploader = new Varien_File_Uploader('popup_image');
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    $path = Mage::getBaseDir('media') . DS . 'p_gallery' . DS ;
                    $uploader->save($path, $_FILES['popup_image']['name']);
                    $popupdata['popup_image'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'p_gallery/'. $_FILES['popup_image']['name'];
                }catch(Exception $e){}
            }
            else
            {
                if(isset($popupdata['fileinputname']['delete']) && $popupdata['fileinputname']['delete'] == 1){                 
                    $popupdata['popup_image_main'] = '';
                }
                else{
                    unset($popupdata['popup_image']);
                }
            }

            try {
                if( intval($this->getRequest()->getParam('id')) == 0 ) { // New
                    $model = Mage::getModel('gallery/gallery')
                        ->setGalpicId   ( $this->getRequest()->getParam('id')    )
                        ->setGalpicStore( $this->getRequest()->getParam('store') )
                        ->setGalpicDate ( date('Y-m-d', time()) )
                        ->setGalpicName ( $this->getRequest()->getParam('name')  )
                        ->setGalpicImage( $data['image']                         )
                        ->setGalpicPopupImage( $popupdata['popup_image']                         )
                        ->save();
                }
                else { // Edit
                    $model = Mage::getModel('gallery/gallery')
                        ->setGalpicId   ( $this->getRequest()->getParam('id')    )
                        ->setGalpicStore( $this->getRequest()->getParam('store') )
                        ->setGalpicName ( $this->getRequest()->getParam('name')  )
                        ->setGalpicImage( $data['image']                         )
                        ->setGalpicPopupImage( $popupdata['popup_image']                         )
                        ->save();
                }

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Galpic was successfully saved'));

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

    public function deleteAction()
    {
        if( $this->getRequest()->getParam('id') > 0 ) {
            try {
                $model = Mage::getModel('gallery/gallery');
                $model->setGalpicId($this->getRequest()->getParam('id'))
                    ->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Galpic was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }

    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('cms/gallery');
    }
}

以下是用于捕获数据的表单:

class ParadoxLabs_Gallery_Block_Adminhtml_Gallery_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
            $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
        }
    }

    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
                'enctype' => 'multipart/form-data',
                'id' => 'edit_form',
                'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                'method' => 'post',
            ));

        $fieldset = $form->addFieldset('gallery_form', array(
            'legend'      => Mage::helper('gallery')->__('Galpic'),
            'class'     => 'fieldset-wide'
          )
        );

        $fieldset->addField('galpic_name', 'text', array(
            'name'      => 'name',
            'label'     => Mage::helper('gallery')->__('Name'),
            'class'     => 'required-entry',
            'required'  => true,
        ));

        $fieldset->addField('galpic_image', 'image', array(
            'name'      => 'image',
            'label'     => Mage::helper('gallery')->__('Image'),
            'class'     => 'required-entry',
            'required'  => true,
        ));
        $fieldset->addField('galpic_popup_image', 'image', array(
            'name'      => 'popup_image',
            'label'     => Mage::helper('gallery')->__('Popup Image'),
            'class'     => 'required-entry',
            'required'  => true,
        ));

        $fieldset->addField('galpic_store', 'select', array(
            'name'      => 'store',
            'label'     => Mage::helper('core')->__('Store View'),
            'title'     => Mage::helper('core')->__('Store View'),
            'required'  => true,
            'values'    => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
        ));

        if (Mage::registry('gallery')) {
          $form->setValues(Mage::registry('gallery')->getData());
        }

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

1 个答案:

答案 0 :(得分:1)

好的,我修好了!

有人建议确保列与控制器保存项目的顺序相同,所以我这样做了。它似乎没有效果(但它可能已经完成)。

我将第二个图片上传代码更改为以下内容(基本上从Kiatng建议的$ popupdata中删除了'popup'):

if(isset($_FILES['popup_image']['name']) && (file_exists($_FILES['popup_image']['tmp_name'])))
            {
                try{
                    $uploader = new Varien_File_Uploader('popup_image');
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    $path = Mage::getBaseDir('media') . DS . 'p_gallery' . DS ;
                    $uploader->save($path, $_FILES['popup_image']['name']);
                    $data['popup_image'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'p_gallery/'. $_FILES['popup_image']['name'];
                }catch(Exception $e){}
            }
            else
            {
                if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1){                   
                    $data['image_main'] = '';
                }
                else{
                    unset($data['popup_image']);
                }
            }