From 61d059ee90c39f031ae3e69f06aab3e721ec92c1 Mon Sep 17 00:00:00 2001 From: Sammaye Date: Fri, 27 Sep 2013 12:16:28 +0100 Subject: [PATCH] Fixed: https://github.com/Sammaye/MongoYii/issues/125 --- EMongoDocument.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/EMongoDocument.php b/EMongoDocument.php index 3f25d9a..1072e4c 100644 --- a/EMongoDocument.php +++ b/EMongoDocument.php @@ -530,17 +530,23 @@ public function update($attributes=null){ if($this->{$this->primaryKey()} === null) // An _id is required throw new CDbException(Yii::t('yii', 'The active record cannot be updated because it has no _id.')); - if($attributes !== null) + $partial=false; + if($attributes !== null){ $attributes = $this->filterRawDocument($attributes); - elseif($this->getIsPartial()){ + $partial=true; + }elseif($this->getIsPartial()){ foreach($this->_projected_fields as $field => $v) $attributes[$field] = $this->$field; $attributes = $this->filterRawDocument($attributes); + $partial=true; }else $attributes = $this->getRawDocument(); unset($attributes['_id']); // Unset the _id before update - $this->lastError = $this->updateByPk($this->{$this->primaryKey()}, array('$set' => $attributes)); + if($partial===true) + $this->lastError = $this->updateByPk($this->{$this->primaryKey()}, array('$set' => $attributes)); + else // If this is not partial then we replace entire doc + $this->lastError = $this->updateByPk($this->{$this->primaryKey()}, $attributes); $this->afterSave(); return true; }