0
votes

I have the following error occur in my custom module :

Fatal error: Call to a member function setData() on a non-object in /opt/lampp/htdocs/magento/app/code/local/Phparrow/Hello/controllers/IndexController.php

My code is as follows -

config.xml

<global>
    <blocks>
        <hello>
                <class>Phparrow_Hello_Block</class>
        </hello>
    </blocks>
    <helpers>
        <hello>
            <class>Phparrow_Hello_Helper</class>   
        </hello>
    </helpers>
        <models>
            <hello>
                <class>Phparrow_Hello_Model</class>
                <resourceModel>hello_mysql4</resourceModel>
            </hello>
            <hello_mysql4>
                <class>Phparrow_Hello_Model_Mysql4</class>
                <entities>        
                    <hello>
                        <table>hello</table>
                    </hello>
                </entities>
            </hello_mysql4>
        </models>
        <resources>
            <hello_setup>
                <setup>
                    <module>Phparrow_Hello</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </hello_setup>
            <hello_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </hello_write>
            <hello_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </hello_read>
        </resources>
</global> 

magento\app\code\local\Phparrow\Hello\Model\Hello.php

<?php
class Phparrow_Hello_Model_Hello extends Mage_Core_Model_Abstract
{
    public function _construct(){
        print 'test';
       $this->_init('hello/hello');
    print 'test1';
    }
}

magento\app\code\local\Phparrow\Hello\Model\mysql4\Hello.php

<?php
class Phparrow_Hello_Model_Mysql4_Hello extends Mage_Core_Model_Mysql4_Abstract
    public function _construct()
    {
        $this->_init('hello/hello', 'hello_id');
    }
}

magento\app\code\local\Phparrow\Hello\Model\mysql4\Hello\Collection.php

<?php
class Phparrow_Hello_Model_Mysql4_Hello_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct(){
        $this->_init('hello/hello');
    }
}

magento\app\code\local\Phparrow\Hello\controllers\IndexController.php

class Phparrow_Hello_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    { 
     $model1= Mage::getModel('hello/hello');
     echo get_class($model1);
     $model1->setData('name'->'Harsh','contact'->'8445895621');
     $model1->save();
    }
}

I have a hello table have that has three fields:

hello_id, name, contact

Why would this not work in Linux when it successfully executes in Windows?

2
Try clear All caches from magento admin. Also check for compilation mode from admin, It should be Off. - Disha V.
Thanks for answer I had clear all caches and compilation mode already off but still issue is exist - user2240203
$model1->setData('name'=>'Harsh','contact'=>'8445895621'); - Disha V.
I have done this way but still error coming - user2240203
Thanks Disha I have logout and login back and again enable and disable compilation mode and it work - user2240203

2 Answers

0
votes

I'm pretty sure this has to do with your path or file naming, make sure that the files and folders are named exactly as you typed it here.

Windows doesn't care much and would probably serve up the file HelLo.php or hELLO.php just fine when the autoloader tried to find the model.

0
votes

This is the correct way to save the data in model. You need to make an array of data and pass in setData()

class Phparrow_Hello_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    { 
     $model1= Mage::getModel('hello/hello');
     $data = array('name'=>'Harsh','contact'=>'8445895621');
     $model1->setData($data);
     $model1->save();
    }
}