【问题标题】:Magento: Fatal error: Call to a member function load() on a non-objectMagento:致命错误:在非对象上调用成员函数 load()
【发布时间】:2014-05-06 14:43:52
【问题描述】:

我正在创建自己的模块,一切都很顺利,直到我开始创建自己的模型。

我正在尝试查询我添加的数据库表(其中包含数据),我想要做的就是打印数据。

当我查看调用模块的页面时,我收到以下消息

致命错误:在非对象上调用成员函数 load()

在这条线上

$model = Mage::getResourceModel('facebooklikediscount/facebookcoupon')->load(1);

这是我的 config.xml(模型部分)

<models>
    <mymodule_facebooklikediscount>
        <class>MyModule_FacebookLikeDiscount_Model</class>
        <resourceModel>mymodule_facebooklikediscount_resource</resourceModel>
    </mymodule_facebooklikediscount>
    <mymodule_facebooklikediscount_resource>
        <class>MyModule_FacebookLikeDiscount_Model_Resource</class>
        <deprecatedNode>mymodule_facebooklikediscount_mysql4</deprecatedNode>
        <entities>
            <facebookcoupon>
                <table>salesrule_coupon_facebook</table>
            </facebookcoupon>
        </entities>
    </mymodule_facebooklikediscount_resource>
</models>

我的模型

<?php

class MyModule_FacebookLikeDiscount_Model_Facebookcoupon extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('facebooklikediscount/facebookcoupon');
    }
}

资源模型

<?php

class MyModule_FacebookLikeDiscount_Model_Resource_Facebookcoupon extends Mage_Core_Model_Resource_Db_Abstract
{
    protected $_storeId;

    protected function _construct()
    {
        $this->_init('facebooklikediscount/facebookcoupon', 'entity_id');
        $this->_storeId = (int)Mage::app()->getStore()->getId();
    }

    public function getData($entityId)
    {
        $resource = Mage::getSingleton('core/resource');
        $select = $resource->getConnection('core_read')->select();
        $select
            ->from($this->getTable(array('facebooklikediscount/facebookcoupon', $this->_storeId)), '*')
            ->where('entity_id = :entity_id');

        $result = $resource->getConnection('core_read')->fetchAll($select, array('entity_id' => $entityId));

        return $result;
    }
}

【问题讨论】:

  • Adrock,请放资源模型文件

标签: php xml magento magento-1.12


【解决方案1】:

由于您收到错误“致命错误:在非对象上调用成员函数 load()”,如果您没有正确设置模型集合,则会出现该错误。 load 函数查找模型资源 - 然后需要模型集合。

查找如何设置资源和集合:

http://www.pixafy.com/blog/2013/04/creating-a-magento-custom-model/

【讨论】:

    【解决方案2】:

    型号代码应该是

    $model = Mage::getModel('mymodule_facebooklikediscount/facebookcoupon')->load(1);
    

    而且Resource确实有可能load()函数

    $model = Mage::getResourceModel('mymodule_facebooklikediscount/facebookcoupon')->load();
    

    再次

        resource model load() function is loading total records.It is did not load 
    by primary key 
    

    单条记录使用

          $model = Mage::getModel('mymodule_facebooklikediscount/facebookcoupon')
    ->load($primary_key);
    

    Model>Facebookcoupon.php 代码应该是 来自

    protected function _construct()
        {
            $this->_init('facebooklikediscount/facebookcoupon');
        }
    to 
    protected function _construct()
        {
            $this->_init('mymodule_facebooklikediscount/facebookcoupon');
        }
    

    model>resource>facebookcoupon.php 应该是

     protected function _construct()
        {
            $this->_init('mymodule_facebooklikediscoun/facebookcoupon', 'entity_id');
            $this->_storeId = (int)Mage::app()->getStore()->getId();
        }
    

    【讨论】:

    • 感谢您的回答,但我今天早上又开始工作了。我认为我的 config.xml 应该受到责备
    【解决方案3】:

    尝试调用 getCollection() 方法:

    $model = Mage::getModel('facebooklikediscount/facebookcoupon')->getCollection()->load(1);
    

    【讨论】:

      【解决方案4】:

      你应该像这样创建模型实例

      $model = Mage::getModel('mymodule_facebooklikediscount/facebookcoupon')->load(1);
      

      使用getModel 而不是getResourceModel
      您传递给getModel 的参数由两部分组成。
      斜线之前的部分是您在config.xml 中添加的models 标签下的标签名称。在你的情况下mymodule_facebooklikediscount

      第二部分是您要实例化的类名的小写字符串,您可以从中删除在模型的&lt;class&gt; 标记中添加的内容。

      所以如果类名是MyModule_FacebookLikeDiscount_Model_Facebookcoupon,而你在config.xml中有class&gt;MyModule_FacebookLikeDiscount_Model&lt;/class&gt;,那么第二部分是facebookcoupon

      【讨论】:

      • 感谢您的回复。我现在收到此错误消息Fatal error: Call to a member function load() on a non-object in C:\www\magento.dev\app\code\core\Mage\Core\Model\Abstract.php on line 225
      猜你喜欢
      • 2013-06-12
      • 2010-12-20
      • 2015-06-16
      • 2016-02-02
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多