我有这个域类,比方说:
class Person {
String name
Integer age
//car data that needs to be shown and filled in views
//but not persisted in Person class
String model
String color
static afterInsert = {
def car = new Car(model: model, color: color)
car.save()
}
}
class Car {
String model
String color
}
我需要的是在Person
视图(create
和edit
)中显示在Person
类中定义的模型和颜色属性,但这些属性不必与该类一起持久化。这些数据model
和color
必须使用Car
域类(可能使用afterInsert
事件)来持久化。换句话说,我需要使用来自另一个域类的视图来保存来自域类的数据。
提前谢谢。
您可以在希望GORM忽略的属性上使用瞬态,例如
class Person {
static transients = ['model', 'color']
String name
Integer age
//car data that needs to be shown and filled in views
//but not persisted in Person class
String model
String color
..
}
只是好奇,但有没有原因你不使用关联
class Person {
..
static hasMany = [cars: Car]
}
class Car {
..
static belongsTo = [Person]
static hasMany = [drivers: Person]
}
或组成
class Person {
Car car
}
或者简单地与多个域进行数据绑定
//params passed to controller
/personCarController/save?person.name=John&age=30&car.model=honda&car.color=red
//in your controller
def person = new Person(params.person)
def car = new Car(params.car)