将用户定义属性添加到域类

Men*_*kin 3 grails grails-domain-class

我要求允许用户在其中一个系统实体中定义一些自定义字段.你有任何建议/模式/插件,将帮助我将此功能添加到我的应用程序.

谢谢,

Meni

Bur*_*ith 6

您可以将Map属性添加到域类并在其中存储任意数据.但它相当有限.它将生成一个包含varchar(255)键和值的表,因此您需要自己管理任何类型的转换,例如

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()
Run Code Online (Sandbox Code Playgroud)

有关简要的在线文档,请参阅http://grails.org/doc/latest/上的"5.2.4集,列表和地图"部分.