Extend a model

I have a module which I want to improve without having to change code inside it to future benefit from updates on that module.

I succeeded into extending the controllers without changing the module’s code(using controllerMap inside module’s config).

I want also to extend a model from that module without having to change module’s code(i.e change module’s class instantiation name inside controllers).

Could you please give me some advice as I didn’t find yet a solution on any of the topics I have read?

Is not clear what you are trying to achive.

Do you want to create some master class for model?

Simply extend CActiveRecord and then your models will extend this masterclass.

If you want to achive something different, try to explain a bit more what do you need.

I want to add or extend functions to models inside that module and without having to change the module’s code

i.e I have a model named user inside that module and I create an extension of that model named UserExt. Inside UserExt I override one of User’s functions. Now when we call that User’s function inside the module I want it to go to the new function and not use the old one without having to change the instantiations inside the module (meaning using new UserExt instead of new User )

You should work in the opposite way.

Create a base model UserBase with the base utility for user.

In the module you can create a model named User that extends UserBase and add some functionalty, tipically chages the rules.

Thank you for your reply.

But perhaps I should rephrase my question:

I currently downloaded and installed a module for USER Authentication. It is quite a good module, except that i want to change some minor things in it’s user model, like adding an extra field.

I know I can simply go in the module and edit it, but then I would break upgrade capabilities. I would not benefit anymore of any updates that module has.

Therefore, I want to extend this model, in the same way you can extend a module’s controller, without having to touch any part of the existing code.

Please let me know if this is better.



It’s a bit ugly shortcut, but try to make model name that you can change in config and then use it unstead of User

Thanks. But how can I change in config? Can you please give me an example

Let’s say the module’s model class/file is named User.php …

A wordaround could be to rename it to UserBase.php and create your extended class as User extends UserBase and override whatever you need.

Then in every upgrade you just have to remember to rename that file. I know it’s not the “Right way” to do it, but it’s the only one i could think of right now …

At least that way you don’t need to change any existing module’s code :)

Couldn’t you just extend the model?

class newModel extends oldModel{

   public function oldFunction(){

      // new code



and when creating the new object just use:


$model->oldFunction(); // new code