将用户定义属性添加到域类 [英] add user define properties to a domain class

查看:95
本文介绍了将用户定义属性添加到域类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求允许用户在其中一个系统实体中定义一些自定义字段。

谢谢,



你有任何建议/模式/插件可以帮助我将此功能添加到我的应用程序中。 p> Meni

解决方案

您可以将Map属性添加到您的域类,并在其中存储任意数据。虽然这是相当有限的。它会生成一个varchar(255)键和值的表,所以你需要自己管理任何类型的转换,例如

  class事物{
字符串名称
映射extraProperties = [:]
}

int age = 123
def thing = new Thing(name:'whatever' )
thing.extraProperties.age = age.toString()
thing.save()

...

def thing = Thing.get (thingId)
int age = thing.extraProperties.age.toInteger()

请参阅5.2.4设置,列表和地图,位于 http://grails.org/doc/latest/查看简要的在线文档。


i have a requirement to allow the user to define some custom field in one of the system entities. do you have any suggestion/pattern/plugin that will help me add this feature to my application.

thanks,

Meni

解决方案

You can add a Map property to your domain class and store arbitrary data there. It's rather limited though. It will generate a table with varchar(255) keys and values, so you need to manage any type conversions yourself, e.g.

class Thing {
   String name
   Map extraProperties = [:]
}

int age = 123
def thing = new Thing(name: 'whatever')
thing.extraProperties.age = age.toString()
thing.save()

...

def thing = Thing.get(thingId)
int age = thing.extraProperties.age.toInteger()

See section "5.2.4 Sets, Lists and Maps" at http://grails.org/doc/latest/ for the brief online docs.

这篇关于将用户定义属性添加到域类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆