1

I am new to Magento and trying to build a module. in my module, I have created a custom databse table which is successfully added to database now I want to add some data to the database table and I am getting following error.

Fatal error</b>:  Uncaught Error: Call to a member function addData() on boolean in E:\xampp\htdocs\magento4\app\code\local\Wri\TodoHelper\controllers\IndexController.php:19
Stack trace:
#0 E:\xampp\htdocs\magento4\app\code\core\Mage\Core\Controller\Varien\Action.php(418): Wri_TodoHelper_IndexController-&gt;sayHelloAction()
#1 E:\xampp\htdocs\magento4\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(254): Mage_Core_Controller_Varien_Action-&gt;dispatch('sayHello')
#2 E:\xampp\htdocs\magento4\app\code\core\Mage\Core\Controller\Varien\Front.php(172): Mage_Core_Controller_Varien_Router_Standard-&gt;match(Object(Mage_Core_Controller_Request_Http))
#3 E:\xampp\htdocs\magento4\app\code\core\Mage\Core\Model\App.php(365): Mage_Core_Controller_Varien_Front-&gt;dispatch()
#4 E:\xampp\htdocs\magento4\app\Mage.php(683): Mage_Core_Model_App-&gt;run(Array)
#5 E:\xampp\htdocs\magento4\index.php(83): Mage::run('', 'store')
#6 {main}
  thrown in <b>E:\xampp\htdocs\magento4\app\code\local\Wri\TodoHelper\controllers\IndexController.php</b> on line <b>19</b><br />

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Wri_TodoHelper>
            <version>1.0.0</version>
        </Wri_TodoHelper>
    </modules>
    <global>
        <helpers>
            <wri_todohelper>
                <class>Wri_TodoHelper_Helper</class>
            </wri_todohelper>
        </helpers>
        <models>
            <wri_todohelper>
                <class>Wri_TodoHelper_Model</class>
                <resourceModel>wri_todohelper_resource</resourceModel>
            </wri_todohelper>

            <wri_todohelper_resource>
                <class>Wri_TodoHelper_Model_Resource</class>
                <entities>
                    <todo> <!-- define an new  entities for module -->
                        <table>wri_todohelper_lists</table> <!-- define table -->
                    </todo>
                </entities>
            </wri_todohelper_resource>
        </models>
        <blocks>
            <wri_todohelper>
                <class>Wri_TodoHelper_Block</class>
            </wri_todohelper>
        </blocks>
        <resources>
            <wri_todohelper_setup>
                <setup>
                    <module>Wri_TodoHelper</module>
                </setup>
            </wri_todohelper_setup>
        </resources>
    </global>
    <frontend>
        <routers>
            <Wri_TodoHelper_SayHello>
                <use>standard</use>
                <args>
                    <module>Wri_TodoHelper</module>
                    <frontName>todo</frontName>
                </args>
            </Wri_TodoHelper_SayHello>
        </routers>
        <layout>
            <updates>
                <custommodule>
                    <file>todohelper.xml</file>
                </custommodule>
            </updates>
        </layout>
    </frontend>
</config>

install-1.0.0.php

$tableName = $installer->getTable('wri_todohelper/todo');
if (!$connection->isTableExists($tableName)){
    $table = $connection
        ->newTable($tableName)
        ->addColumn(
            'id',
            Varien_Db_Ddl_Table::TYPE_INTEGER,
            null,
            array(
                'identity' => true,
                'unsigned' => true,
                'nullable' => false,
                'primary'  => true,
            ),
            'ID'
        )
        ->addColumn(
            'task',
            Varien_Db_Ddl_Table::TYPE_VARCHAR,
            255,
            array(
                'nullable' => false,
            ),
            'Task Name'
        )->addColumn(
            'status',
            Varien_Db_Ddl_Table::TYPE_INTEGER,
            10,
            array(
                'nullable' => false,
            ),
            'Task Staus'
        ) ->addColumn(
            'created_at',
            Varien_Db_Ddl_Table::TYPE_DATETIME,
            null,
            array(
                'nullable' => false,
            ),
            'Created At'
        );
    $connection->createTable($table);
}

$installer->endSetup();

Todo.php root/app/code/local/Wri/TodoHelper/Model/

<?php
class Wri_TodoHelper_Model_TodoHelper extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        $this->_init('wri_todohelper/todo');
    }

}

TodoHelper.php root/app/code/local/Wri/TodoHelper/Model/Resource

    <?php

class Wri_TodoHelper_Model_Resource_TodoHelper extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('wri_todohelper/todo', 'id');
    }
}

?>

Collection.php root/app/code/local/Wri/TodoHelper/Model/Resource/TodoHelper

<?php

class Wri_TodoHelper_Model_Resource_TodoHelper_Collection
    extends Mage_Core_Model_Resource_Db_Collection_Abstract{
    protected function _construct(){
        parent::_construct();
        $this->_init('wri_todohelper/todo');
    }
}

IndexController.php

<?php

class Wri_TodoHelper_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo 'Hello developer...';
    }

    public function sayHelloAction()
    {
        $taskname = $_REQUEST['todo'];
        $status = $_REQUEST['todo'];

        $response = '';

        if ($taskname && $status){
            $data = array('task'=>$taskname, 'status'=> $status);
            $model = Mage::getModel('wri_todohelper/todo')->addData($data);
            try {
                $model->save(); //save data
                $insertId = $model->getId();
                $response->status = "success";
                $response->data = "Data successfully inserted. Insert ID: " . $insertId;
                echo $response;
            } catch (Exception $e) {
                $response->status = "failed";
                $response->data = $e->getMessage();
                echo $response;
            }
        }else{
            $response->status = "failed";
            $response->data = "Not Found";
            echo $response;
        }
    }
}

?>

Please Help !!!

  • root/app/code/local/Wri/TodoHelper/Model/Todo.php - class Wri_TodoHelper_Model_TodoHelper <- that class name seems incorrect to me (class Wri_TodoHelper_Model_Todo) – Christoph Farnleitner Jan 25 '18 at 6:32
1

Just adding it here so this question can be closed:

Probably due to the modifications @Priyank has suggested the class name has not been updated accordingly by the questioner:

app/code/local/Wri/TodoHelper/Model/Todo.php must inherit a class with the name Wri_TodoHelper_Model_Todo rather than Wri_TodoHelper_Model_TodoHelper because else the class will not be loaded.

  • yes, i was trying to solve so do lots of modification and forget to rename classes.Thanks Once again – Vikas Kumar Tiwari Jan 25 '18 at 7:11
  • I need 15 reputations for upvote. so, I can accept your answer and I also want to thanks, @Priyank. – Vikas Kumar Tiwari Jan 25 '18 at 7:14
  • @VikasKumarTiwari, done that; he probably earned it for a good initial help :) – Christoph Farnleitner Jan 25 '18 at 8:25
2

As per your config file Try changing your model design pattern should be

$this->_init('wri_todohelper/todohelper');

Change the above in your model files. And In your controller call it in similar way

$model = Mage::getModel('wri_todohelper/todohelper')->addData($data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.