Commit a39b2d37 by Paul Klimov

Default options setup added to Mongo Collection operations.

parent 9c7d2b23
...@@ -134,10 +134,6 @@ abstract class ActiveRecord extends Model ...@@ -134,10 +134,6 @@ abstract class ActiveRecord extends Model
*/ */
public static function updateAll($attributes, $condition = [], $options = []) 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); return static::getCollection()->update($condition, $attributes, $options);
} }
...@@ -158,10 +154,6 @@ abstract class ActiveRecord extends Model ...@@ -158,10 +154,6 @@ abstract class ActiveRecord extends Model
*/ */
public static function updateAllCounters($counters, $condition = [], $options = []) 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); return static::getCollection()->update($condition, ['$inc' => $counters], $options);
} }
...@@ -798,7 +790,7 @@ abstract class ActiveRecord extends Model ...@@ -798,7 +790,7 @@ abstract class ActiveRecord extends Model
} }
// We do not check the return value of update() because it's possible // We do not check the return value of update() because it's possible
// that it doesn't change anything and thus returns 0. // 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) { if ($lock !== null && !$rows) {
throw new StaleObjectException('The object being updated is outdated.'); throw new StaleObjectException('The object being updated is outdated.');
...@@ -871,7 +863,7 @@ abstract class ActiveRecord extends Model ...@@ -871,7 +863,7 @@ abstract class ActiveRecord extends Model
if ($lock !== null) { if ($lock !== null) {
$condition[$lock] = $this->$lock; $condition[$lock] = $this->$lock;
} }
$result = static::getCollection()->remove($condition, ['w' => 1]); $result = static::getCollection()->remove($condition);
if ($lock !== null && !$result) { if ($lock !== null && !$result) {
throw new StaleObjectException('The object being deleted is outdated.'); throw new StaleObjectException('The object being deleted is outdated.');
} }
......
...@@ -70,6 +70,7 @@ class Collection extends Object ...@@ -70,6 +70,7 @@ class Collection extends Object
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$options = array_merge(['w' => 1], $options);
$this->tryResultError($this->mongoCollection->insert($data, $options)); $this->tryResultError($this->mongoCollection->insert($data, $options));
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return is_array($data) ? $data['_id'] : $data->_id; return is_array($data) ? $data['_id'] : $data->_id;
...@@ -92,6 +93,7 @@ class Collection extends Object ...@@ -92,6 +93,7 @@ class Collection extends Object
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$options = array_merge(['w' => 1], $options);
$this->tryResultError($this->mongoCollection->batchInsert($rows, $options)); $this->tryResultError($this->mongoCollection->batchInsert($rows, $options));
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return $rows; return $rows;
...@@ -115,7 +117,15 @@ class Collection extends Object ...@@ -115,7 +117,15 @@ class Collection extends Object
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); 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); $this->tryResultError($result);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
if (is_array($result) && array_key_exists('n', $result)) { if (is_array($result) && array_key_exists('n', $result)) {
...@@ -142,6 +152,7 @@ class Collection extends Object ...@@ -142,6 +152,7 @@ class Collection extends Object
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$options = array_merge(['w' => 1], $options);
$this->tryResultError($this->mongoCollection->save($data, $options)); $this->tryResultError($this->mongoCollection->save($data, $options));
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return is_array($data) ? $data['_id'] : $data->_id; return is_array($data) ? $data['_id'] : $data->_id;
...@@ -164,6 +175,7 @@ class Collection extends Object ...@@ -164,6 +175,7 @@ class Collection extends Object
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$options = array_merge(['w' => 1, 'multiple' => true], $options);
$result = $this->mongoCollection->remove($this->buildCondition($condition), $options); $result = $this->mongoCollection->remove($this->buildCondition($condition), $options);
$this->tryResultError($result); $this->tryResultError($result);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
......
...@@ -117,7 +117,8 @@ class CollectionTest extends MongoTestCase ...@@ -117,7 +117,8 @@ class CollectionTest extends MongoTestCase
]; ];
$id = $collection->insert($data); $id = $collection->insert($data);
$collection->remove(['_id' => $id]); $count = $collection->remove(['_id' => $id]);
$this->assertEquals(1, $count);
$rows = $collection->findAll(); $rows = $collection->findAll();
$this->assertEquals(0, count($rows)); $this->assertEquals(0, count($rows));
...@@ -138,7 +139,8 @@ class CollectionTest extends MongoTestCase ...@@ -138,7 +139,8 @@ class CollectionTest extends MongoTestCase
$newData = [ $newData = [
'name' => 'new name' 'name' => 'new name'
]; ];
$collection->update(['_id' => $id], $newData); $count = $collection->update(['_id' => $id], $newData);
$this->assertEquals(1, $count);
list($row) = $collection->findAll(); list($row) = $collection->findAll();
$this->assertEquals($newData['name'], $row['name']); $this->assertEquals($newData['name'], $row['name']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment