![](/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.