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
on this line
$model = Mage::getResourceModel('facebooklikediscount/facebookcoupon')->load(1);
Here is my config.xml (the model part)
<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
Use
getModel
instead ofgetResourceModel
.The parameter you pass to
getModel
consists of 2 parts.The part before the slash it's the tag name you added in
config.xml
under themodels
tag. IN your casemymodule_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.So if the class name is
MyModule_FacebookLikeDiscount_Model_Facebookcoupon
and you have in config.xmlclass>MyModule_FacebookLikeDiscount_Model</class>
then the s econd part isfacebookcoupon