From 624fec69391dd9eff3d558c065c4aaeb31a5b77d Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Fri, 30 Oct 2020 12:30:34 +0330 Subject: [PATCH 1/5] Update ActiveRecord.php --- src/ActiveRecord.php | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index c720d94b2..7cfc1e2fb 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -25,6 +25,9 @@ */ abstract class ActiveRecord extends BaseActiveRecord { + + protected $unsetAttrs = []; + /** * Returns the Mongo connection used by this AR class. * By default, the "mongodb" application component is used as the Mongo connection. @@ -165,6 +168,25 @@ public function attributes() throw new InvalidConfigException('The attributes() method of mongodb ActiveRecord has to be implemented by child classes.'); } + public function __set($name, $value) + { + unset($this->unsetAttrs[$name]); + parent::__set($name,$value); + } + + public function unset() + { + if ($this->getIsNewRecord()) { + throw new InvalidConfigException('You can not use `unset` method when the current record is new.'); + } + foreach(func_get_args() as $attribute) { + if (!$this->hasAttribute($attribute)) { + throw new UnknownPropertyException('Unsetting unknown property: ' . get_class($this) . '::' . $attribute); + } + $this->unsetAttrs[$attribute] = ''; + } + } + /** * Inserts a row into the associated Mongo collection using the attribute values of this record. * @@ -253,7 +275,7 @@ protected function updateInternal($attributes = null) return false; } $values = $this->getDirtyAttributes($attributes); - if (empty($values)) { + if (empty($values) && empty($this->unsetAttrs)) { $this->afterSave(false, $values); return 0; } @@ -265,9 +287,25 @@ protected function updateInternal($attributes = null) } $condition[$lock] = $this->$lock; } + + $document = $values; + if (!empty($this->unsetAttrs)){ + foreach ($this->unsetAttrs as $attr => $_) { + unset($document[$attr],$values[$attr],$this->$attr); + } + if (empty($document)) { + $document = ['$unset' => $this->unsetAttrs]; + } else { + $document = [ + '$set' => $document, + '$unset' => $this->unsetAttrs, + ]; + } + $this->unsetAttrs = []; + } // We do not check the return value of update() because it's possible // that it doesn't change anything and thus returns 0. - $rows = static::getCollection()->update($condition, $values); + $rows = static::getCollection()->update($condition, $document); if ($lock !== null && !$rows) { throw new StaleObjectException('The object being updated is outdated.'); From a6c156bf89db6a7d8680fee6b154f72b0b915bd6 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Fri, 30 Oct 2020 13:39:06 +0330 Subject: [PATCH 2/5] Update ActiveRecord.php --- src/ActiveRecord.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 7cfc1e2fb..3f75f1e5a 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -184,6 +184,7 @@ public function unset() throw new UnknownPropertyException('Unsetting unknown property: ' . get_class($this) . '::' . $attribute); } $this->unsetAttrs[$attribute] = ''; + $this->$attr = null; } } From f851d0b34139f71365bc847414c1e74ac29bc3f5 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Wed, 11 Nov 2020 13:41:00 +0330 Subject: [PATCH 3/5] supports insert mode --- src/ActiveRecord.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 3f75f1e5a..30c5098b7 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -176,15 +176,17 @@ public function __set($name, $value) public function unset() { - if ($this->getIsNewRecord()) { - throw new InvalidConfigException('You can not use `unset` method when the current record is new.'); - } foreach(func_get_args() as $attribute) { if (!$this->hasAttribute($attribute)) { throw new UnknownPropertyException('Unsetting unknown property: ' . get_class($this) . '::' . $attribute); } - $this->unsetAttrs[$attribute] = ''; - $this->$attr = null; + if ($this->getIsNewRecord()) { + unset($this->$attr); + } + else { + $this->unsetAttrs[$attribute] = ''; + $this->$attr = null; + } } } From 8fae95a3d99aa9617932349e1dc6e004a5412ebf Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Fri, 13 Nov 2020 15:58:13 +0330 Subject: [PATCH 4/5] Update ActiveRecord.php --- src/ActiveRecord.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 30c5098b7..2581837b0 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -181,11 +181,11 @@ public function unset() throw new UnknownPropertyException('Unsetting unknown property: ' . get_class($this) . '::' . $attribute); } if ($this->getIsNewRecord()) { - unset($this->$attr); + unset($this->$attribute); } else { $this->unsetAttrs[$attribute] = ''; - $this->$attr = null; + $this->$attribute = null; } } } From 0b8a958a4ae442491034a2895eaa7b1677f3f3e2 Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Tue, 28 Feb 2023 19:46:32 +0330 Subject: [PATCH 5/5] fix bug --- src/ActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 2581837b0..1030076b6 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -184,8 +184,8 @@ public function unset() unset($this->$attribute); } else { - $this->unsetAttrs[$attribute] = ''; $this->$attribute = null; + $this->unsetAttrs[$attribute] = ''; } } }