【问题标题】:How to call custom Module/Model class in Magento如何在 Magento 中调用自定义模块/模型类
【发布时间】:2016-05-09 06:52:13
【问题描述】:

我必须在 billing.phtml 中调用自定义模块类,它会在结帐页面上显示额外的字段。

但我收到PHP Fatal error: Call to a member function getAllOptions() on a non-object

下面是我的代码 -

文件名 - Business.php

文件路径 - /var/www/html/app/code/community/Partsimple/CustomFields/Model/Primarybusiness

class Partsimple_CustomFields_Model_Primarybusiness_Business extends Mage_Eav_Model_Entity_Attribute_Source_Table
{
    public function getAllOptions()
    {
        if (!$this->_options) {


           $this->_options[] = array(
                    'value' => '',
                    'label' => 'Please Select Primary Business'
            );
            $this->_options[] = array(
                    'value' => 1,
                    'label' => 'End Consumer / DIY'
            );
            $this->_options[] = array(
                    'value' => 2,
                    'label' => 'Service Technician'
            );
            $this->_options[] = array(
                    'value' => 3,
                    'label' => 'Other Professional'
            );

         //   $this->_options = Mage::getResourceModel('directory/region_collection')->load()->toOptionArray();

        }
        return $this->_options;
    }
}

**文件名 - **config.xml****

<config>
    <modules>
        <Partsimple_CustomFields>
            <version>1.0.1</version>
        </Partsimple_CustomFields>
    </modules>
    <global>
        <models>
            <Partsimple_CustomFields>
                <class>Partsimple_CustomFields_Model</class>
            </Partsimple_CustomFields>
        </models>
        <fieldsets>
            <customer_account>
            <vendor_account>
                <create>1</create>
                <update>1</update>
            </vendor_account>
            </customer_account>
            <customer_address>
            <vendor_account>
                <to_quote_address>*</to_quote_address>
            </vendor_account>
            <primary_business>
                    <to_quote_address>*</to_quote_address>
            <primary_business>
            </customer_address>
            <sales_convert_order_address>
            <vendor_account>
                <to_quote_address>*</to_quote_address>
                </vendor_account>
            </sales_convert_order_address>
            <sales_convert_quote_address>
                <vendor_account>
                    <to_order_address>*</to_order_address>
                    <to_customer_address>*</to_customer_address>
                </vendor_account>
                <primary_business>
                    <to_order_address>*</to_order_address>
                    <to_customer_address>*</to_customer_address>
                </primary_business>
            </sales_convert_quote_address>
        </fieldsets>
    </global>   
</config>

我在 billing.phtml 文件中写下一行来调用上面的business.php 类方法getAlloptions();

<?php  echo Mage::getModel('partsimple/customFields_model_primarybusiness_business')->getAllOptions();?>

我也试过打印类

<?php  echo "Class: ".get_class(Mage::getModel('partsimple/customfields_model_primarybusiness_business'));?>

但我什么也得不到。 请让我知道我在哪里做错了, 我想在 billing.phtml 上调用这个方法。

更新

在 config.xml 文件中进行更正后,它工作正常。

要检查config.xml 中的错误,请在> 任何编辑器中编辑后在浏览器中打开此文件,以便显示错误。

现在该属性显示结帐页面和管理客户页面以及客户我的帐户部分,但在此部分中。我无法更新它。 config.xml fot customer_account 标签中需要添加哪些字段。

我在config.xml中添加了这样的

<customer_account>
   <primary_business>
       <create>1</create>
         <update>1</update>
   </primary_business>
</customer_account>

【问题讨论】:

  • 你当然想要:Mage::getModel('partsimple_customFields/primarybusiness_business')
  • 是的,我想回显那个类方法数组

标签: magento magento-1.7 magento-1.9


【解决方案1】:

您是否将此文件添加到名为 Partsimple_CustomFields.xml 的 app/etc/modules 中:

<?xml version="1.0" encoding="UTF-8"?>    
<config>
    <modules>
        <Partsimple_CustomFields>
            <active>true</active>
            <codePool>community</codePool>
        </Partsimple_CustomFields>
    </modules>
</config>

【讨论】:

  • 是的,我已经添加了这个 xml。
  • 我试图打印 但仍然没有上我的课。我认为类没有实例化你能告诉我如何实例化这个类吗?
  • 您尝试在某事上执行“getAllOptions()”,您能否在此“某事”上尝试 echo、die、var_dump 或类似的操作并告诉我?
  • 是的,我试图回应这种方法。但我收到 PHP 致命错误:调用非对象上的成员函数 getAllOptions()
  • 我认为问题是你在做的时候什么都没有: echo "Class: ".get_class(Mage::getModel('partsimple/customfields_model_primarybusiness_business'));因为它是空的,所以你“调用非对象上的成员函数 getAllOptions()”问题是:为什么这个类是空的?
【解决方案2】:

您已经说过 magento 将在 config.xml 中使用“Partsimple_CustomFields”调用您的模型,因此您应该始终使用名称“Partsimple_CustomFields”来调用您的模型,如下所示:

<?php  echo Mage::getModel('Partsimple_CustomFields/primarybusiness_business')->getAllOptions();?>

【讨论】:

    【解决方案3】:

    您需要查看您的&lt;models&gt; 节点。您将模型分组为 Partsimple_CustomFields,但您正尝试使用此 Mage::getModel('partsimple/customFields_model_primarybusiness_business') 实例化您的类。您需要更改模型节点中的第一个节点,如下所示

        <models>
            <partsimple>
                <class>Partsimple_CustomFields_Model</class>
            </partsimple>
        </models>
    

    或者你需要像这样实例化你的模型:

    Mage::getModel('Partsimple_CustomFields/customfields_model_primarybusiness_business')
    

    &lt;models&gt;, &lt;blocks&gt;, and &lt;helpers&gt; 内的第一个节点是您的分组节点。当 Magento 试图确定您打算实例化的类时,会调用 Mage_Core_Model_Config::getGroupedClassName($groupType, $classId, $groupRootNode) 将 Partsimple_CustomFields/customfields_model_primarybusiness_business 转换为实际的类名。它会检查您的 config.xml。

    我还应该提到,在模型、块和辅助节点中根本不使用任何大写字母是一种常见做法。

    【讨论】:

      猜你喜欢
      • 2016-01-16
      • 2013-06-03
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      相关资源
      最近更新 更多