いくつかのフィールドを持つモデルがあり、このモデルのデータベースに新しいエントリを追加したいのですが、フィールドを1つだけ変更します。新しいインスタンスを作成したり、フィールドごとに1つずつ設定したりせずに、これを行うための最良の方法はありますか?
場合 :
public class MyModel extends Model {
public String firstname;
public String lastname;
public String city;
public String country;
public Integer age;
}
そして私が実際に持っているコード
MyModel user1 = MyModel.findById(1);
MyModel user2 = new MyModel();
// is there a way to do it with clone or user1.id = null ? and then create()?
// Actually, I do that :
user2.firstname = "John";
user2.lastname = user1.lastname;
user2.city = user1.city;
user2.country = user1.country;
user2.age = user1.age;
user2.create();
私が探しているのは、次のようなことです。
MyModel user1 = MyModel.findById(1);
MyModel user2 = clone user1;
user2.firstname = "John";
user2.create();
また
MyModel user = MyModel.findById(1);
user.id = null;
user.firstname = "John";
user.create();
しかし、そのようにすることが正しいかどうかはわかりません。