2015-11-26 53 views
0

只是我的研究延续关于Magento Admin Grid。我试图创建一个文件上传,我成功地做到了。但是,我遇到了有关更新和删除文件上传表单字段的问题。其他输入表单域填充了数据,可以从记录中更新和删除。Magento文件上传不会删除或更新上传的文件

问题:

当尝试更新,文件上传字段不显示上传的文件名的记录,并在尝试更新它,没有删除旧的上载文件,但该文件路径已在记录中更新。

当尝试删除记录时,上载的文件未被删除,但在记录上被删除。

我也有关于电网的小问题。网格不显示记录的ID和其他整数或数字,即使它们在网格中声明。

问:

什么我在我的更新表单字段和网格失踪?

这是我的表单字段的示例。

$fieldset->addField('title', 'text', array(
     'label' => Mage::helper('pmadmin')->__('Matrix Title'), 
     'class' => 'required-entry', 
     'required' => true, 
     'name' => 'title', 
    )); 

    $fieldset->addField('file_path', 'file', array(
     'label' => Mage::helper('pmadmin')->__('File'), 
     'value' => '', 
     'class' => 'required-entry', 
     'required' => true, 
     'disabled' => false, 
     'readonly' => true, 
     'name' => 'file_path', 

     )); 

    $fieldset->addField('short_description', 'text', array(
     'label' => Mage::helper('pmadmin')->__('Short Description'), 
     'class' => 'required-entry', 
     'required' => true, 
     'name' => 'short_description', 
    )); 

这里是我的控制器

public function editAction() 
{ 
    $pmadminId  = $this->getRequest()->getParam('id'); 
    $pmadminModel = Mage::getModel('pmadmin/pmadmin')->load($pmadminId); 

    if ($pmadminModel->getId() || $pmadminId == 0) { 

     Mage::register('pmadmin_data', $pmadminModel); 

     $this->loadLayout(); 
     $this->_setActiveMenu('pmadmin/items'); 

     $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager')); 
     $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News')); 

     $this->getLayout()->getBlock('head')->setCanLoadExtJs(true); 

     $this->_addContent($this->getLayout()->createBlock('pmadmin/adminhtml_pmadmin_edit')) 
      ->_addLeft($this->getLayout()->createBlock('pmadmin/adminhtml_pmadmin_edit_tabs')); 

     $this->renderLayout(); 
    } else { 
     Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pmadmin')->__('Item does not exist')); 
     $this->_redirect('*/*/'); 
    } 
} 

public function newAction() 
{ 
    $this->_forward('edit'); 
} 

public function saveAction() { 
    $post_data=$this->getRequest()->getPost(); 

    if ($post_data) { 
     try {     
    //save file to the destination folder 
      if (isset($_FILES)){ 
       if ($_FILES['file_path']['name']) { 

        $path = Mage::getBaseDir('media') . DS . 'rts' . DS .'pmadmin'.DS; 
        $uploader = new Varien_File_Uploader('file_path'); 
        $uploader->setAllowedExtensions(array('PDF','pdf')); 
        $uploader->setAllowRenameFiles(false); 
        $uploader->setFilesDispersion(false); 
        $destFile = $path.$_FILES['file_path']['name']; 
        $filename = $uploader->getNewFileName($destFile); 
        $uploader->save($path, $filename); 

        $post_data['file_path']='rts/pmadmin/'.$filename; 
       } 
      } 
    //save file path to the database 
      $model = Mage::getModel("pmadmin/pmadmin") 
      ->addData($post_data) 
      ->setId($this->getRequest()->getParam("id")) 
      ->save(); 

      Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("File was successfully saved")); 
      Mage::getSingleton("adminhtml/session")->setPmadminData(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")->setPmadminData($this->getRequest()->getPost()); 
      $this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id"))); 
     return; 
     } 

    } 
    $this->_redirect("*/*/"); 
} 

public function deleteAction() 
{ 
    if($this->getRequest()->getParam('id') > 0) { 
     try { 
      $pmadminModel = Mage::getModel('pmadmin/pmadmin'); 

      $pmadminModel->setId($this->getRequest()->getParam('id')) 
       ->delete(); 

      Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item 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('*/*/'); 
} 

注:

我能够成功地更新其他表单字段和记录的文件路径,但没有上传文件。

回答

0

如果用户没有上传编辑,则必须添加其他语句。 $postdata['file_path']['value']这是由magento自动创建的。

if (isset($_FILES)){ 
       if ($_FILES['file_path']['name']) { 

        $path = Mage::getBaseDir('media') . DS . 'rts' . DS .'pmadmin'.DS; 
        $uploader = new Varien_File_Uploader('file_path'); 
        $uploader->setAllowedExtensions(array('PDF','pdf')); 
        $uploader->setAllowRenameFiles(false); 
        $uploader->setFilesDispersion(false); 
        $destFile = $path.$_FILES['file_path']['name']; 
        $filename = $uploader->getNewFileName($destFile); 
        $uploader->save($path, $filename); 

        $post_data['file_path']='rts/pmadmin/'.$filename; 
       } 
    else { 
       $postdata['file_path']=$postdata['file_path']['value']; 
      } 
    }