Override MVC methods while keeping arguments
When extending functions in Sugar's MVC framework, it's quite possible you need to call the parent method before or after your code to allow for the original code to run. In the official documentation and in SugarCRM's code itself, the "parent::method()" method is pretty common. However, this can cause a problem with changing function arguments.
For instance, when overriding the save function of a bean, one can use:
<?php
function save($check_notify = FALSE) {
$this->fill_name();
parent::save($check_notify);
}
?>At this moment, when the arguments list or order of the SugarBean method change, this call will prevent the arguments from passing through and you're putting your system at risk. Using the code below, all arguments are properly passed to the parent method:
<?php
function save() {
$args = func_get_args();
$this->fill_name();
call_user_func_array(array('parent', __FUNCTION__), $args);
}
?>Using this code as a template, you'll make sure you're not interfering with future bug fixes or feature updates!
- Topic:
- sugardev.net's blog
- Login or register to post comments

Comments
How do I use this tool
Hi
I am new to Sugar. Thanks for the tool
Do you mean the tools module?
Do you mean the tools module? You can download it from SugarForge and install it with the module loader. Please note that this article is about programming for SugarCRM and the intended audience for this article and the module are developers and administrators.