I am suffering from a wired problem with my Custom Module. maybe i have wrong logic in my mind or it's a problem. I created a custom module "PoProduct" to extend back-end functionality (\app\code\local\Company\Po) and i have a controller file which contains action like Save, Edit, Add etc.. Now, when i call this module into a external file and trigger save action, the controller function not called.
-------- External File Code Sample --------
<?php
require_once('app/Mage.php');
Mage::app('default');
$writeConnection = Mage::getSingleton('core/resource')->getConnection('core_write');
$readConnection = Mage::getSingleton('core/resource')->getConnection('core_read');
try {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product_Collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('status', 1);
foreach ($product_Collection as $_product) {
$po_product_Collection = Mage::getModel('po/poproduct')->getCollection()->addFieldToFilter('product_id', $_product->getId())->addFieldToFilter('stock_status', array("neq" => "deleted"));
foreach ($po_product_Collection as $poproduct) {
$poproduct->setCustomFlag("Modified");
$poproduct->save();
}
}
} catch (Exception $ex) {}
?>
Here, when calling $poproduct->save();
code it's not fire PoproductController.php
saveAction()
.
-------- Custom Module Code Sample --------
- config.xml (\app\code\local\Company\Po\etc)
Code in config file
<config>
<modules>
<Company_Po>
<version>1.0.2</version>
</Company_Po>
</modules>
<admin>
<routers>
<po>
<use>admin</use>
<args>
<module>Company_Po</module>
<frontName>po</frontName>
</args>
</po>
</routers>
</admin>
<global>
<models>
<po>
<class>Company_Po_Model</class>
<resourceModel>po_mysql4</resourceModel>
</po>
<po_mysql4>
<class>Company_Po_Model_Mysql4</class>
<entities>
<po>
<table>purchase_order_cr</table>
</po>
<poproduct>
<table>purchase_order_products_cr</table>
</poproduct>
</entities>
</po_mysql4>
<po_setup>
<setup>
<module>Company_Po</module>
<class>Company_Po_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</po_setup>
</models>
<resources>
<po_write>
<connection>
<use>core_write</use>
</connection>
</po_write>
<po_read>
<connection>
<use>core_read</use>
</connection>
</po_read>
</resources>
<blocks>
<po>
<class>Company_Po_Block</class>
</po>
</blocks>
<helpers>
<po>
<class>Company_Po_Helper</class>
</po>
</helpers>
</global>
</config>
Controller File (PoproductController.php in \app\code\local\Company\Po\controllers\Adminhtml)
protected function _isAllowed() { return true; } protected function _initAction() { $this->loadLayout() ->_setActiveMenu('Purchase') ->_addBreadcrumb(Mage::helper('adminhtml')->__('Purchase Order Product'), Mage::helper('adminhtml')->__('Purchase Order Product')); return $this; } public function indexAction() { $this->_initAction() ->_addContent($this->getLayout()->createBlock('po/adminhtml_poproduct')) ->renderLayout(); } public function saveAction() { if ($data = $this->getRequest()->getPost()) { $model = Mage::getModel('po/poproduct'); if ($id = $this->getRequest()->getParam('po_product_id')) { $model->load($id); } $model_poproduct = Mage::getModel('po/poproduct'); $model_poproduct->setData($data)->setId($this->getRequest()->getParam('po_product_id')); try { $model_poproduct->save(); $product_id = $model_poproduct->getProductId(); $product = Mage::getModel("catalog/product")->load($product_id); $product->setSomeCustomDate("XYZ"); $product->save(); Mage::getSingleton('core/session')->addSuccess(Mage::helper('po')->__('Product has been saved succesfully')); Mage::getSingleton('core/session')->setFormData(false); return; } catch (Exception $e) { Mage::getSingleton('core/session')->addError($e->getMessage()); Mage::getSingleton('core/session')->setFormData($data); return; } } }
}