Is there a generic way of setting field values in mapper from list of values?

StackOverflow https://stackoverflow.com/questions/8223548

  •  06-03-2021
  •  | 
  •  

質問

Is there a way of creating method for setting the value in the Model's fields without setting the values explicitly like -

ModelName.create.fieldName1("value").fieldName2("value2") and so on

Can we iterate through all available fields of that model and set their values form some list-of-values ?

something like ...

Model.allFields.foreach((fld)=> {
fld.set(valueList(indx)); indx+=1
}

Actually I want to set values into all models using some generic method that works for all models.

役に立ちましたか?

解決

According to my comment:

val list = List(...)
val record = YourRecordClass.createRecord
record.allFields.zip(list).foreach {case(field,value) => field.setFromAny(value)}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top