1

Creating a custom module Im facing many problems saving data to DB. My code look like this:

config.xml:

<modules>
    <Pricebinc_Shipping>
        <version>0.1.0</version>
    </Pricebinc_Shipping>
</modules>
<global>
    <models>
        <shipping>
            <class>Pricebinc_Shipping_Model</class>
            <resourceModel>shipping_resource</resourceModel>
        </shipping>
        <shipping_resource>
            <class>Pricebinc_Shipping_Model_Resource</class>
            <entities>
                <shipmentlist>
                    <table>shipping_shipmentlist</table>
                </shipmentlist>
            </entities>
        </shipping_resource>
    </models>
    <resources>
        <shipping_setup>
            <setup>
                <module>Pricebinc_Shipping</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </shipping_setup>
        <shipping_write>
            <connection>
                <use>core_write</use>
            </connection>
        </shipping_write>
        <shipping_read>
            <connection>
                <use>core_read</use>
            </connection>
        </shipping_read>
    </resources>

In Model I have three files. a- model/shipmentlist.php

class Pricebinc_Shipping_Model_Shipmentlist extends Mage_Core_Model_Abstract {

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

Model/Resource/Shipmentlist.php with code:

class Pricebinc_Shipping_Model_Resource_Shipmentlist extends Mage_Core_Model_Resource_Db_Abstract
{
    protected function _construct()
    {
        parent::_construct();
        $this->_init('shipping/shipmentlist', 'autoid');
    }
}

autoid is my primary key.

and Model/Resource/Shipmentlist/collection.php with code:

class Pricebinc_Shipping_Model_Resource_Shipmentlist_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    protected function _construct()
    {
        parent::_construct();
        $this->_init('shipping/shipmentlist');
    }
}

finally I have a controller and trying to save data using the following code:

public function newAwbAction()
{
            $shipcontact = ''.$this->getRequest()->getPost('shipContact');
            $shipper = ''.$this->getRequest()->getPost('shipper');
            $shipaddr1 = ''.$this->getRequest()->getPost('shipAddr1');
            $shipaddr2 = ''.$this->getRequest()->getPost('shipAddr2');

            $magentoShippingModel = Mage::getModel('shipping/shipmentlist');
            $magentoShippingModel->setData('shipContact', $shipcontact);
            $magentoShippingModel->setData('shipper', $shipper);
            $magentoShippingModel->setData('shipAddr1', $shipaddr1);
            $magentoShippingModel->setData('shipAddr2', $shipaddr2);
            $magentoShippingModel->save();

}

Im receiving the following error -> Fatal error: Cannot call abstract method Mage_Core_Model_Resource_Abstract::_construct() in C:\Zend\Apache2\htdocs\app\code\local\Pricebinc\Shipping\Model\Resource\Shipmentlist.php on line 6

If I change Model/Resource/Shipmentlist.php to

class Pricebinc_Shipping_Model_Resource_Shipmentlist extends Mage_Core_Model_Resource_Db_Abstract
{
    protected function _construct()
    {
        $mainTable = 'shipping_shipmentlist'; // check the node in the config.xml
        $idFieldName = 'autoid'; // whatever the column is named.
        $this->_init($mainTable, $idFieldName);
    }
}

I receive the following error -> Fatal error: Call to a member function getConnection() on a non-object in C:\Zend\Apache2\htdocs\app\code\core\Mage\Core\Model\Resource\Db\Abstract.php on line 322

any help appreciated.

  • 1
    comment out parent::_construct() inside your resoure file and try again. note keep the first code instead of your second try – Rajeev K Tomy Sep 17 '14 at 7:42
2

Try making your Pricebinc_Shipping_Model_Resource_Shipmentlist class look like this:

class Pricebinc_Shipping_Model_Resource_Shipmentlist extends Mage_Core_Model_Resource_Db_Abstract {
    protected function _construct()
    {
        $this->_init('shipping/shipmentlist', 'autoid');
    }
}

So don't call the parent::_construct method and the first parameter of _init should contains a slash not an underscore.

  • yes correct. I already commented it. But this may some time make problem. you need to remove the extra space that comes after your class name. it's my humble suggestion, great guru :) – Rajeev K Tomy Sep 17 '14 at 7:46
  • @programmer_rkt. What space? you mean the one before the {. That won't hurt anything. – Marius Sep 17 '14 at 7:50
  • nope that is not what I meant. You have placed extend part in the next line. I have seen some cases, where that makes some problems.(in very rare instance). – Rajeev K Tomy Sep 17 '14 at 7:53
  • OK. I'll change it, but I've been doing it like this for a very long time for class names that are long and never had an issue with it. – Marius Sep 17 '14 at 7:55
  • framework.zend.com/manual/1.12/en/… please refer this. just go below, you can see class decalration part. You can see that, it recommends my method of coding. If you refer PSR standards, you can see this there too. ALWAYS KEEP BEST PRACTICES. that is why I mentioned it. :) – Rajeev K Tomy Sep 17 '14 at 8:00

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.