From 51260aa87f5733b9219fcf0c04bf97b9c081bc0d Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Sat, 18 Feb 2023 20:09:23 -0500 Subject: [PATCH 1/2] Fixed mass assign issue on save --- src/masoniteorm/models/Model.py | 4 ++-- src/masoniteorm/query/QueryBuilder.py | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index 743c2483..c72bb7c5 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -863,7 +863,7 @@ def save(self, query=False): if not query: if self.is_loaded(): - result = builder.update(self.__dirty_attributes__) + result = builder.update(self.__dirty_attributes__, ignore_mass_assignment=True) else: result = self.create( self.__dirty_attributes__, @@ -876,7 +876,7 @@ def save(self, query=False): return result if self.is_loaded(): - result = builder.update(self.__dirty_attributes__, dry=query).to_sql() + result = builder.update(self.__dirty_attributes__, dry=query, ignore_mass_assignment=True).to_sql() else: result = self.create(self.__dirty_attributes__, query=query) diff --git a/src/masoniteorm/query/QueryBuilder.py b/src/masoniteorm/query/QueryBuilder.py index 2e98c5dc..0454b644 100644 --- a/src/masoniteorm/query/QueryBuilder.py +++ b/src/masoniteorm/query/QueryBuilder.py @@ -499,6 +499,7 @@ def create( query: bool = False, id_key: str = "id", cast: bool = False, + ignore_mass_assignment: bool = False, **kwargs, ): """Specifies a dictionary that should be used to create new values. @@ -518,7 +519,8 @@ def create( # Update values with related record's self._creates.update(self._creates_related) # Filter __fillable/__guarded__ fields - self._creates = model.filter_mass_assignment(self._creates) + if not ignore_mass_assignment: + self._creates = model.filter_mass_assignment(self._creates) # Cast values if necessary if cast: self._creates = model.cast_values(self._creates) @@ -1388,6 +1390,7 @@ def update( dry: bool = False, force: bool = False, cast: bool = False, + ignore_mass_assignment: bool = False ): """Specifies columns and values to be updated. @@ -1396,6 +1399,7 @@ def update( dry {bool, optional}: Do everything except execute the query against the DB force {bool, optional}: Force an update statement to be executed even if nothing was changed cast {bool, optional}: Run all values through model's casters + ignore_mass_assignment {bool, optional}: Whether the update should ignore mass assignment on the model Returns: self @@ -1407,7 +1411,8 @@ def update( if self._model: model = self._model # Filter __fillable/__guarded__ fields - updates = model.filter_mass_assignment(updates) + if not ignore_mass_assignment: + updates = model.filter_mass_assignment(updates) if model and model.is_loaded(): self.where(model.get_primary_key(), model.get_primary_key_value()) From c4fd7870f4e10df6e87bf11241ff39d6d9269ecb Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Sat, 18 Feb 2023 20:12:41 -0500 Subject: [PATCH 2/2] Format --- src/masoniteorm/models/Model.py | 9 +++++++-- src/masoniteorm/query/QueryBuilder.py | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index c72bb7c5..f1155b78 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -863,12 +863,15 @@ def save(self, query=False): if not query: if self.is_loaded(): - result = builder.update(self.__dirty_attributes__, ignore_mass_assignment=True) + result = builder.update( + self.__dirty_attributes__, ignore_mass_assignment=True + ) else: result = self.create( self.__dirty_attributes__, query=query, id_key=self.get_primary_key(), + ignore_mass_assignment=True, ) self.observe_events(self, "saved") self.fill(result.__attributes__) @@ -876,7 +879,9 @@ def save(self, query=False): return result if self.is_loaded(): - result = builder.update(self.__dirty_attributes__, dry=query, ignore_mass_assignment=True).to_sql() + result = builder.update( + self.__dirty_attributes__, dry=query, ignore_mass_assignment=True + ).to_sql() else: result = self.create(self.__dirty_attributes__, query=query) diff --git a/src/masoniteorm/query/QueryBuilder.py b/src/masoniteorm/query/QueryBuilder.py index 0454b644..0c496bae 100644 --- a/src/masoniteorm/query/QueryBuilder.py +++ b/src/masoniteorm/query/QueryBuilder.py @@ -1390,7 +1390,7 @@ def update( dry: bool = False, force: bool = False, cast: bool = False, - ignore_mass_assignment: bool = False + ignore_mass_assignment: bool = False, ): """Specifies columns and values to be updated.