Magento – Resource Model rewriting issue (1.7) [Resolved]

overridesresource-model

[EDIT]
Ok, don't bother reading all the following suff, I just noticed I was saving my module's config.xml file in the module's root directory instead of etc directory… So it works fine now. Maybe my explanations can help someone else though. TPs 1 to 4 (model, block, resource model, helper rewrites) are ok.
[/EDIT]

Hello !
I'm pretty new to Magento, following a tutorial where I'm supposed to rewrite components. I'm working on a brand new install with Magento website's sample data.
So far, model, block and helper rewriting worked fine, although I can't make Product Resource Model rewriting work. My new resource model class is plainly ignored. Maybe you could help?

It may have something to do with the fact that the configuration files in the tutorial are slightly different (there is this node that I don't have : catalog_resource_eav_mysql4 ).

Some more info :

Here is a part of the app/code/core/Mage/Catalog/etc/config.xml :

<global>
        <models>
            <catalog>
                <class>Mage_Catalog_Model</class>
                <resourceModel>catalog_resource</resourceModel>
            </catalog>
            <catalog_resource>
                <class>Mage_Catalog_Model_Resource</class>
                <deprecatedNode>catalog_resource_eav_mysql4</deprecatedNode>
                <entities>
                    <product>
                        <table>catalog_product_entity</table>
                    </product>

Here is my module declaration file First_Module.xml in app/etc/modules (it must be ok because the other rewrites in this module worked so far) :

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <First_Module>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </First_Module>
    </modules>
</config>

My module under local contains :
First/Module/etc/config.xml :

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <First_Module>
            <version>1.0</version>
        </First_Module>
    </modules>
    <global>
        <!-- TP 1 Override Catalog Product Model -->
        <models>
            <catalog>
                <rewrite>
                    <product>First_Module_Model_Product</product>
                </rewrite>
            </catalog>
            <!-- TP3 Override Catalog Product Resource Model --> 
            <!-- FAIL #1 :-->
            <catalog_resource>
                <rewrite>
                    <product>First_Module_Model_Resource_Product</product>
                </rewrite>
            </catalog_resource>
            <!-- FAIL #2 : -->           
<!--             <catalog_resource_eav_mysql4>
                <rewrite>
                    <product>First_Module_Model_Resource_Product</product>
                </rewrite>
            </catalog_resource_eav_mysql4> -->
        </models>
       <!--  TP2 Override Catalog Product View Block -->
        <blocks>
            <catalog>
                <rewrite>
                    <product_view>First_Module_Block_Product_View</product_view>
                </rewrite>
            </catalog>
        </blocks>
        <!-- TP4 Override Sales Helper -->
        <helpers>
            <sales>
                <rewrite>
                    <data>First_Module_Helper_Data</data>
                </rewrite>
            </sales>
        </helpers>
        <!-- TP5 Override Customer Address Helper -->
    </global>
</config>

This, Mage_Catalog_Model_Resource_Product, if I get it right, is the original resource model class that I'm supposed to rewrite (I only paste the beginning because it's huge):

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


/**
 * Product entity resource model
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Catalog_Model_Resource_Product extends Mage_Catalog_Model_Resource_Abstract
{

….

And this is the class I created to override the previous resource model, in app/code/local/First/Module/Model/Resource/Product.php :

<?php

class First_Module_Model_Resource_Product extends Mage_Catalog_Model_Resource_Product
{
}

Eventually, here is my test script test.php in magento root (which worked for TPs 1 and 2, I leave them for comparison) :

<?php
    // chargement du minimum de magento pour les tests
    include 'app/Mage.php';
    umask(0);
    Mage::app();

    /*    
    Overrides :
    - catalog product model
    - catalog product view block
    - catalog product resource model
    - sales helper
    - customer address helper
    - Display changed class in cms index controller
    - Revert all changes in core files
    */


    /*********** TP 1 Override Catalog Product Model **********/

    $model = Mage::getModel('catalog/product');
    echo get_class( $model );
    // affiche Mage_Catalog_Model_Product

    var_dump($model->getName());
    // affiche null car l'instance de product ne contient aucune donnée

    // charge les données d'un produit en mémoire 
    // $model = Mage::getModel('catalog/product')->load(166);
    $model->load(166);
    var_dump($model->getName());
    // affiche string(17) "HTC Touch Diamond"

    /************** TP2 Override Catalog Product View Block ***************/

    $block = Mage::app()
                ->getLayout()
                ->createBlock('catalog/product_view');

    echo '<br/><br/>'.get_class( $block );
    echo ' '.$block->test();


    /************** TP3 Override Catalog Product Resource Model ***************/

    $rm = Mage::getResourceModel('catalog/product');
    echo '<br/><br/>'.get_class( $rm );

    /************** TP4 Override Sales Helper ***************/

    $helper = Mage::helper('sales');
    echo '<br/><br/>'.get_class( $helper );

    /************** TP5 Override Customer Address Helper ***************/
    /************** TP6 Display changed class in cms index controller ***************/

This is what is displayed in my browser when I navigate to localhost/magento/test.php/

First_Module_Model_Productstring(0) "" string(17) "HTC TOUCH DIAMOND"

First_Module_Block_Product_View test First_Module_Block_Product_View

Mage_Catalog_Model_Resource_Product

Mage_Catalog_Model_Resource_Product shouldn't appear here, it should be
First_Module_Model_Resource_Product.

I hope I'm being clear enough, and would greatly appreciate any info about this issue 🙂

Best regards.

Laura P.

Best Answer

The code looks ok:

<models>
    <catalog_resource>
         <rewrite>
              <product>First_Module_Model_Resource_Product</product>
         </rewrite>
     </catalog_resource>
</models>

Refresh the magento cache if you have the cache enabled.

For debugging purposes you could check the Mage_Core_Model_Config::getGroupedClassName This is the location where the class name is retrieved.