-1

PLEASE!!! I'm stuck with this problem for about a month.

My original problem is Mage::getResourceModel() returns false. I tried everything and couldn't succeed.

Then I started to trace the core classes which relate to the getResourceModel() function.

I end up in Config.php _getResourceModelFactoryClassName() function, which reads the XML from $this->_xml->global->models object.

I var_dump that XML and found out that other module has _resource attribute, but my module doesn't have it.

["weee"]=>
 object(Mage_Core_Model_Config_Element)#143 (2) {
    ["class"]=>
    string(15) "Mage_Weee_Model"
    ["resourceModel"]=>
    string(13) "weee_resource"
  }
  ["weee_resource"]=>
  object(Mage_Core_Model_Config_Element)#144 (3) {
    ["class"]=>
    string(24) "Mage_Weee_Model_Resource"
    ["deprecatedNode"]=>
    string(11) "weee_mysql4"
    ["entities"]=>
    object(Mage_Core_Model_Config_Element)#150 (2) {
      ["tax"]=>
      object(Mage_Core_Model_Config_Element)#175 (1) {
       ["table"]=>
        string(8) "weee_tax"
      }
      ["discount"]=>
      object(Mage_Core_Model_Config_Element)#172 (1) {
        ["table"]=>
       string(13) "weee_discount"
      }
    }
 }

  ["module"]=>
  object(Mage_Core_Model_Config_Element)#146 (2) {
    ["class"]=>
    string(12) "Namespace_Module_Model"
    ["resourceModel"]=>
    string(11) "module_resource"
  }

In above code ["module"] is my custom module and it doesn't come with it's resource like ["weee_resource"]

<global>
    <models>
        <module>
            <class>Namespace_Module_Model</class>
            <resourceModel>module_resource</resourceModel>
        </module>
    </models>
    <module_resource>
        <class>Namespace_Module_Model_Resource</class>
        <deprecatedNode>cb_mysql4</deprecatedNode>
        <entities>
            <payment>
                <table>acs_transaction</table>
            </payment>
        </entities>
    </module_resource>

Thanks.

Mike
  • 2,132
  • 3
  • 20
  • 33
KKLN
  • 3
  • 2

2 Answers2

0

There is no module_resource NODE in config.xml

    </models>
    <module_resource>

this declaration is wrong your modules_resource needs to go under models node

<global>
    <models>
        <your_module>
            <class>Namespace_Module_Model</class>
            <resourceModel>your_module_resource</resourceModel>
        </your_module>
        <your_module_resource >
        <class>Namespace_Module_Model_Resource</class>
        <deprecatedNode>cb_mysql4</deprecatedNode>
        <entities>
            <payment>
                <table>acs_transaction</table>
            </payment>
        </entities>
    </your_module_resource >
    </models>

after that you can define resources and database tables in

<global>
    <resources>

node

MagentoNinja
  • 421
  • 2
  • 9
  • Hi, thanks for your answer, after I fixed the xml, it passes previous break point and then it hit new error 'Fatal error: Call to a member function getConnection() on a non-object in ' ... when i trace the code it's in Resource/DB/Abstract.php on line 323 at this line '$this->_resources->getConnection($connectionName);' ... seems _resources is null ... what is the likelihood of this problem? any special config to set up for this to work? i refer to core modules and the simply works without anything extra ... exhausted ... :( – KKLN May 09 '15 at 07:56
0

To get Collection you also need it to be defined by Model configuration. Simply to say you require 3 files:

Mynamespace/Mymodule/Model/Mymodel.php
Mynamespace/Mymodule/Model/Resource/MyModel.php
Mynamespace/Mymodule/Model/Resource/MyModel/Collection.php

Mynamespace/Mymodule/Model/Mymodel.php needs to have _init() method implemented and point to correct resource model;

class Mynamespace_Mymodule_Model_MyModel 
    extends Mage_Core_Model_Abstract {
    public function _construct() {
        $this->_init('mymodule/mymodel');
    }
}

then in Mynamespace/Mymodule/Model/Resource/Mymodel.php needs to point to correct resource (table or eav)

class Mynamespace_Mymodule_Model_Resource_Mymodel
    extends Mage_Core_Model_Resource_Db_Abstract
    {
    public function _construct() {
        $this->_init('mymodule/mymodel', 'entity_id'); // entity_id - autoincrement column
    }
}

and last file you require is

Mynamespace/Mymodule/Model/Resource/Mymodel/Collection.php file

class Mynamespace_Mymodule_Model_Resource_Mymodel_Collection
    extends Mage_Core_Model_Resource_Db_Collection_Abstract
    {
    public function _construct() {
        $this->_init('mymodule/mymodel');
    }
}

if you want to test it it works simply create a test.php file and run

var_dump(get_class(Mage::getModel('mymodule/mymodel'));
var_dump(get_class(Mage::getResourceModel('mymodule/mymodel'));
var_dump(get_class(Mage::getModel('mymodule/mymodel')->getCollection());

if you get 3 class names, you are goot to go.

MagentoNinja
  • 421
  • 2
  • 9
  • Hi, thanks for the reply, I have all those files but didn't work ... what I'm currently doing is simply create a model, set some values and save to database ... that's all '$_tran = Mage::getModel('mymodule/mymodel'); ... assign some values ... $_tran->save();' – KKLN May 09 '15 at 08:59