Trying to override _beforeSave method and Magento calling both custom method and core method as well
All I am trying to override the _beforeSave() method in resource model Mage_Customer_Model_Resource_Customer.
There is no problem in overriding as I can get the call to my custom _beforeSave method in my Custom Class.
Config for my custom Class is:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Abc_Customer>
<version>0.1.0</version>
</Abc_Customer>
</modules>
<global>
<models>
<cust>
<class>Abc_Customer_Model</class>
<resourceModel>cust_resource</resourceModel>
</cust>
<cust_resource>
<class>Abc_Customer_Model_Resource</class>
</cust_resource>
<customer_resource>
<rewrite>
<customer>Abc_Customer_Model_Resource_Customer</customer>
</rewrite>
</customer_resource>
</models>
</global>
</config>
After that I have created the Abc_Customer_Model_Resource_Customer class, copied the _beforeSave method from the core class and changed according to need.
But the problem is now that magento is calling both of the methods.
How can I stop this behaviour and just let only my custom method to run?
Please suggest.
Thanks
2 Answers 2
The problem is not actually a problem.
If you copied the original _beforeSave() then this line is in there:
parent::_beforeSave($customer);
This the original class this was used to call the method in the parent class: Mage_Eav_Model_Entity_Abstract::_beforeSave().
This is what basically happens to your new class. The method _beforeSave() from the parent is called from your method. Only now the parent of your class is Mage_Customer_Model_Resource_Customer, so the original method is called.
What you need it something like parent::parent::_beforeSave($customer) which in PHP can be done by using the grandparent_class_name::method
parent::_beforeSave($customer)
with
Mage_Eav_Model_Entity_Abstract::_beforeSave($customer)
-
4PHP actually does support doing parent::parent by using the parent class name::method, thus replacing the 'parent::_beforeSave($customer) with Mage_Eav_Model_Entity_Abstract::_beforeSave($customer)' will work to call the grandparent class function. This has (at times) caused quite a debate in our office, as some claim this breaks OOP conventions ;) Ref: php.net/manual/en/language.oop5.paamayim-nekudotayim.php "...allows access to static, constant, and overridden properties or methods of a class." ref: bugs.php.net/bug.php?id=42016ProxiBlue– ProxiBlue2013年08月08日 09:37:40 +00:00Commented Aug 8, 2013 at 9:37
-
@ProxiBlue OK....so I learn new thing every day :). I did not know that. Feel free to edit my answer.Marius– Marius2013年08月08日 09:40:05 +00:00Commented Aug 8, 2013 at 9:40
-
I appreciate your help. I was wondering, is there could be any other way to do the same like this could get confusing for other people to understand.Ricky Sharma– Ricky Sharma2013年08月08日 14:24:28 +00:00Commented Aug 8, 2013 at 14:24
-
@RIK. Well...everything can be confusing to some people. This is the way magento works for extending classes.Marius– Marius2013年08月08日 14:40:33 +00:00Commented Aug 8, 2013 at 14:40
-
@Marius - Please note that as of PHP5.4, calling the parent::parent method statically as described now throws a strict warning. ref: stackoverflow.com/questions/10768576/… (to notice is that the PHP docs for the :: does not reflect this)ProxiBlue– ProxiBlue2013年10月19日 05:37:04 +00:00Commented Oct 19, 2013 at 5:37
You need to know where the second call is originating from.
It could be that something else is extending that core class, and also doing a save, thus making the second call.
If you are using a debugger, then place a breakpoint on the core _beforeSave() and check the stack trace to see where the call is originating from. You'd be interested in the calling class instance name.
If no debugger, use PHP debug_backtrace to get a trace of the execution to that call.
Once you can find where it originates from, then you'd most likely solve the issue.