0

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 --------

  1. 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>
  1. 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;
            }
        }
    }
    

    }

2
  • Save call model save not controller save
    – user52581
    Commented Apr 12, 2017 at 14:51
  • @Feedlay Technologies: Okay, It's wired for me. I am thinking its ->save() will call controller action. Can you please tell me which save method executed?
    – Ishan Jain
    Commented Apr 13, 2017 at 4:35

2 Answers 2

0

I can see you have called $product_id in controller file. Can you debug once on this. I assume $product_id = $model_poproduct->getProductId(); is not calling product model. please call product model as per your module.

1
  • Hmm, Accually i tried to mage log at the start of action function "public function saveAction()" i.e. first line of function is create a log file. But it's not creating log file to me.
    – Ishan Jain
    Commented Apr 13, 2017 at 13:55
0

Here Mage::getModel('po/poproduct')->getCollection() you are calling Model and executing the save function.
You are overwriting the controller where you need to overwrite the model file.
SaveAction() will be executed from the URL.

2
  • How can i execute this from URL?
    – Ishan Jain
    Commented Apr 13, 2017 at 13:52
  • Define proper routes to the url, it will be something like domainname.com/modulename/controllername/action Commented Apr 13, 2017 at 13:59

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.