且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-06 16:49:46

您可以将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.