Avoid overridind your modifications with model autogeneration
Posted 04 March 2010 - 03:54 PM
I'm new to Yii.
I used the auto generation of the models described here http://www.yiiframew...totype.scaffold
But later in the tutorial, we are asked to modified the auto generated files ( http://www.yiiframew...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.
Posted 04 March 2010 - 07:19 PM
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.
Posted 04 March 2010 - 09:30 PM
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.
Posted 05 March 2010 - 12:04 AM
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.