简体   繁体   English

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

[英]Magento: Fatal error: Call to a member function load() on a non-object

I am creating my own module and everything was going well until i started creating my own models.我正在创建自己的模块,一切都很顺利,直到我开始创建自己的模型。

I'm trying to query a database table I've added (which has data) and all i want to do is print the data.我正在尝试查询我添加的数据库表(其中包含数据),我想做的就是打印数据。

When i view the page which calls the module, I get the following message当我查看调用模块的页面时,我收到以下消息

Fatal error: Call to a member function load() on a non-object致命错误:在非对象上调用成员函数 load()

on this line在这条线上

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

Here is my config.xml (the model part)这是我的 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>

My model我的模型

<?php

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

Resource model资源模型

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

You should create the model instance like this您应该像这样创建模型实例

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

Use getModel instead of getResourceModel .使用getModel而不是getResourceModel
The parameter you pass to getModel consists of 2 parts.您传递给getModel的参数由 2 部分组成。
The part before the slash it's the tag name you added in config.xml under the models tag.斜线之前的部分是您在config.xml添加到models标签下的标签名称。 IN your case mymodule_facebooklikediscount .在你的情况下mymodule_facebooklikediscount

The second part is the lowercase string of the class name you are instantiating from which you remove what you added in the <class> tag for models.第二部分是您正在实例化的类名的小写字符串,从中删除您在模型的<class>标记中添加的内容。

So if the class name is MyModule_FacebookLikeDiscount_Model_Facebookcoupon and you have in config.xml class>MyModule_FacebookLikeDiscount_Model</class> then the s econd part is facebookcoupon因此,如果类名称是MyModule_FacebookLikeDiscount_Model_Facebookcoupon并且您在 config.xml class>MyModule_FacebookLikeDiscount_Model</class>那么第二部分是facebookcoupon

尝试调用 getCollection() 方法:

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

the model code should be型号代码应该是

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

And Resource did have may load() function并且 Resource 确实有可能 load() 函数

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

Again再次

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

for single records use单条记录使用

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

Model>Facebookcoupon.php code should be from模型>Facebookcoupon.php 代码应该来自

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

model>resource>facebookcoupon.php should be模型>资源>facebookcoupon.php 应该是

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

Since you get the error "Fatal error: Call to a member function load() on a non-object", that error would come up if you did not setup your model collection correctly.由于您收到错误“致命错误:在非对象上调用成员函数 load()”,如果您没有正确设置模型集合,则会出现该错误。 The load function looks for the model resource - which then needs model collection.加载函数查找模型资源 - 然后需要模型集合。

Lookup how to setup resource and collection:查找如何设置资源和集合:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento:致命错误:在非对象上调用成员函数 getMethodInstance() - Magento : Fatal error: Call to a member function getMethodInstance() on a non-object Magento致命错误-在非对象上调用成员函数getId() - Magento Fatal Error - Call to a member function getId() on a non-object Magento致命错误:在非对象上调用成员函数save() - Magento Fatal error: Call to a member function save() on a non-object 致命错误:在Magento中的非对象上调用成员函数getIdFieldName() - Fatal error: Call to a member function getIdFieldName() on a non-object in Magento 致命错误:在 magento 中的非对象上调用成员函数 getLevel() - Fatal error: Call to a member function getLevel() on a non-object in magento 致命错误:在magento中的非对象上调用成员函数getEmail() - Fatal error: Call to a member function getEmail() on a non-object in magento Magento致命错误:在非对象上调用成员函数addFieldToFilter() - Magento Fatal error: Call to a member function addFieldToFilter() on a non-object Magento致命错误:在非对象上调用成员函数getSku() - Magento Fatal Error: Call to member function getSku() on a non-object Magento:致命错误:在非对象上调用成员函数getItems() - Magento: Fatal error: Call to a member function getItems() on a non-object 致命错误:在非对象Joomla上调用成员函数load() - Fatal error: Call to a member function load() on a non-object Joomla
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM