Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting driver wide collection options for safe, fsync and timeout. #32

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5a2c3dd
Changes allow setting of driver wide colleciton options for safe, fsy…
elricho Sep 2, 2011
9b2d990
Fixed bug in insert introduced in previous commit.
elricho Sep 5, 2011
afe9b9e
Updated to use collectionOptions on a per collection basis rather tha…
elricho Sep 11, 2011
fc17456
Library ported from using php-mongo extebsion to new php-mongodb exte…
Oct 31, 2016
c1a6c8b
Remove outdated directories.
Oct 31, 2016
437a15e
Remove outdated docs.
Oct 31, 2016
a61f886
Remove outdated Cakephp 1.3 samples.
Oct 31, 2016
ae89f69
Remove debug statements.
Oct 31, 2016
ca4d05c
Update readme.
Oct 31, 2016
0f4daec
Updated readme.
Oct 31, 2016
91412f9
Cleanup JSON for Packagist
Oct 31, 2016
39e9906
Remove uppercase chars from package name ! Doh, i always forget this.
Oct 31, 2016
d52cce6
Adjust composer instructions for lowercase composer name.
Oct 31, 2016
ec2eae5
Add Travis CI
Nov 1, 2016
acc2fa9
Install php-mongodb extension for php 5.6
Nov 1, 2016
5b9ef91
Use correct path to tests.
Nov 1, 2016
7e8513d
Remove debug from tests.
Nov 1, 2016
e784112
BugFix : Change MongoDate to new class MongoDB\BSON\UTCDateTime.
Nov 1, 2016
228ac24
BugFix : Replace MongoDate with MongoDB\BSON\UTCDateTime
Nov 1, 2016
20129c0
BugFIx : Use MongoDB::MONGODB_VERSION constant to get the driver vers…
Nov 1, 2016
70e745e
BugFix : Update exceptions to new exception classes.
Nov 1, 2016
eed4233
Have composer install deps.
Nov 1, 2016
034f278
Ensure composer autoload.php executes before tests run.
Nov 1, 2016
83621ba
Fix previous blunder - doh !
Nov 1, 2016
d378b79
Add composer autoload to cakephp bootstrap.
Nov 1, 2016
e350dc0
Update composer.json for correct phpunit version. (Cakephp 2.x needs …
Nov 1, 2016
a696900
Remove reference to mysql.
Nov 1, 2016
fc6357a
Cleanup better after tests.
Nov 1, 2016
ca8bdaf
BugFix : testCreateConnectionName using incorrect datasource name.
Nov 1, 2016
fcea761
Fix datasource issues with $test database defaults.
Nov 1, 2016
7c95938
Added debug to tests - tracing a travis issue.
Nov 2, 2016
3897805
Install Mongodb 3.2 (Travis uses 2.4).
Nov 2, 2016
b6a1d37
Don't drop system collections.
Nov 2, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions models/datasources/mongodb_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,29 @@ class MongodbSource extends DboSource {
'port' => '27017',
'login' => '',
'password' => '',
'replicaset' => '',
'replicaset' => ''
);

/**
* collection options
*
* set collection options for various mongo write operations.
* options can be found in the php manual
* http://www.php.net/manual/en/mongocollection.save.php
* http://www.php.net/manual/en/mongocollection.insert.php
* http://www.php.net/manual/en/mongocollection.batchinsert.php
* http://www.php.net/manual/en/mongocollection.update.php
*
* @var array
*/

public $collectionOptions = array(
'save' => array(),
'insert' => array(),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'insert' => array('safe' => true),

'batchInsert' => array(),
'update' => array()
);

/**
* column definition
*
Expand Down Expand Up @@ -200,7 +220,7 @@ public function connect() {

$this->connected = true;
}

} catch(MongoException $e) {
$this->error = $e->getMessage();
trigger_error($this->error);
Expand Down Expand Up @@ -256,16 +276,17 @@ public function insertMulti($table, $fields, $values) {
$data[] = array_combine($fields, $row);
}
$this->_prepareLogQuery($table); // just sets a timer
$params = array_merge($this->collectionOptions['batchInsert'], array('safe' => true));
try{
$return = $this->_db
->selectCollection($table)
->batchInsert($data, array('safe' => true));
->batchInsert($data, $params);
} catch (MongoException $e) {
$this->error = $e->getMessage();
trigger_error($this->error);
}
if ($this->fullDebug) {
$this->logQuery("db.{$table}.insertMulti( :data , array('safe' => true))", compact('data'));
$this->logQuery("db.{$table}.insertMulti( :data , :params )", compact('data','params'));
}
}

Expand Down Expand Up @@ -466,16 +487,17 @@ public function create(&$Model, $fields = null, $values = null) {
}

$this->_prepareLogQuery($Model); // just sets a timer
$params = $this->collectionOptions['insert'];
try{
$return = $this->_db
->selectCollection($Model->table)
->insert($data, true);
->insert($data, $params);
} catch (MongoException $e) {
$this->error = $e->getMessage();
trigger_error($this->error);
}
if ($this->fullDebug) {
$this->logQuery("db.{$Model->useTable}.insert( :data , true)", compact('data'));
$this->logQuery("db.{$Model->useTable}.insert( :data , :params )", compact('data','params'));
}

if (!empty($return) && $return['ok']) {
Expand Down Expand Up @@ -710,33 +732,35 @@ public function update(&$Model, $fields = null, $values = null, $conditions = nu
}

$this->_prepareLogQuery($Model); // just sets a timer
$return = false;
if (!empty($data['_id'])) {
$this->_convertId($data['_id']);
$cond = array('_id' => $data['_id']);
unset($data['_id']);

$data = $this->setMongoUpdateOperator($Model, $data);

$params = array_merge($this->collectionOptions['update'], array("multiple" => true));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$params = array_merge($this->collectionOptions['update'], array("multiple" => false));

try{
$return = $mongoCollectionObj->update($cond, $data, array("multiple" => false));
$return = $mongoCollectionObj->update($cond, $data, $params);
} catch (MongoException $e) {
$this->error = $e->getMessage();
trigger_error($this->error);
}
if ($this->fullDebug) {
$this->logQuery("db.{$Model->useTable}.update( :conditions, :data, :params )",
array('conditions' => $cond, 'data' => $data, 'params' => array("multiple" => false))
array('conditions' => $cond, 'data' => $data, 'params' => $params)
);
}
} else {
$params = $this->collectionOptions['save'];
try{
$return = $mongoCollectionObj->save($data);
$return = $mongoCollectionObj->save($data, $params);
} catch (MongoException $e) {
$this->error = $e->getMessage();
trigger_error($this->error);
}
if ($this->fullDebug) {
$this->logQuery("db.{$Model->useTable}.save( :data )", compact('data'));
$this->logQuery("db.{$Model->useTable}.save( :data, :params )", compact('data', 'params'));
}
}
return $return;
Expand Down Expand Up @@ -804,20 +828,21 @@ public function updateAll(&$Model, $fields = null, $conditions = null) {
$this->_stripAlias($fields, $Model->alias, false, 'value');

$fields = $this->setMongoUpdateOperator($Model, $fields);

$params = array_merge($this->collectionOptions['update'], array("multiple" => true));

$this->_prepareLogQuery($Model); // just sets a timer
try{
$return = $this->_db
->selectCollection($Model->table)
->update($conditions, $fields, array("multiple" => true));
->update($conditions, $fields, $params);
} catch (MongoException $e) {
$this->error = $e->getMessage();
trigger_error($this->error);
}

if ($this->fullDebug) {
$this->logQuery("db.{$Model->useTable}.update( :conditions, :fields, :params )",
array('conditions' => $conditions, 'fields' => $fields, 'params' => array("multiple" => true))
array('conditions' => $conditions, 'fields' => $fields, 'params' => $params)
);
}
return $return;
Expand Down