2011-10-20 41 views
1

我正在創建一個模塊來添加橫幅。現在我想編輯橫幅時遇到問題。Magento - 新模塊編輯操作正在創建一個新項目

問題是,當我點擊「保存」,Magento創建一個新的橫幅,並不會修改原來的橫幅。它發生在我修改某些內容時,如果我不修改任何內容並點擊「保存」。

我有另一個問題。我有一個圖像場,它的工作效果很好,但是當我點擊「編輯」時,它會顯示預覽圖像和刪除複選框,但該字段爲空,如果我保存橫幅,則會將圖像字段留空。

我希望你能幫助我。在此先感謝,如果您需要更多信息,請向我諮詢。

+0

將有問題的代碼添加到您的問題將有極大的幫助。 –

+0

我在這篇文章中附加了一些文件(網格,表單和控制器,它們都很短^^):http://www.magentocommerce.com/boards/viewthread/263717/ 感謝評論(我總是需要編輯這個,我恨,當我點擊「輸入」它發送評論jaja。) – saturno

回答

1

關於保存橫幅廣告,請根據您的情況調整以下代碼: 這裏重要的是在保存時使用註冊表。 檢查保存時是否在$ data中提供橫幅標識。您form.php的必須提供,前形式 - $> setValues方法(...)行添加以下代碼:

$model = Mage::registry('BannerManagement_data'); 
if ($model->getEntityId()) { 
    $fieldset->addField('entity_id', 'hidden', array('name' => 'entity_id'));// or banner_id depends on what id title you gave in your database table 
} 

當然,你必須驗證用戶beforeSave的輸入。在橫幅模型中使用受保護的方法_beforeSave()來實現這些輸入驗證,或直接在保存操作中直接在控制器中執行。

/** 
* Common init to almost all actions 
*/ 
protected function _initAction(){ 
    $this->_title ($this->__("Banner")); 

    $this->loadLayout(); 
    $this->_setActiveMenu('mymenu/banner'); 
    $this->_addBreadcrumb(Mage::helper('banner')->__('Banners'), Mage::helper('banner')->__('Items')); 
    } 

    if(! Mage::registry('current_banner')){ 
     Mage::register('current_banner', Mage::getModel('banner/item')); 
    } 

    $id = $this->getRequest()->getParam('id'); 
    if (!is_null($id)) { 
     $model = Mage::registry('current_banner')->load($id); 

     if (! $model->getId()) { 
      $this->_getSession()->addError(Mage::helper('banner')->__('This banner item no longer exists')); 
      $this->_redirect('*/*/'); 
      return; 
     } 
    } 

    return $this; 
} 


/** 
* Banner edit page 
*/ 
public function editAction(){ 
    $this->_initAction(); 
    $this->_title('Banner Edit'); 

    // 1. Get ID and create model 
    $banner = Mage::registry('current_banner'); 

    // 2. set entered data if there had errors when we do save 
    $data = $this->_getSession()->getBannerData(true); 

    // 3. restore data from SESSION and provide a correct date format 
    if (!empty($data)) { 
     $banner->addData($data); 
    } 

    // 4. Build Edit form 
    $this->_addBreadcrumb(Mage::helper('banner')->__('Edit banner Item'), Mage::helper('banner')->__('Edit Banner Item')); 
    $this->_addContent($this->getLayout()->createBlock('banner/adminhtml_banner')); 
    $this->renderLayout(); 
} 

/** 
* Subscritpion save process 
*/ 
public function saveAction(){ 

    $this->_initAction(); 
    $banner = Mage::registry('current_banner'); 
    $data = $this->getRequest()->getParams(); 

    if ($data) { 

     try { 
      $banner->addData($data); 
      $banner->save(); 

      $this->_getSession()->addSuccess(Mage::helper('banner')->__('The banner item has been saved.')); 

      if ($this->getRequest()->getParam('back', false)) { 
       $this->_redirect('*/*/edit', array('id' => $banner->getId(), '_current'=>true)); 
       return; 
      } 
     } catch (Exception $e) { 
      $this->_getSession()->addError($e->getMessage()); 
      $this->_getSession()->setBannerData($banner->getData()); 
      $this->_redirectUrl($this->getUrl('*/*/edit', array('id' => $banner->getId()))); 
      return; 
     } 
    } 
    $this->_redirectUrl($this->getUrl('*/adminhtml_overview')); 
} 
+0

我已經嘗試與法師::註冊表,但不工作。我不知道那是什麼;) 如何驗證數據?我想用php編程,不用Magento ... – saturno

+0

檢查我寫的關於你的文件form.php,你的橫幅的entity_id是否作爲隱藏值丟失 –

+0

是的,我已經添加了,但不工作。我已經看到,當我進入編輯頁面時,我擁有所有正確的數據和ID。 非常感謝您使用我的時間。 – saturno

相關問題