added butchInsert to mongo model
This commit is contained in:
@ -121,10 +121,16 @@ class InsertMongoCommand extends MongoDbCommand
|
||||
|
||||
protected $insertId = false;
|
||||
|
||||
protected $multiple = false;
|
||||
|
||||
protected function concreteExecute()
|
||||
{
|
||||
if (!$this->multiple) {
|
||||
$result = $this->collection->insert($this->data, array('safe' => $this->safe));
|
||||
$this->insertId = $this->data['_id'];
|
||||
} else {
|
||||
$result = $this->collection->batchInsert($this->data, array('safe' => $this->safe));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
@ -150,6 +156,8 @@ class InsertMongoCommand extends MongoDbCommand
|
||||
var_dump($this->data);
|
||||
$data = ob_get_clean();
|
||||
$result .= 'Data: ' . $data . PHP_EOL;
|
||||
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
||||
$result .= 'Bulk insert: ' . $mult . PHP_EOL;
|
||||
$safe = $this->safe ? 'TRUE' : 'FALSE';
|
||||
$result .= 'Safe operation: ' . $safe . PHP_EOL;
|
||||
return $result;
|
||||
|
@ -77,12 +77,13 @@ class MongoDriver extends NoSqlDbDriver
|
||||
return $this->query($command, $params);
|
||||
}
|
||||
|
||||
public function insert($collection, $data, $safe = true)
|
||||
public function insert($collection, $data, $multiple = false, $safe = true)
|
||||
{
|
||||
$command = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->getCollection($collection));
|
||||
$params = array(
|
||||
'data' => $data,
|
||||
'safe' => $safe
|
||||
'safe' => $safe,
|
||||
'multiple' => $multiple
|
||||
);
|
||||
$result = $this->query($command, $params);
|
||||
$this->last_insert_id = $result->getInsertId();
|
||||
|
@ -40,6 +40,11 @@ abstract class MongoModel extends Model
|
||||
return $this->db->get($this->table(), array('_id' => $id))->fetch();
|
||||
}
|
||||
|
||||
public function batchInsert($data)
|
||||
{
|
||||
return $this->db->insert($this->table(), $data, true);
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
if ($this->useMongoId) {
|
||||
|
@ -132,6 +132,40 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testInsertCommandMultipleObjects()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
|
||||
$data = array(
|
||||
array('name' => 'first object'),
|
||||
array('name' => 'second object'),
|
||||
array('name' => 'equal object'),
|
||||
array('name' => 'equal object')
|
||||
);
|
||||
$cmd
|
||||
->bindParam('data', $data)
|
||||
->bindParam('multiple', true)
|
||||
->bindParam('safe', true);
|
||||
|
||||
$this->assertFalse($cmd->getInsertId());
|
||||
|
||||
$this->assertArrayHasKey('n', $cmd->execute());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$cmd->bindParam('condition', array('name' => 'first object'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(1, $result->count());
|
||||
$cmd->bindParam('condition', array('name' => 'second object'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(1, $result->count());
|
||||
$cmd->bindParam('condition', array('name' => 'equal object'))->bindParam('fields', array());
|
||||
$result = $cmd->execute();
|
||||
$this->assertEquals(2, $result->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testInsertCommandNotAllParamsBinded()
|
||||
{
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
@ -277,6 +311,15 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('safe', true);
|
||||
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||
$this->assertContains('Bulk insert: FALSE', $cmd->__toString());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
$cmd
|
||||
->bindParam('data', array('name' => 'insert'))
|
||||
->bindParam('multiple', true)
|
||||
->bindParam('safe', true);
|
||||
$this->assertContains('Bulk insert: TRUE', $cmd->__toString());
|
||||
|
||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||
|
@ -252,6 +252,29 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testBatchInsert()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
|
||||
$data = array(
|
||||
array('name' => 'first object'),
|
||||
array('name' => 'second object'),
|
||||
array('name' => 'equal object'),
|
||||
array('name' => 'equal object')
|
||||
);
|
||||
$mongo = new MongoDriver($this->conf);
|
||||
$mongo->insert('items', $data, true);
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'first object'))->numRows());
|
||||
$this->assertEquals(1, $mongo->find('items', array('name' => 'second object'))->numRows());
|
||||
$this->assertEquals(2, $mongo->find('items', array('name' => 'equal object'))->numRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testGetInsertId()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
|
@ -112,6 +112,26 @@ class MongoModelTest extends PHPUnit_Framework_TestCase
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testBatchInsert()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
define('DEBUG', false);
|
||||
}
|
||||
$data = array(
|
||||
array('name' => 'first object'),
|
||||
array('name' => 'second object'),
|
||||
array('name' => 'equal object'),
|
||||
array('name' => 'equal object')
|
||||
);
|
||||
$this->model->batchInsert($data);
|
||||
$this->assertEquals(1, $this->model->count(array('name' => 'first object')));
|
||||
$this->assertEquals(2, $this->model->count(array('name' => 'equal object')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @group Mongo
|
||||
*/
|
||||
public function testDeleteAll()
|
||||
{
|
||||
if (!defined('DEBUG')) {
|
||||
|
Reference in New Issue
Block a user