Skip to content

Commit 86b8be5

Browse files
Merge pull request #295 from MasoniteFramework/feature/286
added update_or_create
2 parents b12e4a7 + 1996211 commit 86b8be5

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/masoniteorm/models/Model.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,17 @@ def to_json(self):
380380
"""
381381
return json.dumps(self.serialize())
382382

383-
def update_or_create(self):
384-
pass
383+
@classmethod
384+
def update_or_create(cls, wheres, updates):
385+
self = cls()
386+
record = self.where(wheres).first()
387+
total = {}
388+
total.update(updates)
389+
total.update(wheres)
390+
if not record:
391+
return self.create(total)
392+
393+
return self.where(wheres).update(total)
385394

386395
def relations_to_dict(self):
387396
"""Converts a models relationships to a dictionary
@@ -422,10 +431,6 @@ def _update_timestamps(self, date=None):
422431
def _current_timestamp(self):
423432
return datetime.now()
424433

425-
@staticmethod
426-
def set_connection_resolver(self):
427-
pass
428-
429434
def __getattr__(self, attribute):
430435
"""Magic method that is called when an attribute does not exist on the model.
431436

0 commit comments

Comments
 (0)