Extending Classes And Gii Workflow

Hey guys,

Right now I am extending the CActiveRecord class in my models, this in order to be able to tell when changes where made to my model between find and save.

I am doing this by adding an old_attributes property and assigning it on afterFind() and then comparing it to my array on beforeSave().

This works OK for me, but I feel that this is colliding with the correct workflow for creating yii models and controllers using gii.

In my typical flow, I would make small incremental changes to my database tables (happens, you know), and each time re-generate the model and curd using gii.

What would happen then is obviously my model files will be overwritten and everything lost.

I don’t know whether this is a feature request or if anyone has an existing solution, but I am looking for a way in which gii will not overwrite MY changes to the models/curd.

In apple’s cocoa, for example, you can add methods to a class without actually subclassing it. It is called categories and these methods are stored in a separate file, so if you regenerate your code using their auto-generator, your code isn’t overwritten.

I read somewhere that you can simply change the template used by gii. Is this a solution? how do you do this?

Thanks

Ilan

You could make a subclass of CActiveRecord (lets call it MyActiveRecord) where you have all the base code and you create your own Gii templates which generate models that extend this new MyActiveRecord class.

That’s what I do.

I imagined that this would be a way to do it.

How do you change the templates for Gii?

I learned to change Gii templates here. It’s pretty straightforward.

Look for the "Customizing Code Templates" chapter.

Great,

Actually now when I look at it, it seems that you don’t even have to change the template.

This is because the gii model generator page lets you choose the base class for this model.

So I think all you have to do is create your class which inherits from CActiveRecord, and then each time you use gii to create (or update) a model, you select your base class instead of CActiveRecord

Yes, that’s also possible. I prefer to use templates because I don’t always remember to specify the base class.

Yes I also think I might forget from time to time,

Thanks