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

8.3k views Asked by At

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

There are 4 answers

1
Marius On

You should create the model instance like this

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

Use getModel instead of getResourceModel.
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 the models tag. IN your case 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.

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

0
J.S On

Try with calling getCollection() method:

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

the model code should be

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

And Resource did have may load() function

$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

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

model>resource>facebookcoupon.php should be

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

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. 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/