0

我有这个域类,比方说:

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视图(createedit)中显示在类中定义的模型和颜色属性,Person但这些不必与此类保持一致。这些数据modelcolor必须使用Car域类进行持久化,可能使用afterInsert事件。换句话说,我需要使用来自另一个域类的视图来保存来自域类的数据。

提前致谢。

4

1 回答 1

1

您可以在您希望 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)
于 2013-07-23T22:44:58.863 回答