0

All my files for reference: (with filenames and filepath)

/app/code/local/Namespace/Modulename/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>0.1.0</version>
        </Namespace_Modulename>
    </modules>

    <!-- Configure our module's behavior in the global scope -->
    <frontend>
        <routers>
          <modulename>
            <use>standard</use>
              <args>
                <module>Namespace_Modulename</module>
                <frontName>modulename</frontName>
              </args>
          </modulename>
        </routers>
    </frontend>
    <global>

        <models>
            <Namespace_Modulename>Namespace_Modulename_Model</Namespace_Modulename>

            <modulename>
                <class>Namespace_Modulename_Model</class>
                <resourceModel>Modulename_mysql4</resourceModel>
            </modulename>
            <Modulename_mysql4>
                <class>Namespace_Modulename_Model_Mysql4</class>
                <entities>
                    <testModel1>
                        <table>testModel1</table>
                    </testModel1>
                    <testModel2>
                        <table>testModel2</table>
                    </testModel2>
                </entities>
            </Modulename_mysql4>

        </models>

        <resources>
            <modulename_setup>
                <setup>
                    <module>Namespace_Modulename</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </modulename_setup>
            <modulename_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </modulename_write>
            <modulename_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </modulename_read>
        </resources>

        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <namespace_modulename_checkout_cart_product_add_after_observer>
                        <type>singleton</type>
                        <class>Namespace_Modulename_Model_Observer</class>
                        <method>onAddToCart</method>
                    </namespace_modulename_checkout_cart_product_add_after_observer>
                </observers>
            </checkout_cart_product_add_after>

        </events>

    </global>

</config>

/app/code/local/Namespace/Modulename/Model/TestModel1.php

<?php

class Namespace_ Modulename_Model_TestModel1 extends Mage_Core_Model_Abstract
{

    public function _construct()
    {
        parent::_construct();
        $this->_init('modulename/testModel1');
    }
}

/app/code/local/Namespace/Modulename/Model/TestModel2.php

<?php

class Namespace_ Modulename_Model_TestModel2 extends Mage_Core_Model_Abstract
{

    public function _construct()
    {
        parent::_construct();
        $this->_init('modulename/testModel2');
    }
}

/app/code/local/Namespace/Modulename/Model/Observer.php

<?php

class Namespace_Modulename_Model_Observer
{
    public function onAddToCart(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();

        $name = $product->getName();
        $sku = $product->getSku();
        Mage::log(
            "{$name} ({$sku}) updated",
            null,
            'product-updates.log'
        );
        $data = array('test'=>'test');
        $model = Mage::getModel('modulename/testModel1')->setData($data);
        try {
            $insertId = $model->save()->getId();
            Mage::log(
                "DataInserted, id = {$insertId}",
                null,
                'product-updates.log'
            );
        } catch (Exception $e){
        }
}

/app/code/local/Namespace/Modulename/Model/mysql4/TestModel1.php

<?php

class Namespace_Modulename_Model_Mysql4_TestModel1 extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {  
        $this->_init('modulename/testModel1', 'test_id');
    }
} 

/app/code/local/Namespace/Modulename/Model/mysql4/TestModel2.php

<?php

class Namespace_Modulename_Model_Mysql4_TestModel2 extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {  
        $this->_init('modulename/testModel2', 'test_id');
    }
} 

/app/code/local/Namespace/Modulename/Model/mysql4/TestModel1/Collection.php

<?php

class Namespace_Modulename_Model_Mysql4_TestModel1_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::__construct();
        $this->_init('modulename/testModel1');
    }
}

/app/code/local/Namespace/Modulename/Model/mysql4/TestModel2/Collection.php

<?php

class Namespace_Modulename_Model_Mysql4_TestModel2_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::__construct();
        $this->_init('modulename/testModel2');
    }
}

/app/code/local/Namespace/Modulename/sql/modulename_setup/mysql4-install-0.1.0.php

<?php

$installer = $this;

$installer->startSetup();

    $installer->run("

DROP TABLE IF EXISTS {$this->getTable('testModel1')};
CREATE TABLE {$this->getTable('testModel1')} (
  `test_id` int(11) unsigned NOT NULL auto_increment,
  `created_time` datetime NULL,
  `update_time` datetime NULL,
  `test` varchar(100) NULL,
  PRIMARY KEY (`test_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS {$this->getTable('testModel2')};
CREATE TABLE {$this->getTable('testModel2')} (
  `test_id` int(11) unsigned NOT NULL auto_increment,
  `created_time` datetime NULL,
  `update_time` datetime NULL,
  `test` varchar(100) NULL,
  PRIMARY KEY (`test_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ");



$installer->endSetup(); 

Problem This is my local extension I created myself, named Namespace_Modulename, it works fine and the event of add to cart in observer.php gets triggered as well and it logs the product name and all, so it works fine, but I get this error on my screen when I click on add to cart button,

Fatal error: Call to a member function setData() on a non-object in /opt/lampp/htdocs/magento2/app/code/local/Namespace/Modulename/Model/Observer.php on line 17

This is because there is some problem in the model, which I am not able to figure out. I want to save some data inside a custom model, but the model does not get created. In my core_resource table, following entry is present "modulename_setup", I deleted it and refreshed the page, it gets re-created but the tables are not created.

In my system.log I get this:

Warning: include(Mage/Modulename/Model/TestModel1.php): failed to open stream: No such file or directory  in /opt/lampp/htdocs/magento2/lib/Varien/Autoload.php on line 94

Cache has been disabled+refreshed, logs are enabled.

Edit: I ran this query manually and it does not create any table, you can copy paste and try to run this query

DROP TABLE IF EXISTS testModel2;
CREATE TABLE testModel2 (
  `test_id` int(11) unsigned NOT NULL auto_increment,
  `created_time` datetime NULL,
  `update_time` datetime NULL,
  `test` varchar(100) NULL,
  PRIMARY KEY (`test_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
0

Please check your model file name was incorrect.

Because it is trying to find your "Modulename" and file "TestModel1.php" but it will not found in Autoload function so it will display error like

Warning: include(Mage/Modulename/Model/TestModel1.php): failed to open stream: No such file or directory  in /opt/lampp/htdocs/magento2/lib/Varien/Autoload.php on line 94

I think you have mistake in call model in module somewhere so please check code where you initiate model.

  • like i said problem is in the SQL query... try to run that query manually on your database, it won't run, i have no idea why – user1735921 Oct 23 '15 at 1:50
  • I have execute query it is working properly having such a issue in call model so please check all code where you call Mage::getModel – John Oct 26 '15 at 5:32
0

In config.xml under the <models> tag you have this:

        <Modulename>
            <class>Namespace_Modulename_Model</class>
            <resourceModel>Modulename_mysql4</resourceModel>
        </Modulename>

The tag <Modulename> should be in lowercase <modulename>.

Clear the cache.

  • Hi marius, changed to lowercase, still same problem (updated above code like I updated mine), it creates entry in core_resource but does not create tables – user1735921 Oct 21 '15 at 12:56
  • Edit: Also debugged the mysql-install php file, it successfully logs, that means it gets executed but table is still not created.. – user1735921 Oct 21 '15 at 13:03

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.