Browse Source

Mongo Command 'update' and 'insertBatch' methods added.

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
d42a942aed
  1. 45
      extensions/mongo/Collection.php
  2. 2
      extensions/mongo/Query.php
  3. 52
      tests/unit/extensions/mongo/CollectionTest.php

45
extensions/mongo/Collection.php

@ -79,6 +79,51 @@ class Collection extends Object
}
/**
* Inserts several new rows into collection.
* @param array $rows array of arrays or objects to be inserted.
* @param array $options list of options in format: optionName => optionValue.
* @return array inserted data, each row will have "_id" key assigned to it.
* @throws Exception on failure.
*/
public function batchInsert($rows, $options = [])
{
$token = 'Inserting batch data into ' . $this->mongoCollection->getName();
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$this->tryResultError($this->mongoCollection->batchInsert($rows, $options));
Yii::endProfile($token, __METHOD__);
return $rows;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
* Updates the rows, which matches given criteria by given data.
* @param array $criteria description of the objects to update.
* @param array $newData the object with which to update the matching records.
* @param array $options list of options in format: optionName => optionValue.
* @return boolean whether operation was successful.
* @throws Exception on failure.
*/
public function update($criteria, $newData, $options = [])
{
$token = 'Updating data in ' . $this->mongoCollection->getName();
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$this->mongoCollection->update($criteria, $newData, $options);
Yii::endProfile($token, __METHOD__);
return true;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
* Update the existing database data, otherwise insert this data
* @param array|object $data data to be updated/inserted.
* @param array $options list of options in format: optionName => optionValue.

2
extensions/mongo/Query.php

@ -91,7 +91,7 @@ class Query extends Component implements QueryInterface
if (!empty($this->orderBy)) {
$sort = [];
foreach ($this->orderBy as $fieldName => $sortOrder) {
$sort[$fieldName] = $sortOrder === SORT_DESC ? -1 : 1;
$sort[$fieldName] = $sortOrder === SORT_DESC ? \MongoCollection::DESCENDING : \MongoCollection::ASCENDING;
}
$cursor->sort($this->orderBy);
}

52
tests/unit/extensions/mongo/CollectionTest.php

@ -15,6 +15,13 @@ class CollectionTest extends MongoTestCase
// Tests :
public function testFind()
{
$collection = $this->getConnection()->getCollection('customer');
$cursor = $collection->find();
$this->assertTrue($cursor instanceof \MongoCursor);
}
public function testInsert()
{
$collection = $this->getConnection()->getCollection('customer');
@ -44,6 +51,28 @@ class CollectionTest extends MongoTestCase
$this->assertEquals($id, $rows[0]['_id']);
}
/**
* @depends testFind
*/
public function testBatchInsert()
{
$collection = $this->getConnection()->getCollection('customer');
$rows = [
[
'name' => 'customer 1',
'address' => 'customer 1 address',
],
[
'name' => 'customer 2',
'address' => 'customer 2 address',
],
];
$insertedRows = $collection->batchInsert($rows);
$this->assertTrue($insertedRows[0]['_id'] instanceof \MongoId);
$this->assertTrue($insertedRows[1]['_id'] instanceof \MongoId);
$this->assertEquals(count($rows), $collection->find()->count());
}
public function testSave()
{
$collection = $this->getConnection()->getCollection('customer');
@ -59,7 +88,7 @@ class CollectionTest extends MongoTestCase
/**
* @depends testSave
*/
public function testUpdate()
public function testUpdateBySave()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
@ -93,4 +122,25 @@ class CollectionTest extends MongoTestCase
$rows = $collection->findAll();
$this->assertEquals(0, count($rows));
}
/**
* @depends testFindAll
*/
public function testUpdate()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $collection->insert($data);
$newData = [
'name' => 'new name'
];
$collection->update(['_id' => $id], $newData);
list($row) = $collection->findAll();
$this->assertEquals($newData['name'], $row['name']);
}
}
Loading…
Cancel
Save