Rebuild the whole model without overriding the existing code

Hello Yii forum,

Is there a way we can build Yii model without deleting the my code in Model?

Overwrite and diff are the only options for me to see before and after my Model is overwritten without existence of my additional useful functions (that I implemented)inside the model :-[ . This means my code within the model will also be deleted when I rebuild my models.

Please help me anyone who know how to deal with this issue, it will be very thankful and helpful to other Yii user as well.


As i can see, you say about gii. No, there is no way to do this. At least now.


Thank you for your reply. But there is really no [font="Times New Roman"]way to extend the model which has multiple relations[/font], but manual modifying?

Actually, I have used Symfony framework and that function is available to satisfy this task. I thought it is also available in Yii Framework as well.

Really, there is no way to do this… yet ;) Try to post ticket about this feature.


  1. Why you want to recreate your models?

  2. If you really need to do this, you can use the auto generated model as base model and add your custom code to a class that extends this base model


Thanks!! Yeah I think I will have to do that way finally, though it takes time.

I definitely need to re-build the models to handle the system extension in the future. As I am only working on the beginning of implementation phase of the system, there is not a big deal to handle the change of my models, but some of my models have so many relatives so when I want to change a model, it will effect all relative models as well. As the result, I have to spend more time repairing(Ex: taking the whole related helper function in my old model to my new model…etc) the whole system every time I regenerate the model.

Hi there!

I’ve also wanted to create extensions to various auto-generated models, for example to add helper functions to a model class that represents a database table. I went with the subclass option too, so that I can still regenerate the underlying base class as necessary without affecting the customizations in my subclass.

One thing to note is that I had to make sure to override the model() static function in my subclass to make sure everything worked nicely. For example, consider a User model that is generated by Gii and a MyUser subclass of User. In order for the following to still work (i.e., to return an instance of MyUser, not User):

$user = MyUser::model()->findAll();

You should define your subclass like:

class MyUser extends User


        public static function model($className=__CLASS__)


		return parent::model($className);


	... the rest of your functions ...