Permalink
Browse files

Merge branch 'master' into cake2.0

  • Loading branch information...
ichikaway committed Aug 24, 2011
2 parents 0b8a390 + 01390ea commit 002db95354bcd4b4bd4f5e2c4bb7078a1a7f7d02
Showing with 67 additions and 12 deletions.
  1. +27 −12 Model/Datasource/MongodbSource.php
  2. +40 −0 Test/Case/Datasource/MongodbSourceTest.php
@@ -709,22 +709,28 @@ public function update(&$Model, $fields = null, $values = null, $conditions = nu
$cond = array('_id' => $data['_id']);
unset($data['_id']);
+ if(isset($data['updated'])) {
+ $updateField = 'updated';
+ } else {
+ $updateField = 'modified';
+ }
+
//setting Mongo operator
if(empty($Model->mongoNoSetOperator)) {
if(!preg_grep('/^\$/', array_keys($data))) {
$data = array('$set' => $data);
} else {
- if(!empty($data['modified'])) {
- $modified = $data['modified'];
- unset($data['modified']);
- $data['$set'] = array('modified' => $modified);
+ if(!empty($data[$updateField])) {
+ $modified = $data[$updateField];
+ unset($data[$updateField]);
+ $data['$set'] = array($updateField => $modified);
}
}
} elseif(substr($Model->mongoNoSetOperator,0,1) === '$') {
- if(!empty($data['modified'])) {
- $modified = $data['modified'];
- unset($data['modified']);
- $data = array($Model->mongoNoSetOperator => $data, '$set' => array('modified' => $modified));
+ if(!empty($data[$updateField])) {
+ $modified = $data[$updateField];
+ unset($data[$updateField]);
+ $data = array($Model->mongoNoSetOperator => $data, '$set' => array($updateField => $modified));
} else {
$data = array($Model->mongoNoSetOperator => $data);
@@ -986,7 +992,7 @@ public function read(&$Model, $query = array()) {
->limit($limit)
->skip($offset);
if ($this->fullDebug) {
- $count = $return->count();
+ $count = $return->count(true);
$this->logQuery("db.{$Model->useTable}.find( :conditions, :fields ).sort( :order ).limit( :limit ).skip( :offset )",
compact('conditions', 'fields', 'order', 'limit', 'offset', 'count')
);
@@ -1109,10 +1115,19 @@ public function mapReduce($query, $timeout = null) {
}
$result = $this->query($query);
+
if($result['ok']) {
- $data = $this->_db->selectCollection($result['result'])->find();
- if(!empty($timeout)) {
- $data->timeout($timeout);
+ if (isset($query['out']['inline']) && $query['out']['inline'] === 1) {
+ if (is_array($result['results'])) {
+ $data = $result['results'];
+ }else{
+ $data = false;
+ }
+ }else {
+ $data = $this->_db->selectCollection($result['result'])->find();
+ if(!empty($timeout)) {
+ $data->timeout($timeout);
+ }
}
return $data;
}
@@ -163,6 +163,18 @@ public function tearDown() {
ClassRegistry::flush();
}
+
+/**
+ * get Mongod server version
+ *
+ * @return numeric
+ * @access public
+ */
+ public function getMongodVersion() {
+ $mongo = $this->Post->getDataSource();
+ return $mongo->execute('db.version()');
+ }
+
/**
* Insert data method for mongodb.
*
@@ -1045,6 +1057,34 @@ public function testMapReduce() {
$this->assertEqual(1, $posts['test3']);
+ //get results as inline data
+ $version = $this->getMongodVersion();
+ if( $version >= '1.7.4') {
+ $params = array(
+ "mapreduce" => "posts",
+ "map" => $map,
+ "reduce" => $reduce,
+ "query" => array(
+ "count" => array('$gt' => -2),
+ ),
+ 'out' => array('inline' => 1),
+ );
+
+ $results = $mongo->mapReduce($params);
+
+ $posts = array();
+ foreach ($results as $post) {
+ $posts[$post['_id']] = $post['value'];
+ }
+
+ $this->assertEqual(30, count($posts));
+ $this->assertEqual(1, $posts['test0']);
+ $this->assertEqual(2, $posts['test1']);
+ $this->assertEqual(3, $posts['test2']);
+ $this->assertEqual(1, $posts['test3']);
+ }
+
+
}

0 comments on commit 002db95

Please sign in to comment.