2015-06-03 53 views
2

我是一個初學者的Magento所以請多多包涵...... 我創造了我的網站自定義字段中adminhtml添加到我的標籤的簡單延伸。自定義字段只是一個數字,我需要標識特定的Z塊(cms塊擴展名),以便我可以將它作爲小部件訪問並在標籤「類別」的前端顯示它。Magento的saveAction - 初學者

我已經創建了一個自定義模塊,它正在工作:我使用$ fieldset在表單中設置了一個字段並擴展了TagController.php,兩者都在使用中(我做了一個簡單的試驗,看他們是否有被認可)。不過,我不知道如何去了解我的自定義字段保存到數據庫(是否修改saveAction是不夠的,我還沒有做它正確,或者如果我需要添加安裝自定義模式或SQL)。

對不起,對於「基本」的問題,但我是新來的,並且主要完成了前端開發(所以我的擴展知識僅僅是有限的)。

謝謝你的人誰可以幫助... 克勞迪婭

新的標記FORM:

public function __construct() 
{ 
    parent::__construct(); 
    $this->setId('tag_form'); 
    $this->setTitle(Mage::helper('tag')->__('Block Information')); 
} 

/** 
* Prepare form 
* 
* @return Mage_Adminhtml_Block_Widget_Form 
*/ 
protected function _prepareForm() 
{ 
    $model = Mage::registry('tag_tag'); 

    $form = new Varien_Data_Form(
     array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post') 
    ); 

    $fieldset = $form->addFieldset('base_fieldset', 
     array('legend'=>Mage::helper('tag')->__('General Information'))); 

    if ($model->getTagId()) { 
     $fieldset->addField('tag_id', 'hidden', array(
      'name' => 'tag_id', 
     )); 
    } 

    $fieldset->addField('form_key', 'hidden', array(
     'name' => 'form_key', 
     'value' => Mage::getSingleton('core/session')->getFormKey(), 
    )); 

    $fieldset->addField('store_id', 'hidden', array(
     'name' => 'store_id', 
     'value' => (int)$this->getRequest()->getParam('store') 
    )); 

    $fieldset->addField('name', 'text', array(
     'name' => 'tag_name', 
     'label' => Mage::helper('tag')->__('Tag Name'), 
     'title' => Mage::helper('tag')->__('Tag Name'), 
     'required' => true, 
     'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'), 
    )); 

    $fieldset->addField('zblock', 'text', array(
     'name' => 'zblock_id', 
     'label' => Mage::helper('tag')->__('Z-Block Id'), 
     'title' => Mage::helper('tag')->__('Z-Block Id'), 
     'required' => true, 
     'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'), 
    )); 

    $fieldset->addField('status', 'select', array(
     'label' => Mage::helper('tag')->__('Status'), 
     'title' => Mage::helper('tag')->__('Status'), 
     'name' => 'tag_status', 
     'required' => true, 
     'options' => array(
      Mage_Tag_Model_Tag::STATUS_DISABLED => Mage::helper('tag')->__('Disabled'), 
      Mage_Tag_Model_Tag::STATUS_PENDING => Mage::helper('tag')->__('Pending'), 
      Mage_Tag_Model_Tag::STATUS_APPROVED => Mage::helper('tag')->__('Approved'), 
     ), 
     'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'), 
    )); 

    $fieldset->addField('base_popularity', 'text', array(
     'name' => 'base_popularity', 
     'label' => Mage::helper('tag')->__('Base Popularity'), 
     'title' => Mage::helper('tag')->__('Base Popularity'), 
     'after_element_html' => ' ' . Mage::helper('tag')->__('[STORE VIEW]'), 
    )); 

    if (!$model->getId() && !Mage::getSingleton('adminhtml/session')->getTagData()) { 
     $model->setStatus(Mage_Tag_Model_Tag::STATUS_APPROVED); 
    } 

    if (Mage::getSingleton('adminhtml/session')->getTagData()) { 
     $form->addValues(Mage::getSingleton('adminhtml/session')->getTagData()); 
     Mage::getSingleton('adminhtml/session')->setTagData(null); 
    } else { 
     $form->addValues($model->getData()); 
    } 

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

新的控制器:

public function saveAction() 
{ 
    if ($postData = $this->getRequest()->getPost()) { 
     if (isset($postData['tag_id'])) { 
      $data['tag_id'] = $postData['tag_id']; 
     } 

     $data['name']    = trim($postData['tag_name']); 
     $data['zblock']    = $postData['zblock_id']; 
     $data['status']    = $postData['tag_status']; 
     $data['base_popularity'] = (isset($postData['base_popularity'])) ? $postData['base_popularity'] : 0; 
     $data['store']    = $postData['store_id']; 

     if (!$model = $this->_initTag()) { 
      Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Wrong tag was specified.')); 
      return $this->_redirect('*/*/index', array('store' => $data['store'])); 
     } 

     $model->addData($data); 

     if (isset($postData['tag_assigned_products'])) { 
      $productIds = Mage::helper('adminhtml/js')->decodeGridSerializedInput(
       $postData['tag_assigned_products'] 
      ); 
      $model->setData('tag_assigned_products', $productIds); 
     } 

     try { 
      $model->save(); 
      Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The tag has been saved.')); 
      Mage::getSingleton('adminhtml/session')->setTagData(false); 

      if (($continue = $this->getRequest()->getParam('continue'))) { 
       return $this->_redirect('*/tag/edit', array('tag_id' => $model->getId(), 'store' => $model->getStoreId(), 'ret' => $continue)); 
      } else { 
       return $this->_redirect('*/tag/' . $this->getRequest()->getParam('ret', 'index')); 
      } 
     } catch (Exception $e) { 
      Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); 
      Mage::getSingleton('adminhtml/session')->setTagData($data); 

      return $this->_redirect('*/*/edit', array('tag_id' => $model->getId(), 'store' => $model->getStoreId())); 
     } 
    } 

    return $this->_redirect('*/tag/index', array('_current' => true)); 

} 

我想自定義字段添加是「zblock」......謝謝,再次,忍耐着我! :)

+0

請考慮添加一些代碼。它會幫助你理解並幫助你 –

+0

對,我的錯誤:)我會編輯我的文章,謝謝你的建議! –

回答

0

首先添加數據庫表中的字段。 例如,如果您想添加自定義表格。

ALTER TABLE myCustomModuleTable ADD COLUMN 'myCustomField' int(10); 

Thenafter,在您的控制器操作中,獲取該表的模型對象並設置字段。

如果要添加到現有錶行數據:

$value = 6; 
    $rowInWhichIWantToSave = Mage:getModel('companyname/modulename')->load($rowId); 
    $rowInWhichIWantToSave->setData('myCustomField',$value)->save(); 

如果要添加新行:

$value = 6; 
$rowInWhichIWantToSave = Mage:getModel('companyname/modulename'); 
$rowInWhichIWantToSave->setData('myCustomField',$value)->save(); 

希望這有助於!

+0

請接受和upvote如果它幫助的答案! –