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.

1
  • Can you share your source code via git?
    – zhartaunik
    Aug 4, 2015 at 9:03

3 Answers 3

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>
5
  • Thanks for your suggestion ... I have added <resourceModel>tprg_ordercomment_resource</resourceModel> and <resourceModel>ordercomment_resource</resourceModel> remain unlucky
    – m82amjad
    Aug 4, 2015 at 10:08
  • There's only one resource model per model, so you can't add both. Did you clear the cache?
    – Adi
    Aug 4, 2015 at 10:15
  • <resourceModel>ordercomment_resource</resourceModel> worked but there was few error found in php_error.log
    – m82amjad
    Aug 4, 2015 at 10:18
  • You can open a new question for the other errors, since this one is fixed.
    – Adi
    Aug 4, 2015 at 10:23
  • Resolve the error.. Posted an answer below with working code. Thanks a lot. you are a star !!
    – m82amjad
    Aug 4, 2015 at 10:25
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();
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.

5
  • It is a working module. Just adding a custom module.
    – m82amjad
    Aug 4, 2015 at 8:51
  • And it is under global section.
    – m82amjad
    Aug 4, 2015 at 8:52
  • you are talking about custom model, not module, correct me if I'm wrong. Beside this your code looks good. Did you clean the cache? Aug 4, 2015 at 9:05
  • custom model in a working module !! :)
    – m82amjad
    Aug 4, 2015 at 10:02
  • @m82amjad make the comment about the model, don't edit posts for this purpose.
    – SR_Magento
    Aug 4, 2015 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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