3

I would like to create a custom module. I have taken the following setps my code are below.

app\code\local\Amzad\OrderComment\etc\config.xml

<models>
  <ordercomment>
    <class>Amzad_OrderComment_Model</class>
  </ordercomment>
    <ordercomment_resource>
        <class>Amzad_OrderComment_Model_Resource</class>
        <entities>
            <config>
                <table>ordercomment_config</table>
            </config>
        </entities>
    </ordercomment_resource>
</models>

app\code\local\Amzad\OrderComment\Model\Resource\Config\Collection.php

   class Amzad_OrderComment_Model_Resource_Config_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
        protected function _construct()
        {
            $this->_init('ordercomment/config');
        }
    }

app\code\local\Amzad\OrderComment\Model\Resource\Config.php

    class Amzad_OrderComment_Model_Resource_Config extends Mage_Core_Model_Resource_Db_Abstract
    {
        protected function _construct()
        {
            $this->_init('ordercomment/config', 'id');
        }
    }

app\code\local\Amzad\OrderComment\Model\Config.php

    class Amzad_OrderComment_Model_Config extends Mage_Core_Model_Abstract
    {

        protected function _construct()
        {
            $this->_init('ordercomment/config');
        }
    }

Following 2 lines does not give me any output.

  $collection = Mage::getModel('ordercomment/config')->getCollection();
  echo "\n" . (string)$collection->getSelect()->limit(1);

php_error.log

 PHP Fatal error:  Call to a member function getSelect() on a non-object in 

Could someone help me resolve this issue.

m82amjad
  • 313
  • 4
  • 10

3 Answers3

4

I think you're missing a <resourceModel>ordercomment_resource</resourceModel> node in config/global/models/ordercomment node.

/Edit: Something like:

<config>
...
  <global>
    ...
    <models>
      <ordercomment>
        <class>Amzad_OrderComment_Model</class>
        <resourceModel>ordercomment_resource</resourceModel>
      </ordercomment>
      <ordercomment_resource>
        <class>Amzad_OrderComment_Model_Resource</class>
        <entities>
            <config>
                <table>ordercomment_config</table>
            </config>
        </entities>
      </ordercomment_resource>
...
    </models>
...
  </global>
...
</config>
m82amjad
  • 313
  • 4
  • 10
Adi
  • 1,216
  • 6
  • 14
1

Is this your complete config?

Change it to:

<config>
    <global>
        <models>
          <ordercomment>
            <class>Tprg_OrderComment_Model</class>
          </ordercomment>
            <ordercomment_resource>
                <class>Tprg_OrderComment_Model_Resource</class>
                <entities>
                    <config>
                        <table>ordercomment_config</table>
                    </config>
                </entities>
            </ordercomment_resource>
        </models>    
    </global>
</config>

And I assume you have the module xml file in app/etc/modules.

Fabian Blechschmidt
  • 35,388
  • 8
  • 75
  • 182
1

Its working now !! Thanks you everybody !!!

app\code\local\Amzad\OrderComment\etc\config.xml

<models>
  <ordercomment>
      <class>Amzad_OrderComment_Model</class>
      <resourceModel>ordercomment_resource</resourceModel>
  </ordercomment>
    <ordercomment_resource>
        <class>Amzad_OrderComment_Model_Resource</class>
        <entities>
            <config>
                <table>ordercomment_config</table>
            </config>
        </entities>
    </ordercomment_resource>
</models>

app\code\local\Amzad\OrderComment\Model\Config.php

    class Amzad_OrderComment_Model_Config extends Mage_Core_Model_Abstract
    {

        protected function _construct()
        {
            $this->_init('ordercomment/config');
        }
    }

app\code\local\Amzad\OrderComment\Model\Resource\Config.php

    class Amzad_OrderComment_Model_Resource_Config extends Mage_Core_Model_Resource_Db_Abstract
    {
        protected function _construct()
        {
            $this->_init('ordercomment/config', 'id');
        }
    }

app\code\local\Amzad\OrderComment\Model\Resource\Config\Collection.php

 class Amzad_OrderComment_Model_Resource_Config_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
    protected function _construct()
    {
        $this->_init('ordercomment/config');
    }
}


    $collection = Mage::getModel('ordercomment/config')->getCollection();
    echo "\n" . (string)$collection->getSelect()->limit(1);

Here is some sample if you would like to retrieve and save data .

     Mage::getModel('ordercomment/config')->getCollection()
    ->getFirstItem()->getLastExportId();
    Mage::getModel('ordercomment/config')->load(1)->getLastExportId();
    $model = Mage::getModel('ordercomment/config')->load(1)->addData(array('last_export_id'=>$maxId));
    $model->setId(1)->save();
m82amjad
  • 313
  • 4
  • 10