Yii Framework Forum: Avoid overridind your modifications with model autogeneration - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Avoid overridind your modifications with model autogeneration Rate Topic: -----

#1 User is offline   jptsetung 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 04-March 10

Posted 04 March 2010 - 03:54 PM

Hi,

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.
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 04 March 2010 - 04:28 PM

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

#3 User is offline   jptsetung 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 04-March 10

Posted 04 March 2010 - 06:02 PM

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 ?
0

#4 User is offline   Theill11 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 03-March 10
  • Location:@ localhost

Posted 04 March 2010 - 07:19 PM

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.
1

#5 User is offline   long long ago 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 04-March 10

Posted 04 March 2010 - 08:32 PM

sorry,I have no idea!
0

#6 User is offline   Theill11 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 03-March 10
  • Location:@ localhost

Posted 04 March 2010 - 09:30 PM

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. ::)
0

#7 User is offline   cmx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 21-November 09
  • Location:California USA

Posted 05 March 2010 - 12:04 AM

View PostKomodo, on 04 March 2010 - 09:30 PM, said:

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


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.
0

#8 User is offline   jptsetung 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 04-March 10

Posted 05 March 2010 - 04:52 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users