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.
Best Answer
Try making your
Pricebinc_Shipping_Model_Resource_Shipmentlist
class look like this:So don't call the
parent::_construct
method and the first parameter of_init
should contains a slash not an underscore.