Search code examples
magentomodeloverwrite

Magento Overwrite Model from Mage_Core


When I try to overwrite a model from Mage_Catalog (Mage_Catalog_Model_Product_Image for instance) I have no problem but when I try to do it for a model from Mage_Core (Mage_Core_Model_File_Uploader) it doesn't work.

Is there some kind of protection on Mage_Core? I cannot use an observer instead so I really have to get that working.

My config.xml:

<models>
    <myshop>
        <class>Adrien_Myshop_Model</class>
    </myshop>
    <core>
        <rewrite>
            <file_uploader>Adrien_Myshop_Model_Core_File_Uploader</file_uploader>
        </rewrite>
    </core>
    <catalog>
        <rewrite>
            <product_image>Adrien_Myshop_Model_Catalog_Product_Image</product_image>
        </rewrite>
    </catalog>
</models>

My new Uploader model:

class Adrien_Myshop_Model_Core_File_Uploader extends Mage_Core_Model_File_Uploader
{
    protected function _moveFile($tmpPath, $destPath)
    {
        Mage::log("custom upload");
        return parent::_moveFile($tmpPath, $destPath);
    }

}

I put a debug log into Mage_Core_Model_File_Uploader::_moveFile as well, when I run the script I get only the log from Mage_Core_Model_File_Uploader.

However, when I test it with Catalog_Product_Image I properly get both debug logs.


Solution

  • If you grep the source code for that class name you can see that it is invoked directly in PHP via new, hence the inability to rewrite.

    If you must rewrite this class, you will need to do so by copying it to the local codepool and making your changes therein, noting that you'll need to merge in new changes as you upgrade the application.