![](/img/trans.png)
[英]Magento : Fatal error: Call to a member function getMethodInstance() on a non-object
[英]Magento: Fatal error: Call to a member function load() on a non-object
我正在创建自己的模块,一切都很顺利,直到我开始创建自己的模型。
我正在尝试查询我添加的数据库表(其中包含数据),我想做的就是打印数据。
当我查看调用模块的页面时,我收到以下消息
致命错误:在非对象上调用成员函数 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;
}
}
您应该像这样创建模型实例
$model = Mage::getModel('mymodule_facebooklikediscount/facebookcoupon')->load(1);
使用getModel
而不是getResourceModel
。
您传递给getModel
的参数由 2 部分组成。
斜线之前的部分是您在config.xml
添加到models
标签下的标签名称。 在你的情况下mymodule_facebooklikediscount
。
第二部分是您正在实例化的类名的小写字符串,从中删除您在模型的<class>
标记中添加的内容。
因此,如果类名称是MyModule_FacebookLikeDiscount_Model_Facebookcoupon
并且您在 config.xml class>MyModule_FacebookLikeDiscount_Model</class>
那么第二部分是facebookcoupon
尝试调用 getCollection() 方法:
$model = Mage::getModel('facebooklikediscount/facebookcoupon')->getCollection()->load(1);
型号代码应该是
$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);
模型>Facebookcoupon.php 代码应该来自
protected function _construct()
{
$this->_init('facebooklikediscount/facebookcoupon');
}
to
protected function _construct()
{
$this->_init('mymodule_facebooklikediscount/facebookcoupon');
}
模型>资源>facebookcoupon.php 应该是
protected function _construct()
{
$this->_init('mymodule_facebooklikediscoun/facebookcoupon', 'entity_id');
$this->_storeId = (int)Mage::app()->getStore()->getId();
}
由于您收到错误“致命错误:在非对象上调用成员函数 load()”,如果您没有正确设置模型集合,则会出现该错误。 加载函数查找模型资源 - 然后需要模型集合。
查找如何设置资源和集合:
http://www.pixafy.com/blog/2013/04/creating-a-magento-custom-model/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.