简体   繁体   English

向Grails域类添加字段?

[英]Add a field to a Grails Domain Class?

I would like to add a field to an existing domain class. 我想向现有的域类添加一个字段。 I do not want to lose my view files however as I know will happen if i run generate-all. 我不想丢失我的视图文件,但是我知道如果我运行generate-all,将会发生这种情况。 Is there another way I can update the mapping etc? 有没有其他方法可以更新映射等?

I think this is a common concern. 我认为这是一个普遍关注的问题。

This is not a direct solution to your problem, but this is what I do and works very well for me. 这不是直接解决您的问题的方法,但这是我的工作,对我来说非常有效。

  1. I never make direct modifications to the grails scaffolded artifacts (views and controllers) 我从未对grails脚手架的工件(视图和控制器)进行直接修改。

  2. I keep my production views/controllers separate from the scaffolded artifacts; 我将生产视图/控制器与脚手架的工件分开; through I use the scaffolded as the starting point of my application controllers and views. 通过使用脚手架作为我的应用程序控制器和视图的起点。

  3. If there are changes to the domain model, I re-generate the views and copy-paste (wherever possible) from scaffolded artifacts to hand-coded artifacts. 如果域模型发生更改,我将重新生成视图,并在可能的情况下将粘贴的信息从脚手架的构件复制到手工编码的构件。

  4. At some point, I either delete all the scaffolded artifacts from the app or just protect access to them. 在某个时候,我要么从应用程序中删除所有支架式工件,要么只是保护对它们的访问。

Hope this helps. 希望这可以帮助。

There are a couple of ways to do this. 有两种方法可以做到这一点。

  1. If you have not modified your generated views too much and are using version control, you could allow grails to overwrite your views and then merge the changes to the new templates with what is in version control. 如果尚未对生成的视图进行过多修改,并且正在使用版本控制,则可以允许grails覆盖视图,然后将更改与版本控制中的内容合并到新模板中。

  2. If you have modified your generated views, you could just answer no to the prompt for overwriting the views. 如果您修改了生成的视图,则只需对覆盖视图的提示回答否。 If you are only adding a new field, changing a field name or something simple like that, modifying the view templates should be pretty straight forward to do manually. 如果您只是添加一个新字段,更改字段名称或类似的简单操作,则修改视图模板应该很容易手动完成。

You would deal with any possible changes to the Controllers in the same way. 您将以相同的方式处理对Controllers的任何可能更改。

不要重新生成视图,只需将另一个相似的字段复制并粘贴到要放入所需视图中的字段即可。

just declare it: 只需声明:

class MyDomain {
    String newField;
}

Should be all you need to do. 应该是您所需要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM