Magento管理员编辑表单字段 - 自定义模型字段

3

想要在自定义的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;
}
}

我的表单选项卡文件 - Tab 显示了多选字段,但是它是空的(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();
   }


  }

我有其他字段、选项卡等,它们都保存了数据,但是我无法使用多选字段中的自定义模型呈现值。


http://stackoverflow.com/questions/14056140/magento-saving-multiselect-array-on-custom-model 这个也很有用。 - BENN1TH
2个回答

3

看起来源模型中的方法名不正确。此外,您可能不需要在源模型中扩展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')),
        );
    }
}

并将“source_model”更改为'values' => Mage :: getModel('module / mymodel')-> toOptionArray(), - BENN1TH

0

楼主的解决方案已从问题迁移到答案中:

updated the MyModel.php to get a foreach in a collection (CMS Pages for example)

<?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;
}

and within My/Module/Block/Adminhtml/Module/Edit/Tab/Data.php I just removed "source_model" and replaced it with

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

Just to add, also had the issue of multiselect values not saving/updating the multiselect field on refresh/save on the edit page. To get this working, I edited the admin controller under the saveAction (or the action name to save the form data). See below my saveAction in the controller for the admin/backend located in My/Module/controllers/Adminhtml/ModuleController.php

 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('*/*/');
    }

This saves an imploded array (ie 2, 3 ,6, 23, 28,) into the database value and renders the selected multiselect fields on the corresponding tab on refresh/update/save


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接