有没有最好的方法来克隆仅更改一个条目的模型?
|
我有一个带有某些字段的模型,我想在中添加一个新条目
此模型的数据库,但只更改一个字段。在那儿
这样做的最佳方法,而无需创建新实例,并且
每个字段一一设置?
案件 :
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();
但是我不知道那样做是否正确。
没有找到相关结果
已邀请:
1 个回复
壬驴拿歪竞
接口,然后调用
方法将返回原始对象的浅表副本。要获取深层副本,请覆盖它,在此处您可以将id设置为
并复制非原始字段。
保留克隆的对象: