3

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

asked Aug 8, 2013 at 6:07

2 Answers 2

9

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)

ProxiBlue
9,9463 gold badges35 silver badges61 bronze badges
answered Aug 8, 2013 at 8:14
5
  • 4
    PHP 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=42016 Commented 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. Commented 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. Commented Aug 8, 2013 at 14:24
  • @RIK. Well...everything can be confusing to some people. This is the way magento works for extending classes. Commented 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) Commented Oct 19, 2013 at 5:37
2

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.

answered Aug 8, 2013 at 8:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.