Open magnumrocha opened 10 years ago
you must fetch the model with primaryKeyField=1 and then set someField at this model. like this: ModelClass obj = Query.one(ModelClass.class,"Select * from modelclass where"+primaryKeyField+"=1"); if(obj==null){ obj = new ModelClass(); obj.primaryKeyField = 1; }
obj.someField = "new value"; obj.saveAsync(...);
Ok @lsjwzh,
I understand this solution. It's works fine. But, why not update the register with the existent id in save() and saveAssync() method ?? without execute a query before...
Instead to do a query on database and a write action, do only a write action. This help to avoid unnecessary disk operation.
What do you think about ?
Hi, magnumrocha , Do you mean to update the fields which has not null value,instead to update all fields?
Yes @lsjwzh, exactly it.
Update only the fields setted on Model instance.
I also like this feature!!!! But, it need some extra works to maintain the state. And may be need to intercept POJO's setter. I'm doing some works to improve sprinkles,i have implemented some features like relationship,entity cache,auto create table/columns,fluent query api. And i also consider "changes monitor",but i remain have no good idea on it.
Hi,@magnumrocha How do you think about adding an extra method to do this in Sprinkles? saveNotNull or other name?
Hi @lsjwzh,
I guess that saveNotNull() is a good name, but, just to know about: the original methods: save() and saveAssync() can't be updated with new rules ??
When I save a model that already exists in database, but creating a new instance and set just some fields (the primary key and some fields that I want update the value in database register), Sprinkles is cleaning the fields that is not setted on object instance.
Eg.:
ModelClass obj = new ModelClass(); obj.primaryKeyField = 1; obj.someField = "new value"; obj.saveAsync(...);
Suppose that existe another field called "anotherField" on Model Class. The anotherField value is cleaned on database.