diff --git a/extensions/mongo/ActiveRecord.php b/extensions/mongo/ActiveRecord.php index d6788fc..5e2d9bc 100644 --- a/extensions/mongo/ActiveRecord.php +++ b/extensions/mongo/ActiveRecord.php @@ -134,10 +134,6 @@ abstract class ActiveRecord extends Model */ public static function updateAll($attributes, $condition = [], $options = []) { - $options['w'] = 1; - if (!array_key_exists('multiple', $options)) { - $options['multiple'] = true; - } return static::getCollection()->update($condition, $attributes, $options); } @@ -158,10 +154,6 @@ abstract class ActiveRecord extends Model */ public static function updateAllCounters($counters, $condition = [], $options = []) { - $options['w'] = 1; - if (!array_key_exists('multiple', $options)) { - $options['multiple'] = true; - } return static::getCollection()->update($condition, ['$inc' => $counters], $options); } @@ -798,7 +790,7 @@ abstract class ActiveRecord extends Model } // 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, ['w' => 1]); + $rows = static::getCollection()->update($condition, $values); if ($lock !== null && !$rows) { throw new StaleObjectException('The object being updated is outdated.'); @@ -871,7 +863,7 @@ abstract class ActiveRecord extends Model if ($lock !== null) { $condition[$lock] = $this->$lock; } - $result = static::getCollection()->remove($condition, ['w' => 1]); + $result = static::getCollection()->remove($condition); if ($lock !== null && !$result) { throw new StaleObjectException('The object being deleted is outdated.'); } diff --git a/extensions/mongo/Collection.php b/extensions/mongo/Collection.php index 6e1fe3c..7959186 100644 --- a/extensions/mongo/Collection.php +++ b/extensions/mongo/Collection.php @@ -70,6 +70,7 @@ class Collection extends Object Yii::info($token, __METHOD__); try { Yii::beginProfile($token, __METHOD__); + $options = array_merge(['w' => 1], $options); $this->tryResultError($this->mongoCollection->insert($data, $options)); Yii::endProfile($token, __METHOD__); return is_array($data) ? $data['_id'] : $data->_id; @@ -92,6 +93,7 @@ class Collection extends Object Yii::info($token, __METHOD__); try { Yii::beginProfile($token, __METHOD__); + $options = array_merge(['w' => 1], $options); $this->tryResultError($this->mongoCollection->batchInsert($rows, $options)); Yii::endProfile($token, __METHOD__); return $rows; @@ -115,7 +117,15 @@ class Collection extends Object Yii::info($token, __METHOD__); try { Yii::beginProfile($token, __METHOD__); - $result = $this->mongoCollection->update($this->buildCondition($condition), $newData, $options); + $options = array_merge(['w' => 1, 'multiple' => true], $options); + if ($options['multiple']) { + $keys = array_keys($newData); + if (!empty($keys) && strncmp('$', $keys[0], 1) !== 0) { + $newData = ['$set' => $newData]; + } + } + $condition = $this->buildCondition($condition); + $result = $this->mongoCollection->update($condition, $newData, $options); $this->tryResultError($result); Yii::endProfile($token, __METHOD__); if (is_array($result) && array_key_exists('n', $result)) { @@ -142,6 +152,7 @@ class Collection extends Object Yii::info($token, __METHOD__); try { Yii::beginProfile($token, __METHOD__); + $options = array_merge(['w' => 1], $options); $this->tryResultError($this->mongoCollection->save($data, $options)); Yii::endProfile($token, __METHOD__); return is_array($data) ? $data['_id'] : $data->_id; @@ -164,6 +175,7 @@ class Collection extends Object Yii::info($token, __METHOD__); try { Yii::beginProfile($token, __METHOD__); + $options = array_merge(['w' => 1, 'multiple' => true], $options); $result = $this->mongoCollection->remove($this->buildCondition($condition), $options); $this->tryResultError($result); Yii::endProfile($token, __METHOD__); diff --git a/tests/unit/extensions/mongo/CollectionTest.php b/tests/unit/extensions/mongo/CollectionTest.php index 053ee7e..1e86236 100644 --- a/tests/unit/extensions/mongo/CollectionTest.php +++ b/tests/unit/extensions/mongo/CollectionTest.php @@ -117,7 +117,8 @@ class CollectionTest extends MongoTestCase ]; $id = $collection->insert($data); - $collection->remove(['_id' => $id]); + $count = $collection->remove(['_id' => $id]); + $this->assertEquals(1, $count); $rows = $collection->findAll(); $this->assertEquals(0, count($rows)); @@ -138,7 +139,8 @@ class CollectionTest extends MongoTestCase $newData = [ 'name' => 'new name' ]; - $collection->update(['_id' => $id], $newData); + $count = $collection->update(['_id' => $id], $newData); + $this->assertEquals(1, $count); list($row) = $collection->findAll(); $this->assertEquals($newData['name'], $row['name']);