Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Rewrite model not working
 
albupl
Jr. Member
 
Total Posts:  3
Joined:  2011-07-18
 

Hi everyone,

I’m trying to rewrite a core model in the last version of Magento but i can’t do it. I have did a lot of searchs in this forum and in other pages but i can’t find my mistake. Can anyone help me?

My code are this (it’s very simple):

app/etc/modules/test_replacemodel.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
test_replacemodel>
            <
active>true</active>
            <
codePool>local</codePool>
            <
depends>
                <
Mage_Customer />
            </
depends>
        </
test_replacemodel>
    </
modules>
</
config>

app/code/local/test/replacemodel/etc/config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
test_replacemodel>
            <
version>0.1.0</version>
        </
test_replacemodel>
    </
modules>
    <global>
        <
models>
            <
replacemodel>
                <class>
replacemodel_Model</class>
            </
replacemodel>
            <
customer>
                <
rewrite>
                    <
customer>test_replacemodel_Model_Customer</customer>
                </
rewrite>
            </
customer>
        </
models>
    </global>
</
config>

app/code/local/test/replacemodel/Model/Customer.php

<?php

class test_replacemodel_Model_Customer extends Mage_Customer_Model_Customer
{

    
public function getName()
    
{
        $name 
parent::getName();
        
$name $name ' loves Magento';
        return 
$name;
    
}

}

I have also deleted the cache and checked the files permissions.

Thanks in advance.

Alberto.

 
Magento Community Magento Community
Magento Community
Magento Community
 
albupl
Jr. Member
 
Total Posts:  3
Joined:  2011-07-18
 

Solved. I don\’t know why but i changed the namespace and module name to other names with the first letter in capital and it works fine. Someone can explain me why?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Because this is the Magento (Zend framework) standard.
Classes are named with capital letters at the beginning and after each underscore.
Even if in PHP class names are case insensitive, the file names are not (at least on Linux).
If you name your class ‘test_replacemodel_Model_Customer’ the autoloader will search for it in this file:
test/replacemodel/Model/Customer.php that is not the same as:
Test/Replacemodel/Model/Customer.php

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
albupl
Jr. Member
 
Total Posts:  3
Joined:  2011-07-18
 

Thank you very much Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Vishal Surani
Jr. Member
 
Avatar
Total Posts:  14
Joined:  2010-04-13
 

Hello bijalbhavsar99,

I have tried same but I couldn’t find success,

but you can use Catalog/Model/Resource/Eav/Attribute.php

I have Override above file in custom module and its working fine

Here is the override code

<global>
 <
models>
      <
catalog>
          <
rewrite>
              <
resource_eav_attribute>Namespace_Modulename_Model_Resource_Eav_Attribute</resource_eav_attribute>
          </
rewrite>
      </
catalog>
   </
models>
</global>

Please let me know if you have any issue with this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

@bijalbhavsar99
What you did looks ok. I think you have successfully overridden the mode you want.
Here is how You can check to make sure.
Create this file on the same level as index.php. Let’s call it test.php

<?php
error_reporting
(E_ALL E_STRICT);
define('MAGENTO_ROOT'getcwd());
$mageFilename MAGENTO_ROOT '/app/Mage.php';
require_once 
$mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors'1);
umask(0);
Mage::app();
echo 
get_class(Mage::getModel('eav/entity_attribute'));
Now call in the browser mysite.com/test.php. If you see ‘Test_Award_Model_Entity_Attribute’ then you did it ok. If you see Mage_Eav_Model_Entity_Attribute then it means it didn’t work.

I think it works.
Your problem is different.
You probably are trying to see you changes in a class that extens Mage_Eav_Model_Entity_Attribute. (for example ‘Mage_Catalog_Model_Entity_Product_Attribute_Frontend_Image’ - but there are may others)
Even if you override the Mage_Eav_Model_Entity_Attribute the classes that extend it still extend the original class and not yours.
Here are more details about this: http://marius-strajeru.blogspot.ro/2011/09/why-cant-you-override-some-classes-in.html

Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top