簡體   English   中英

Magento 管理員編輯表單字段 - 自定義模型字段

[英]Magento Admin Edit Form Fields - Custom Model Field(s)

想要添加要在自定義 Magento 管理表單中呈現的自定義模型。 似乎無法獲得源模型來呈現任何選項。 在谷歌上找不到任何東西,因為它主要與系統/配置源模型示例有關。 看下面的代碼

模型文件 (My/Module/Model/MyModel.php)

<?php
class My_Module_Model_MyModel extends Mage_Core_Model_Abstract
{
 static public function getOptionArray()
{

    $allow = array(
       array('value' => '1', 'label' => 'Enable'),
       array('value' => '0', 'label' => 'Disable'),
   );

   return $allow;
}
}

和我的表單選項卡文件 - 選項卡顯示多選字段,但它是空白的(My/Module/Block/Adminhtml/Module/Edit/Tab/Data.php)

<?php

class My_Module_Block_Adminhtml_Module_Edit_Tab_Data extends Mage_Adminhtml_Block_Widget_Form
{

protected function _prepareForm(){ 


    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('module_form', array('legend'=>Mage::helper('module')->__('Module Information')));
    $object = Mage::getModel('module/module')->load( $this->getRequest()->getParam('module_id') );
    

    echo $object;
    
    
   
    $fieldset->addField('module_enabled', 'multiselect', array(
      'label'     => Mage::helper('module')->__('Allowed Module'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'module_enabled',
      'source_model' => 'My_Module_Model_MyModel',
      'after_element_html' => '<small>Select Enable to Allow</small>',
      'tabindex' => 1
    ));
    
    
    if ( Mage::getSingleton('adminhtml/session')->getModuleData() )
    {
          $form->setValues(Mage::getSingleton('adminhtml/session')->getModuleData());
        Mage::getSingleton('adminhtml/session')->setModuleData(null);
    } elseif ( Mage::registry('module_data') ) {
        $form->setValues(Mage::registry('module_data')->getData());
    }
    return parent::_prepareForm();
   }


  }

所以我有其他字段、選項卡,它們都保存數據等,但無法使用多選字段內的自定義模型獲取要呈現的值。

看起來源模型中的方法名稱不正確。 此外,您可能不需要在源模型中擴展Mage_Core_Model_Abstract

嘗試這個:

<?php
class My_Module_Model_MyModel
{
    public function toOptionArray()
    {
        return array(
            array('value' => '1', 'label' => Mage::helper('module')->__('Enable')),
            array('value' => '0', 'label' => Mage::helper('module')->__('Disable')),
        );
    }
}

OP 的解決方案從問題遷移到了答案:

更新 MyModel.php 以獲取集合中的 foreach(例如 CMS 頁面)

 <?php class My_Module_Model_MyModel { public function toOptionArray($withEmpty = false) { $options = array(); $cms_pages = Mage::getModel('cms/page')->getCollection(); foreach ($cms_pages as $value) { $data = $value->getData(); $options[] = array( 'label' => ''.$data['title'].'('.$data['identifier'].')', 'value' => ''.$data['identifier'].'' ); } if ($withEmpty) { array_unshift($options, array('value'=>'', 'label'=>Mage::helper('module')->__('-- Please Select --'))); } return $options; }

在 My/Module/Block/Adminhtml/Module/Edit/Tab/Data.php 我剛剛刪除了“source_model”並將其替換為

'values' => Mage::getModel('module/mymodel')->toOptionArray(),

只是補充一下,還存在多選值的問題,即在編輯頁面上的刷新/保存時不保存/更新多選字段。 為了使其正常工作,我編輯了 saveAction(或保存表單數據的操作名稱)下的管理控制器。 在位於 My/Module/controllers/Adminhtml/ModuleController.php 的 admin/backend 控制器中,請參閱下面我的 saveAction

 public function saveAction() { $model = Mage::getModel('module/module'); if ($data = $this->getRequest()->getPost()) { $model = Mage::getModel('module/module'); $model->setData($data) ->setModuleId($this->getRequest()->getParam('module_id')); try { if ($model->getCreatedTime() == NULL || $model->getUpdateTime() == NULL) { $model->setCreatedTime(now())->setUpdateTime(now()); } else { $model->setUpdateTime(now()); } $ModuleEnabled = $this->getRequest()->getParam('module_enabled'); if (is_array($ModuleEnabled)) { $ModuleEnabledSave = implode(',',$this->getRequest()->getParam('module_enabled')); } $model->setModuleEnabled($ModuleEnabledSave); //save form data/values per field $model->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('module')->__('Item was successfully saved')); Mage::getSingleton('adminhtml/session')->setFormData(false); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('module_id' => $model->getModuleId())); return; } $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); Mage::getSingleton('adminhtml/session')->setFormData($data); $this->_redirect('*/*/edit', array('module_id' => $this->getRequest()->getParam('module_id'))); return; } } Mage::getSingleton('adminhtml/session')->addError(Mage::helper('module')->__('Unable to find item to save')); $this->_redirect('*/*/'); }

這會將內爆數組(即 2, 3 ,6, 23, 28,)保存到數據庫值中,並在刷新/更新/保存的相應選項卡上呈現選定的多選字段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM