Avoid overridind your modifications with model autogeneration

Hi,

I’m new to Yii.

I used the auto generation of the models described here http://www.yiiframework.com/doc/blog/prototype.scaffold

But later in the tutorial, we are asked to modified the auto generated files ( http://www.yiiframework.com/doc/blog/post.model ) . What happens if we change the database and want to generate again the corresponding Yii model ? The modification we had adde in the auto generated files will be lost ?

I must miss something.

Thanks for your help.

You need to do a manual merge (between the modified file and the newly generated file).

Oh thx but I’m quite disapointed by this answer. Would it be a good idea to extend the auto generated files and make the modifications only in those extended files ?

I think that the task to automatically update the model is too complex. I mean, how would it deal with a model, that are massively edited and supplied with custom methods, variables, validation etc…?

If you eg. change the name of a column in your db, you would have to change every reference to this propertys name, not only in the model and behaviors, but also in your controllers and views. It would be impossible to fully automate, and would very likely end up, with you loosing the control over whats get updated and not. And you would most likely have to go through the code anyway.

The best and safest way in my opinion is like Qiang said. To generate the model again and then manually copying and inserting the new code, that is needed, into the existing model.

sorry,I have no idea!

Open up the command shell like the documentation states in your first post.

Type:


model NewModel TableName

(where NewModel is a name you provide for the model (just name it something different than the tablename) and TableName is the name of the table which this model belongs to and is the name of the table in which you have made changes).

This command will create a new file in your models directory with the name NewModel.php. Open this file and from the same directory, open the existing model which likely is named after the tablename.

Depending on what you changed in the database and previously in the existing model, you will have to merge the data from this file (either replacing or inserting) into your existing model. If you eg. added a field to the table, you will look in the rules-method for validation rules and also the attributeLabels for labels or perhaps you have added a foreign key, then you will also look at the relations-method.

After that, and also depending of what kind of changes is made, you may have to update your controllers, views and so on.

There is no quick way of doing this, but it should not be to difficult. ::)

On Windows, I’m using an opensource program called WinMerge. You can open both model files to do a visual comparison. The differences in the files will be highlighted. You can then move the code from the old model to the new model. Also for Windows, there is a commercial product called BeyondCompare. There should be something similar available for other operating systems.

Thx a lot for those explanations. I think the 2 posts of Komodo should be added in the tutorial page, that’s VERY useful for beginners.