How To Override Cactiverecord::save()

Hi Folks,

I want over-ride the save function of CActiveRecord like this way,

public function save()




            /***do calculations on attributes here ***/      

            return parent::save();

        } catch (Exception $exception)


            echo $exception->getMessage();

            return false;



So I will do change on attributes before saving, … in this way I am keeping, my business in model.

But, when I do error_reporting(-1)… means all errors are shown, I am getting the error message as,

Declaration of modelname::save() should be compatible with that of CActiveRecord::save()

As you see in the reference, save() has a signature like this:

public function save($runValidation=true,$attributes=null)

You have use the same signature for your override.

Note that your current version of save() doesn’t handle a call to save() with additional parameters, e.g. “save(false)”.

Why instead of Overloading the Save function you just call the beforeSave() or afterSave() functions in the Controller ?

If it is a validation you have the beforeValidate() and afterValidate() functions too.


I think it’s better not to handle and consume the exceptions inside save() method.

When you want to save AR in a transaction, you would want save() to throw exception.


we can use beforeSave() or afterSave() in model … not in controller…

but I want to catch the unexpected errors/exception in save. So I am creating my own function for that…

thanks… I will use same signature for my save function… now it’s working…

Hi softark,

you are right, we don’t use own save function with exception in AR transaction.

at that case we will use AR save function…