diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..fe073d6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,48 @@ +language: php + +php: + - 5.6 + - 7.0 + +env: + global: + - PLUGIN_NAME=MongoDBLib + - DATASOURCE_NAME=MongodbSource + - REQUIRE="" + + matrix: + - CAKE_VERSION=2.8 + - CAKE_VERSION=2.9 + +addons: + apt: + sources: + - mongodb-upstart + - mongodb-3.2-precise + packages: + - mongodb-org-server + - mongodb-org-shell + +matrix: + include: + - php: 5.6 + env: + - CAKE_VERSION=2.8 + - CODECOVERAGE=1 + - php: 5.6 + env: + - PHPCS=1 + +before_script: + - if [[ $TRAVIS_PHP_VERSION =~ 5.6 ]] ; then echo yes | pecl install mongodb; fi; + - echo "extension=mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini + - git clone -b master https://github.com/FriendsOfCake/travis.git --depth 1 ../travis + - travis_wait ../travis/before_script.sh + - travis_retry composer install + - echo "require APP . 'vendor' . DS . 'autoload.php';" >> ../cakephp/app/Config/bootstrap.php + +script: + - if [ -d ../cakephp/app ]; then cd ../cakephp/app; fi; ./Console/cake test $PLUGIN_NAME Datasource/$DATASOURCE_NAME + +notifications: + email: false diff --git a/models/behaviors/schemaless.php b/Model/Behavior/SchemalessBehavior.php similarity index 93% rename from models/behaviors/schemaless.php rename to Model/Behavior/SchemalessBehavior.php index 03fb696..b721eb0 100644 --- a/models/behaviors/schemaless.php +++ b/Model/Behavior/SchemalessBehavior.php @@ -68,7 +68,7 @@ class SchemalessBehavior extends ModelBehavior { * @return void * @access public */ - public function setup(&$Model, $config = array()) { + public function setup(Model $Model, $config = array()) { //$this->settings[$Model->alias] = array_merge($this->_defaultSettings, $config); } @@ -81,7 +81,7 @@ public function setup(&$Model, $config = array()) { * @return void * @access public */ - public function beforeSave(&$Model) { + public function beforeSave(Model $Model, $options = array()) { $Model->cacheSources = false; $Model->schema(true); return true; diff --git a/models/behaviors/sql_compatible.php b/Model/Behavior/SqlCompatibleBehavior.php similarity index 84% rename from models/behaviors/sql_compatible.php rename to Model/Behavior/SqlCompatibleBehavior.php index 61e23ec..99f5433 100644 --- a/models/behaviors/sql_compatible.php +++ b/Model/Behavior/SqlCompatibleBehavior.php @@ -78,7 +78,7 @@ class SqlCompatibleBehavior extends ModelBehavior { * @return void * @access public */ - public function setup(&$Model, $config = array()) { + public function setup(Model $Model, $config = array()) { $this->settings[$Model->alias] = array_merge($this->_defaultSettings, $config); } @@ -91,7 +91,7 @@ public function setup(&$Model, $config = array()) { * @return void * @access public */ - public function afterFind(&$Model, $results, $primary) { + public function afterFind(Model $Model, $results, $primary=false) { if ($this->settings[$Model->alias]['convertDates']) { $this->convertDates($results); } @@ -108,11 +108,14 @@ public function afterFind(&$Model, $results, $primary) { * @return void * @access public */ - public function beforeFind(&$Model, $query) { + public function beforeFind(Model $Model, $query) { + if (is_array($query['order'])) { + $this->_translateOrders($Model, $query['order']); + } if (is_array($query['conditions']) && $this->_translateConditions($Model, $query['conditions'])) { return $query; } - return true; + return $query; } /** @@ -120,18 +123,40 @@ public function beforeFind(&$Model, $query) { * * @param mixed $results * @return void - * @access public + * @access protected */ - public function convertDates(&$results) { + protected function convertDates(&$results) { if (is_array($results)) { foreach($results as &$row) { $this->convertDates($row); } - } elseif (is_a($results, 'MongoDate')) { - $results = date('Y-M-d h:i:s', $results->sec); + } elseif (is_a($results, 'MongoDB\BSON\UTCDateTime')) { + $results = date('Y-M-d h:i:s', $results->toDateTime()->getTimestamp()); + } + } + + +/** + * translateOrders method + * change order syntax from SQL style to Mongo style + * + * @param mixed $Model + * @param mixed $orders + * @return void + * @access protected + */ + protected function _translateOrders(Model &$Model, &$orders) { + if(!empty($orders[0])) { + foreach($orders[0] as $key => $val) { + if(preg_match('/^(.+) (ASC|DESC)$/i', $val, $match)) { + $orders[0][$match[1]] = $match[2]; + unset($orders[0][$key]); + } + } } } + /** * translateConditions method * @@ -142,7 +167,7 @@ public function convertDates(&$results) { * @return void * @access protected */ - protected function _translateConditions(&$Model, &$conditions) { + protected function _translateConditions(Model &$Model, &$conditions) { $return = false; foreach($conditions as $key => &$value) { $uKey = strtoupper($key); @@ -261,7 +286,7 @@ protected function _translateConditions(&$Model, &$conditions) { * @return string * @access protected */ - protected function _translateOperator($Model, $operator) { + protected function _translateOperator(Model $Model, $operator) { if (!empty($this->settings[$Model->alias]['operators'][$operator])) { return $this->settings[$Model->alias]['operators'][$operator]; } diff --git a/models/datasources/mongodb_source.php b/Model/Datasource/MongodbSource.php similarity index 63% rename from models/datasources/mongodb_source.php rename to Model/Datasource/MongodbSource.php index bf03e29..846676c 100644 --- a/models/datasources/mongodb_source.php +++ b/Model/Datasource/MongodbSource.php @@ -2,10 +2,11 @@ /** * A CakePHP datasource for the mongoDB (http://www.mongodb.org/) document-oriented database. * - * This datasource uses Pecl Mongo (http://php.net/mongo) - * and is thus dependent on PHP 5.0 and greater. + * This datasource uses the new MongoDB Driver for PHP 5.6+ and, more importantly PHP 7.0 + * * * Original implementation by ichikaway(Yasushi Ichikawa) http://github.com/ichikaway/ + * Updated by elricho (Richard Uren) http://github/elricho * * Reference: * Nate Abele's lithium mongoDB datasource (http://li3.rad-dev.org/) @@ -13,7 +14,7 @@ * * Copyright 2010, Yasushi Ichikawa http://github.com/ichikaway/ * - * Contributors: Predominant, Jrbasso, tkyk, AD7six + * Contributors: Predominant, Jrbasso, tkyk, AD7six, elricho * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. @@ -25,6 +26,7 @@ */ App::import('Datasource', 'DboSource'); +App::import('Utility', 'CakeText'); /** * MongoDB Source @@ -60,7 +62,7 @@ class MongodbSource extends DboSource { * @var string * @access protected */ - protected $_driverVersion = Mongo::VERSION; + protected $_driverVersion = MONGODB_VERSION; /** * startTime property @@ -72,6 +74,14 @@ class MongodbSource extends DboSource { */ protected $_startTime = null; +/** + * Set fancy options for find queries .. timeouts, tailable cusrors etc ... + * + * @var string + * @access protected + **/ + protected $_findOptions = array(); + /** * Base Config * @@ -91,7 +101,27 @@ 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(), + 'batchInsert' => array(), + 'update' => array() ); /** @@ -123,6 +153,21 @@ class MongodbSource extends DboSource { 'modified' => array('type' => 'datetime', 'default' => null) ); +/** + * Typemap used for most all find requests + * This best emulates the legacy query capabilities the plugin previously provided + * + * @var array + * @access protected + */ + protected $typeMap = array( + 'typeMap' => array( + 'root' => 'array', + 'document' => 'array', + 'array' => 'array' + ) + ); + /** * construct method * @@ -163,45 +208,25 @@ public function commit() { /** * Connect to the database * - * If using 1.0.2 or above use the mongodb:// format to connect - * The connect syntax changed in version 1.0.2 - so check for that too - * - * If authentication information in present then authenticate the connection - * * @return boolean Connected * @access public */ public function connect() { $this->connected = false; - try{ + try { + $connectionString = $this->createConnectionString($this->config); - $host = $this->createConnectionName($this->config, $this->_driverVersion); - - if (isset($this->config['replicaset']) && count($this->config['replicaset']) === 2) { - $this->connection = new Mongo($this->config['replicaset']['host'], $this->config['replicaset']['options']); - if (isset($this->config['slaveok'])) { - $this->connection->setSlaveOkay($this->config['slaveok']); - } - } else if ($this->_driverVersion >= '1.2.0') { - $this->connection = new Mongo($host, array("persist" => $this->config['persistent'])); + if (isset($this->config['replicaset']['host'])) { + $this->connection = new MongoDB\Client($this->config['replicaset']['host'], $this->config['replicaset']['options'], $this->typeMap); } else { - $this->connection = new Mongo($host, true, $this->config['persistent']); + $this->connection = new MongoDB\Client($connectionString, array(), $this->typeMap); } - - if ($this->_db = $this->connection->selectDB($this->config['database'])) { - if (!empty($this->config['login']) && $this->_driverVersion < '1.2.0') { - $return = $this->_db->authenticate($this->config['login'], $this->config['password']); - if (!$return || !$return['ok']) { - trigger_error('MongodbSource::connect ' . $return['errmsg']); - return false; - } - } + if ($this->_db = $this->connection->selectDatabase($this->config['database'])) { $this->connected = true; } - - } catch(MongoException $e) { + } catch(MongoDB\Driver\Exception\Exception $e) { $this->error = $e->getMessage(); trigger_error($this->error); } @@ -209,30 +234,22 @@ public function connect() { } /** - * create connection name. + * Create connection string * * @param array $config - * @param string $version version of MongoDriver + * @string connection string */ - public function createConnectionName($config, $version) { - $host = null; - - if ($version >= '1.0.2') { - $host = "mongodb://"; - } else { - $host = ''; - } - $hostname = $config['host'] . ':' . $config['port']; - - if(!empty($config['login'])){ - $host .= $config['login'] .':'. $config['password'] . '@' . $hostname . '/'. $config['database']; - } else { - $host .= $hostname; - } + public function createConnectionString($config) { + $host = "mongodb://"; + $hostname = $config['host'] . ':' . $config['port']; - return $host; + if (! empty($config['login'])) { + $host .= $config['login'] .':'. $config['password'] . '@' . $hostname . '/'. $config['database']; + } else { + $host .= $hostname; } - + return $host; + } /** * Inserts multiple values into a table @@ -243,8 +260,6 @@ public function createConnectionName($config, $version) { * @access public */ public function insertMulti($table, $fields, $values) { - $table = $this->fullTableName($table); - if (!is_array($fields) || !is_array($values)) { return false; } @@ -255,18 +270,20 @@ public function insertMulti($table, $fields, $values) { } $data[] = array_combine($fields, $row); } - $this->_prepareLogQuery($table); // just sets a timer - try{ - $return = $this->_db - ->selectCollection($table) - ->batchInsert($data, array('safe' => true)); - } catch (MongoException $e) { + $this->_prepareLogQuery($Model->table); // just sets a timer + $params = array_merge($this->collectionOptions['batchInsert']); + try { + $collection = $this->_db + ->selectCollection($Model->table) + ->insertMany($data, $params); + } catch (MongoDB\Driver\Exception\Exception $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.{$Model->table}.insertMulti( :data , :params )", compact('data','params')); } + return $return; } /** @@ -305,10 +322,7 @@ public function getMongoCollection(&$Model) { if ($this->connected === false) { return false; } - - $collection = $this->_db - ->selectCollection($Model->table); - return $collection; + return $this->_db->selectCollection($Model->table); } /** @@ -347,36 +361,50 @@ public function close() { * @access public */ public function disconnect() { - if ($this->connected) { - $this->connected = !$this->connection->close(); + if ($this->connected !== false) { + $this->connected = false; unset($this->_db, $this->connection); - return !$this->connected; } return true; } /** - * Get list of available Collections + * Set special options for the find command + * Options are in 'key' => 'value' format. Anything from the following URL should be fine : + * https://docs.mongodb.com/php-library/master/reference/method/MongoDBCollection-find/ + * + * @return void + * @access public + */ + public function setFindOptions($options) { + $this->_findOptions = $options; + } + +/** + * Set typeMap + * See this URL for a typemap discussion + * https://docs.mongodb.com/php-library/master/reference/bson/#type-maps * + * @return void + * @access public + */ + public function setTypeMap($typeMap) { + $this->typeMap = $typeMap; + } + +/** + * Get list of available Collections + * Mongodb can create collections on the fly, so return true if connected. + * * @param array $data * @return array Collections * @access public */ public function listSources($data = null) { - if (!$this->isConnected()) { + if (! $this->isConnected()) { return false; } - - $collections = $this->_db->listCollections(); - $sources = array(); - - if(!empty($collections)){ - foreach($collections as $collection){ - $sources[] = $collection->getName(); - } - } - - return $sources; + return true; } /** @@ -390,15 +418,15 @@ public function listSources($data = null) { * @return array if model instance has mongoSchema, return it. * @access public */ - public function describe(&$Model, $field = null) { + public function describe($Model) { $Model->primaryKey = '_id'; $schema = array(); - if (!empty($Model->mongoSchema) && is_array($Model->mongoSchema)) { + if (! empty($Model->mongoSchema) && is_array($Model->mongoSchema)) { $schema = $Model->mongoSchema; return $schema + $this->_defaultSchema; } elseif ($this->isConnected() && is_a($Model, 'Model') && !empty($Model->Behaviors)) { - $Model->Behaviors->attach('Mongodb.Schemaless'); - if (!$Model->data) { + $Model->Behaviors->attach('MongoDBLib.Schemaless'); + if (! $Model->data) { if ($this->_db->selectCollection($Model->table)->count()) { return $this->deriveSchemaFromData($Model, $this->_db->selectCollection($Model->table)->findOne()); } @@ -426,7 +454,7 @@ public function begin() { * @return array * @access public */ - public function calculate(&$Model) { + public function calculate(Model $Model, $func, $params = array()) { return array('count' => true); } @@ -451,8 +479,8 @@ public function name($name) { * @return boolean Insert result * @access public */ - public function create(&$Model, $fields = null, $values = null) { - if (!$this->isConnected()) { + public function create(Model $Model, $fields = null, $values = null) { + if (! $this->isConnected()) { return false; } @@ -461,28 +489,29 @@ public function create(&$Model, $fields = null, $values = null) { } else { $data = $Model->data; } - if (!empty($data['_id'])) { + if (! empty($data['_id'])) { $this->_convertId($data['_id']); } $this->_prepareLogQuery($Model); // just sets a timer - try{ - $return = $this->_db + $params = $this->collectionOptions['insert']; + try { + $this->lastResult = $this->_db ->selectCollection($Model->table) - ->insert($data, true); - } catch (MongoException $e) { + ->insertOne($data, $params); + $return = true; + } catch (MongoDB\Driver\Exception\Exception $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->table}.insert( :data , :params )", compact('data','params')); } - if (!empty($return) && $return['ok']) { - - $id = $data['_id']; - if($this->config['set_string_id'] && is_object($data['_id'])) { - $id = $data['_id']->__toString(); + if (! empty($return)) { + $id = $this->lastResult->getInsertedId(); + if ($this->config['set_string_id'] && is_object($id)) { + $id = $this->lastResult->getInsertedId()->__toString(); } $Model->setInsertID($id); $Model->id = $id; @@ -515,7 +544,7 @@ public function createSchema($schema, $tableName = null) { * @return void * @access public */ - public function dropSchema($schema, $tableName = null) { + public function dropSchema(CakeSchema $schema, $tableName = null) { if (!$this->isConnected()) { return false; } @@ -555,7 +584,7 @@ public function dropSchema($schema, $tableName = null) { * @access public */ public function distinct(&$Model, $keys = array(), $params = array()) { - if (!$this->isConnected()) { + if (! $this->isConnected()) { return false; } @@ -564,25 +593,27 @@ public function distinct(&$Model, $keys = array(), $params = array()) { if (array_key_exists('conditions', $params)) { $params = $params['conditions']; } - try{ + try { $return = $this->_db ->selectCollection($Model->table) ->distinct($keys, $params); - } catch (MongoException $e) { + } catch (MongoDB\Driver\Exception\Exception $e) { $this->error = $e->getMessage(); trigger_error($this->error); } if ($this->fullDebug) { - $this->logQuery("db.{$Model->useTable}.distinct( :keys, :params )", compact('keys', 'params')); + $this->logQuery("db.{$Model->table}.distinct( :keys, :params )", compact('keys', 'params')); } return $return; } - /** * group method * + * Note : https://docs.mongodb.com/php-library/master/upgrade/#old-and-new-methods + * As of 2016-10-28 Mongo advises "Not yet implemented. See PHPLIB-177. Use MongoDB\Database::command. + * * @param mixed $Model * @param array $params array() * Set params same as MongoCollection::group() @@ -600,32 +631,40 @@ public function distinct(&$Model, $keys = array(), $params = array()) { * @return void * @access public */ - public function group(&$Model, $params = array()) { - - if (!$this->isConnected() || count($params) === 0 ) { + public function group($params, Model $Model = null) { + if (! $this->isConnected() || count($params) === 0 ) { return false; } - $this->_prepareLogQuery($Model); // just sets a timer - - $key = (empty($params['key'])) ? array() : $params['key']; - $initial = (empty($params['initial'])) ? array() : $params['initial']; - $reduce = (empty($params['reduce'])) ? array() : $params['reduce']; - $options = (empty($params['options'])) ? array() : $params['options']; - - try{ - $return = $this->_db - ->selectCollection($Model->table) - ->group($key, $initial, $reduce, $options); - } catch (MongoException $e) { + $this->_prepareLogQuery($Model); + $key = empty($params['key']) ? array() : $params['key']; + $initial = empty($params['initial']) ? array() : $params['initial']; + $reduce = empty($params['reduce']) ? array() : $params['reduce']; + $cond = empty($params['conditions']) ? array() : $params['conditions']; + $options = empty($params['options']) ? array() : $params['options']; + + try { + $tmp = $this->_db + ->command( + array( + 'group' => array( + 'ns' => $Model->table, + 'key' => $key, + 'initial' => $initial, + 'cond' => $cond, + '$reduce' => $reduce + ) + ), + $options + ); + $return = $tmp->toArray()[0]; + } catch (MongoDB\Driver\Exception\Exception $e) { $this->error = $e->getMessage(); trigger_error($this->error); } if ($this->fullDebug) { - $this->logQuery("db.{$Model->useTable}.group( :key, :initial, :reduce, :options )", $params); + $this->logQuery("db.{$Model->table}.group( :key, :initial, :reduce, :options )", $params); } - - return $return; } @@ -640,22 +679,22 @@ public function group(&$Model, $params = array()) { * @access public */ public function ensureIndex(&$Model, $keys = array(), $params = array()) { - if (!$this->isConnected()) { + if (! $this->isConnected()) { return false; } $this->_prepareLogQuery($Model); // just sets a timer - try{ + try { $return = $this->_db ->selectCollection($Model->table) - ->ensureIndex($keys, $params); - } catch (MongoException $e) { + ->createIndex($keys, $params); + } catch (MongoDB\Driver\Exception\Exception $e) { $this->error = $e->getMessage(); trigger_error($this->error); } if ($this->fullDebug) { - $this->logQuery("db.{$Model->useTable}.ensureIndex( :keys, :params )", compact('keys', 'params')); + $this->logQuery("db.{$Model->table}.ensureIndex( :keys, :params )", compact('keys', 'params')); } return $return; @@ -681,15 +720,14 @@ public function ensureIndex(&$Model, $keys = array(), $params = array()) { * @return boolean Update result * @access public */ - public function update(&$Model, $fields = null, $values = null, $conditions = null) { - - if (!$this->isConnected()) { + public function update(Model $Model, $fields = null, $values = null, $conditions = null) { + if (! $this->isConnected()) { return false; } if ($fields !== null && $values !== null) { $data = array_combine($fields, $values); - } elseif($fields !== null && $conditions !== null) { + } elseif ($fields !== null && $conditions !== null) { return $this->updateAll($Model, $fields, $conditions); } else{ $data = $Model->data; @@ -700,49 +738,58 @@ public function update(&$Model, $fields = null, $values = null, $conditions = nu } $this->_convertId($data['_id']); - try{ + try { $mongoCollectionObj = $this->_db ->selectCollection($Model->table); - } catch (MongoException $e) { + } catch (MongoDB\Driver\Exception\Exception $e) { $this->error = $e->getMessage(); trigger_error($this->error); return false; } $this->_prepareLogQuery($Model); // just sets a timer - if (!empty($data['_id'])) { + $return = false; + if (! empty($data['_id'])) { $this->_convertId($data['_id']); $cond = array('_id' => $data['_id']); unset($data['_id']); $data = $this->setMongoUpdateOperator($Model, $data); - - try{ - $return = $mongoCollectionObj->update($cond, $data, array("multiple" => false)); - } catch (MongoException $e) { + $params = $this->collectionOptions['update']; + try { + if ($Model->mongoNoSetOperator === true) { + $this->lastResult = $mongoCollectionObj->replaceOne($cond, $data, $params); + } else { + $this->lastResult = $mongoCollectionObj->updateOne($cond, $data, $params); + } + $return = true; + } catch (MongoDB\Driver\Exception\Exception $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)) + $this->logQuery("db.{$Model->table}.update( :conditions, :data, :params )", + array('conditions' => $cond, 'data' => $data, 'params' => $params) ); } } else { - try{ - $return = $mongoCollectionObj->save($data); - } catch (MongoException $e) { + // Not sure this block ever executes. + // If $data['_id'] is empty does the Model call $this->create() instead ?? + $params = $this->collectionOptions['save']; + try { + $this->lastResult = $mongoCollectionObj->insertOne($data, $params); + $return = true; + } catch (MongoDB\Driver\Exception\Exception $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; } - /** * setMongoUpdateOperator * @@ -755,15 +802,15 @@ public function update(&$Model, $fields = null, $values = null, $conditions = nu * @access public */ public function setMongoUpdateOperator(&$Model, $data) { - if(isset($data['updated'])) { + if (isset($data['updated'])) { $updateField = 'updated'; } else { - $updateField = 'modified'; + $updateField = 'modified'; } //setting Mongo operator - if(empty($Model->mongoNoSetOperator)) { - if(!preg_grep('/^\$/', array_keys($data))) { + if (empty($Model->mongoNoSetOperator)) { + if (! preg_grep('/^\$/', array_keys($data))) { $data = array('$set' => $data); } else { if(!empty($data[$updateField])) { @@ -772,22 +819,20 @@ public function setMongoUpdateOperator(&$Model, $data) { $data['$set'] = array($updateField => $modified); } } - } elseif(substr($Model->mongoNoSetOperator,0,1) === '$') { - if(!empty($data[$updateField])) { + } elseif (substr($Model->mongoNoSetOperator,0,1) === '$') { + 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); - } } - return $data; } /** - * Update multiple Record + * Update multiple documents * * @param Model $Model Model Instance * @param array $fields Field data @@ -796,31 +841,30 @@ public function setMongoUpdateOperator(&$Model, $data) { * @access public */ public function updateAll(&$Model, $fields = null, $conditions = null) { - if (!$this->isConnected()) { + if (! $this->isConnected()) { return false; } $this->_stripAlias($conditions, $Model->alias); $this->_stripAlias($fields, $Model->alias, false, 'value'); - $fields = $this->setMongoUpdateOperator($Model, $fields); - - $this->_prepareLogQuery($Model); // just sets a timer - try{ - $return = $this->_db + $this->_prepareLogQuery($Model); + + try { + $this->lastResult = $this->_db ->selectCollection($Model->table) - ->update($conditions, $fields, array("multiple" => true)); - } catch (MongoException $e) { + ->updateMany($conditions, $fields); + } catch (MongoDB\Driver\Exception\Exception $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)) + $this->logQuery("db.{$Model->table}.update( :conditions, :fields, :params )", + array('conditions' => $conditions, 'fields' => $fields, 'params' => $this->collectionOptions['update']) ); } - return $return; + return ! empty($this->lastResult); } /** @@ -832,7 +876,7 @@ public function updateAll(&$Model, $fields = null, $conditions = null) { * @access public */ public function deriveSchemaFromData($Model, $data = array()) { - if (!$data) { + if (! $data) { $data = $Model->data; if ($data && array_key_exists($Model->alias, $data)) { $data = $data[$Model->alias]; @@ -878,50 +922,47 @@ public function deriveSchemaFromData($Model, $data = array()) { * @return boolean Update result * @access public */ - public function delete(&$Model, $conditions = null) { - if (!$this->isConnected()) { + public function delete(Model $Model, $conditions = null) { + if (! $this->isConnected()) { return false; } $id = null; - $this->_stripAlias($conditions, $Model->alias); if ($conditions === true) { $conditions = array(); } elseif (empty($conditions)) { $id = $Model->id; - } elseif (!empty($conditions) && !is_array($conditions)) { + } elseif (! empty($conditions) && !is_array($conditions)) { $id = $conditions; $conditions = array(); } - $mongoCollectionObj = $this->_db - ->selectCollection($Model->table); - $this->_stripAlias($conditions, $Model->alias); - if (!empty($id)) { + if (! empty($id)) { $conditions['_id'] = $id; } - if (!empty($conditions['_id'])) { + if (! empty($conditions['_id'])) { $this->_convertId($conditions['_id'], true); } $return = false; - $r = false; - try{ - $this->_prepareLogQuery($Model); // just sets a timer - $return = $mongoCollectionObj->remove($conditions); - if ($this->fullDebug) { - $this->logQuery("db.{$Model->useTable}.remove( :conditions )", - compact('conditions') - ); - } + $count = 0; + try { + $this->_prepareLogQuery($Model); + $this->lastResult = $this->_db + ->selectCollection($Model->table) + ->deleteMany($conditions); + $count = $this->lastResult->getDeletedCount(); $return = true; - } catch (MongoException $e) { + } catch (MongoDB\Driver\Exception\Exception $e) { $this->error = $e->getMessage(); trigger_error($this->error); } + if ($this->fullDebug) { + $this->logQuery("db.{$Model->table}.remove( :conditions )", compact('conditions', 'count')); + } return $return; } @@ -935,33 +976,47 @@ public function delete(&$Model, $conditions = null) { * @return array Results * @access public */ - public function read(&$Model, $query = array()) { - if (!$this->isConnected()) { + public function read(Model $Model, $query = array(), $recursive = null) { + if (! $this->isConnected()) { return false; } $this->_setEmptyValues($query); extract($query); - if (!empty($order[0])) { + if (! empty($order[0])) { $order = array_shift($order); } $this->_stripAlias($conditions, $Model->alias); $this->_stripAlias($fields, $Model->alias, false, 'value'); $this->_stripAlias($order, $Model->alias, false, 'both'); - if (!empty($conditions['_id'])) { + if (! empty($conditions['_id'])) { $this->_convertId($conditions['_id']); } $fields = (is_array($fields)) ? $fields : array($fields => 1); + // Check for string keys in $fields array. + // New mongodb driver not happy using field names as array values for projection, + // it wants field names as keys eg. array(field1 => 1 , field2 => 1, field3 => 1) + // So clean that up here. + if (count(array_filter(array_keys($fields), 'is_string')) == 0) { + // No string keys found .. assuming sequential array + $tmp = array(); + foreach($fields as $field) { + $tmp[$field] = 1; + } + $fields = $tmp; + } + if ($conditions === true) { $conditions = array(); } elseif (!is_array($conditions)) { $conditions = array($conditions); } - $order = (is_array($order)) ? $order : array($order); + // TODO : Janky ! Rework. + $order = (is_array($order)) ? $order : array($order); if (is_array($order)) { foreach($order as $field => &$dir) { if (is_numeric($field) || is_null($dir)) { @@ -979,81 +1034,128 @@ public function read(&$Model, $query = array()) { } } - if (empty($offset) && $page && $limit) { + if (empty($offset) && $page && $limit) $offset = ($page - 1) * $limit; - } $return = array(); + $this->_prepareLogQuery($Model); - $this->_prepareLogQuery($Model); // just sets a timer - if (empty($modify)) { - if ($Model->findQueryType === 'count' && $fields == array('count' => true)) { + $queryType = isset($Model->findQueryType) ? $Model->findQueryType : 'all'; + + if ($queryType === 'count') { + try { $count = $this->_db ->selectCollection($Model->table) ->count($conditions); - if ($this->fullDebug) { - $this->logQuery("db.{$Model->useTable}.count( :conditions )", - compact('conditions', 'count') - ); + } catch (MongoDB\Driver\Exception\Exception $e) { + $this->error = $e->getMessage(); + trigger_error($this->error); + } + + if ($this->fullDebug) + $this->logQuery("db.{$Model->useTable}.count( :conditions )", compact('conditions', 'count')); + + return array(array($Model->alias => array('count' => $count))); + } + + if ($queryType === 'all' || $queryType === 'first') { + $options = array( + 'projection' => $fields, + 'sort' => $order, + 'limit' => $limit, + 'skip' => $offset + ); + if (! empty($this->_findOptions)) { + $options = array_merge($options, $this->_findOptions); + } + + $count = 0; + try { + $cursor = $this->_db + ->selectCollection($Model->table) + ->find($conditions, $options); + + // Iterate over cursor + foreach($cursor as $mongodata) { + if ($this->config['set_string_id'] && ! empty($mongodata['_id']) && is_object($mongodata['_id'])) { + $mongodata['_id'] = $mongodata['_id']->__toString(); + } + $return[][$Model->alias] = $mongodata; + $count++; } - return array(array($Model->alias => array('count' => $count))); + } catch (MongoDB\Driver\Exception\Exception $e) { + $this->error = $e->getMessage(); + trigger_error($this->error); } - $return = $this->_db - ->selectCollection($Model->table) - ->find($conditions, $fields) - ->sort($order) - ->limit($limit) - ->skip($offset); if ($this->fullDebug) { - $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') - ); + $this->logQuery("db.{$Model->table}.find( :conditions, :options )", compact('conditions', 'options', 'count')); } - } else { - $options = array_filter(array( - 'findandmodify' => $Model->table, - 'query' => $conditions, + + return $return; + } + + // There was code in the previous version to allow setting a 'modify' flag to execute a findAndModify .. + // I've moved that into a query type. + if ($Model->findQueryType === 'modify') { + $options = array( + 'projection' => $fields, 'sort' => $order, - 'remove' => !empty($remove), - 'update' => array('$set' => $modify), - 'new' => !empty($new), - 'fields' => $fields, - 'upsert' => !empty($upsert) - )); - $return = $this->_db - ->command($options); - if ($this->fullDebug) { - if ($return['ok']) { - $count = 1; - if ($this->config['set_string_id'] && !empty($return['value']['_id']) && is_object($return['value']['_id'])) { - $return['value']['_id'] = $return['value']['_id']->__toString(); - } - $return[][$Model->alias] = $return['value']; - } else { - $count = 0; + 'limit' => $limit, + 'skip' => $offset, + 'returnDocument' => ! empty($new), + 'upsert' => ! empty($upsert), + ); + + // Merge preset options + if (! empty($this->_findOptions)) + $options = array_merge($options, $this->_findOptions); + + // If remove is set then replace the document otherwise update. + if (! empty($remove)) { + try { + $this->lastResult = $this->_db + ->selectCollection($Model->table) + ->findOneAndReplace($conditions, array('$set' => $modify), $options); + } catch (MongoDB\Driver\Exception\Exception $e) { + $this->error = $e->getMessage(); + trigger_error($this->error); + } + + if ($this->fullDebug) { + $logQuery = "db.{$Model->table}.findOneAndReplace( :conditions, :options )"; + } + } else { + + try { + $this->lastResult = $this->_db + ->selectCollection($Model->table) + ->findOneAndUpdate($conditions, array('$set' => $modify), $options); + } catch (MongoDB\Driver\Exception\Exception $e) { + $this->error = $e->getMessage(); + trigger_error($this->error); + } + + if ($this->fullDebug) { + $logQuery = "db.{$Model->table}.findOneAndUpdate( :conditions, :options )"; } - $this->logQuery("db.runCommand( :options )", - array('options' => array_filter($options), 'count' => $count) - ); } - } - if ($Model->findQueryType === 'count') { - return array(array($Model->alias => array('count' => $return->count()))); - } + //$result = MongoDB\BSON\toPHP($this->lastResult); + $result = $this->lastResult; - if (is_object($return)) { - $_return = array(); - while ($return->hasNext()) { - $mongodata = $return->getNext(); - if ($this->config['set_string_id'] && !empty($mongodata['_id']) && is_object($mongodata['_id'])) { - $mongodata['_id'] = $mongodata['_id']->__toString(); + $count = 0; + if (! empty($result)) { + $count = 1; + if ($this->config['set_string_id'] && ! empty($result['_id']) && is_object($result['_id'])) { + $result['_id'] = $result['_id']->__toString(); } - $_return[][$Model->alias] = $mongodata; + $return[][$Model->alias] = $result; + } + + if ($this->fullDebug) { + $this->logQuery($logQuery, compact($conditions, $options, $count)); } - return $_return; } return $return; } @@ -1078,11 +1180,18 @@ public function rollback() { * @access public */ public function truncate($table) { - if (!$this->isConnected()) { + if (! $this->isConnected()) { return false; } - - return $this->execute('db.' . $this->fullTableName($table) . '.remove();'); + try { + return $this->_db + ->selectCollection($table) + ->deleteMany(); + } catch (MongoDB\Driver\Exception\Exception $e) { + $this->error = $e->getMessage(); + trigger_error($this->error); + } + return false; } /** @@ -1094,62 +1203,70 @@ public function truncate($table) { * @return void * @access public */ - public function query($query, $params = array()) { - if (!$this->isConnected()) { - return false; + public function query() { + $args = func_get_args(); + $query = $args[0]; + $params = array(); + if (count($args) > 1) { + $params = $args[1]; } - if($query === 'getMongoDb') { + if (! $this->isConnected()) + return false; + + if ($query === 'getMongoDb') return $this->getMongoDb(); - } - $this->_prepareLogQuery($Model); // just sets a timer - $return = $this->_db - ->command($query); - if ($this->fullDebug) { - $this->logQuery("db.runCommand( :query )", compact('query')); + // Compatibility with previous plugin + if ($query == 'db.version()') { + $doc = $this->query(array('serverStatus' => 1)); + return $doc['version']; } + $this->_prepareLogQuery($Model); + $return = array(); + try { + $cursor = $this->_db + ->command($query); + + if (! is_object($cursor)) { + if ($this->fullDebug) { + $this->logQuery("Failed : db.command( :query )", compact('query')); + } + return false; + } + + $count = 0; + // Its a cursor - but is it always only a one document cursor ? + foreach($cursor as $doc) { + $return = $doc; + $count++; + } + + if ($this->fullDebug) { + $this->logQuery("db.command( :query )", compact('query', 'count')); + } + } catch (MongoDB\Driver\Exception\Exception $e) { + $this->error = $e->getMessage(); + trigger_error($this->error); + } return $return; } /** * mapReduce * + * Method maintained for backwards compatibility + * * @param mixed $query - * @param integer $timeout (milli second) + * @param integer $timeout (milli second) NOTE: Currently ignored. * @return mixed false or array * @access public */ public function mapReduce($query, $timeout = null) { - - //above MongoDB1.8, query must object. - if(isset($query['query']) && !is_object($query['query'])) { - $query['query'] = (object)$query['query']; - } - - $result = $this->query($query); - - if($result['ok']) { - 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; - } - return false; + return $this->query($query); } - - /** * Prepares a value, or an array of values for database queries by quoting and escaping them. * @@ -1178,30 +1295,14 @@ public function value($data, $column = null, $read = true) { * @return void * @access public */ - public function execute($query, $params = array()) { - if (!$this->isConnected()) { + public function execute($query, $options = array(), $params = array()) { + if (! $this->isConnected()) return false; - } - if (!$query || $query === true) { + if (! $query || $query === true) return; - } - $this->_prepareLogQuery($Model); // just sets a timer - $return = $this->_db - ->execute($query, $params); - if ($this->fullDebug) { - if ($params) { - $this->logQuery(":query, :params", - compact('query', 'params') - ); - } else { - $this->logQuery($query); - } - } - if ($return['ok']) { - return $return['retval']; - } - return $return; + + return $this->query($query, $params); } /** @@ -1213,16 +1314,11 @@ public function execute($query, $params = array()) { * @access protected */ protected function _setEmptyValues(&$data, $integers = array('limit', 'offset')) { - if (!is_array($data)) { + if (! is_array($data)) return; - } foreach($data as $key => $value) { if (empty($value)) { - if (in_array($key, $integers)) { - $data[$key] = 0; - } else { - $data[$key] = array(); - } + $data[$key] = (in_array($key, $integers)) ? 0 : array(); } } } @@ -1237,7 +1333,7 @@ protected function _setEmptyValues(&$data, $integers = array('limit', 'offset')) * @access protected */ protected function _prepareLogQuery(&$Model) { - if (!$this->fullDebug) { + if (! $this->fullDebug) { return false; } $this->_startTime = microtime(true); @@ -1248,22 +1344,6 @@ protected function _prepareLogQuery(&$Model) { return true; } -/** - * setTimeout Method - * - * Sets the MongoCursor timeout so long queries (like map / reduce) can run at will. - * Expressed in milliseconds, for an infinite timeout, set to -1 - * - * @param int $ms - * @return boolean - * @access public - */ - public function setTimeout($ms){ - MongoCursor::$timeout = $ms; - - return true; - } - /** * logQuery method * @@ -1280,18 +1360,17 @@ public function setTimeout($ms){ public function logQuery($query, $args = array()) { if ($args) { $this->_stringify($args); - $query = String::insert($query, $args); + $query = CakeText::insert($query, $args); } $this->took = round((microtime(true) - $this->_startTime) * 1000, 0); $this->affected = null; if (empty($this->error['err'])) { - $this->error = $this->_db->lastError(); + //$this->error = $this->_db->lastError(); if (!is_scalar($this->error)) { $this->error = json_encode($this->error); } } - $this->numRows = !empty($args['count'])?$args['count']:null; - + $this->numRows = ! empty($args['count']) ? $args['count'] : null; $query = preg_replace('@"ObjectId\((.*?)\)"@', 'ObjectId ("\1")', $query); return parent::logQuery($query); } @@ -1312,13 +1391,13 @@ protected function _convertId(&$mixed, $conditions = false) { if (strlen($mixed) !== 24) { return; } - $mixed = new MongoId($mixed); + $mixed = new MongoDB\BSON\ObjectID($mixed); } if (is_array($mixed)) { foreach($mixed as &$row) { $this->_convertId($row, false); } - if (!empty($mixed[0]) && $conditions) { + if (! empty($mixed[0]) && $conditions) { $mixed = array('$in' => $mixed); } } @@ -1341,9 +1420,9 @@ protected function _stringify(&$args = array(), $level = 0) { $this->_stringify($arg, $level + 1); } elseif (is_object($arg) && is_callable(array($arg, '__toString'))) { $class = get_class($arg); - if ($class === 'MongoId') { + if ($class === 'MongoDB\BSON\ObjectID') { $arg = 'ObjectId(' . $arg->__toString() . ')'; - } elseif ($class === 'MongoRegex') { + } elseif ($class === 'MongoDB\BSON\Regex ') { $arg = '_regexstart_' . $arg->__toString() . '_regexend_'; } else { $arg = $class . '(' . $arg->__toString() . ')'; @@ -1412,8 +1491,5 @@ protected function _stripAlias(&$args = array(), $alias = 'Model', $recurse = tr * @access public */ function MongoDbDateFormatter($date = null) { - if ($date) { - return new MongoDate($date); - } - return new MongoDate(); + return ($date) ? new MongoDB\BSON\UTCDateTime($date) : new MongoDB\BSON\UTCDateTime(time()); } \ No newline at end of file diff --git a/README.markdown b/README.markdown index 18047de..1af6e53 100644 --- a/README.markdown +++ b/README.markdown @@ -1,23 +1,30 @@ -# mongoDB datasource for CakePHP +# MongoDB datasource for CakePHP 2.8+ + +Note : This datasource is Beta, feedback and pull requests (with tests) welcome. :-) ## Requirements -PHP5, -pecl mongo (http://php.net/mongo) + + PHP 5.6, PHP 7+ + CakePHP 2.x (> 2.8, < 3.0) + +## Dependency + + php-mongodb extension (not the php-mongo extension). + mongodb-php-library (https://docs.mongodb.com/php-library/master/, dependency is listed in composer) ## Installation -this repository should be installed in the same way as any other plugin. +This repository should be installed in the same way as any other plugin. +See http://book.cakephp.org/2.0/en/plugins/how-to-install-plugins.html +Use composer + + composer require "handsetdetection/mongodblib=^1.0.0" + To install the driver for use in a single application: cd my/app/plugins - git clone git://github.com/ichikaway/cakephp-mongodb.git mongodb - -To install the driver for use in any/multiple application(s) - - # where ROOT is the name of the directory parent to the base index.php of CakePHP. - cd ROOT/plugins - git clone git://github.com/ichikaway/cakephp-mongodb.git mongodb + git clone git://github.com/HandsetDetection/MongoDBLib.git MongoDBLib ## Sample Code @@ -28,7 +35,7 @@ To use this DB driver, install (obviously) and define a db source such as follow class DATABASE_CONFIG { public $default = array( - 'driver' => 'mongodb.mongodbSource', + 'driver' => 'MongoDBLib.mongodbSource', 'database' => 'driver', 'host' => 'localhost', 'port' => 27017, @@ -42,35 +49,58 @@ To use this DB driver, install (obviously) and define a db source such as follow ); -More detail of replicaset in wiki: -https://github.com/ichikaway/cakephp-mongodb/wiki/How-to-connect-to-replicaset-servers +Model files need to have mongoSchema property, or make use of the schemaless behavior. +Mongo uses a primary key named "\_id" (cannot be renamed). It can be any format you like but if you don't explicitly set it Mongo will use an automatic 24 character (uu)id. -Model files need to have mongoSchema property - or make use of the schemaless behavior. +## Update Notes -Mongo uses a primary key named "\_id" (cannot be renamed). It can be any format you like but if you don't explicitly set it Mongo will use an automatic 24 character (uu)id. +This plugin was derived from the most excellent cakephp-mongodb plugin by Yasushi Ichikawa, originally built for CakePHP 1.3. +cakephp-mongodb works with the php-mono extension which has since been deprecated and will not run on PHP 7.0. -Before you start, you may find it useful to see [a model sample.](http://github.com/ichikaway/mongoDB-Datasource/blob/master/samples/models/post.php) -There are also some sample [controller actions: find,save,delete,deleteAll,updateAll](http://github.com/ichikaway/mongoDB-Datasource/blob/master/samples/controllers/posts_controller.php) note that your controller code needs no specific code to use this datasource. +This plugin is an updated version of cakephp-mongodb and strives to be as backwardly compatible as possible. It uses the +newer php-mongodb extension. If you're migrating from cakephp-mongodb to this plugin please be aware there are a number +of breaking changes. Namely, all the types and classes have changed. -## Author -Yasushi Ichikawa ([ichikaway](http://twitter.com/ichikaway)) +1) Types : All the types have changed. + +MongoId becomes MongoDB\BSON\ObjectID +MongoCode becomes MongoDB\BSON\JavaScript +MongoDate becomes MongoDB\BSON\UTCDateTime +MongoRegex becomes MongoDB\BSON\Regex +MongoBinData becomes MongoDB\BSON\Binary +MongoInt32 php-mongodb extension now chooses the best type automagically. +MongoInt64 php-mongodb extension now chooses the best type automagically. +MongoDBRef deprecated - no corresponding class +MongoMinKey becomes MongoDB\BSON\MinKey +MongoMaxKey becomes MongoDB\BSON\MaxKey +MongoTimestamp becomes MongoDB\BSON\Timestamp + +2) Classes +There's too much detail to cover here with the class changes so check out these references below. +Old classes : http://php.net/manual/en/book.mongo.php +New classes : http://php.net/manual/en/set.mongodb.php + +3) All CRUD calls are the same and should return identical information to the cakephp-mongodb plugin. +Calls doing mapReduce, aggregation and executing database commands should be extensively tested. + + +## Original Authors + +Yasushi Ichikawa ([ichikaway](http://twitter.com/ichikaway)) Andy Dawson ([AD7six](http://twitter.com/AD7six)) -## Contributors -[Predominant](http://github.com/predominant/) : Cleanup code, add documentation +## Original Contributors +[Predominant](http://github.com/predominant/) : Cleanup code, add documentation [Jrbasso](http://github.com/jrbasso/) : Cleanup code - [tkyk](http://github.com/tkyk/) : Fix bug, Add some function. -## Reference -Reference code, Thank you! +## Original Reference +Reference code, Thank you! [Nate Abele's lithium mongoDB datasource](http://li3.rad-dev.org/) - [Joél Perras' divan](http://github.com/jperras/divan/) - diff --git a/tests/cases/datasources/mongodb_source.test.php b/Test/Case/Datasource/MongodbSourceTest.php similarity index 74% rename from tests/cases/datasources/mongodb_source.test.php rename to Test/Case/Datasource/MongodbSourceTest.php index 9a8ec4e..da65c01 100644 --- a/tests/cases/datasources/mongodb_source.test.php +++ b/Test/Case/Datasource/MongodbSourceTest.php @@ -19,12 +19,11 @@ /** * Import relevant classes for testing */ -App::import('Model', 'Mongodb.MongodbSource'); -/** - * Generate Mock Model - */ -Mock::generate('AppModel', 'MockPost'); +App::uses('Model', 'Model'); +App::uses('AppModel', 'Model'); + + /** * Post Model for the test @@ -87,7 +86,6 @@ function manualUniqueValidation($check) { * @subpackage mongodb.tests.cases.datasources */ class MongoArticle extends AppModel { - public $useDbConfig = 'mongo_test'; } @@ -115,7 +113,7 @@ class MongodbSourceTest extends CakeTestCase { * */ protected $_config = array( - 'datasource' => 'mongodb', + 'datasource' => 'MongoDBLib.MongodbSource', 'host' => 'localhost', 'login' => '', 'password' => '', @@ -131,13 +129,10 @@ class MongodbSourceTest extends CakeTestCase { * @return void * @access public */ - public function startTest() { - $connections = ConnectionManager::enumConnectionObjects(); - - if (!empty($connections['test']['classname']) && $connections['test']['classname'] === 'mongodbSource') { - $config = new DATABASE_CONFIG(); - $this->_config = $config->test; - } + public function setUp() { + // Recreate the $test database definition as our Datasource. + ConnectionManager::drop('test'); + ConnectionManager::create('test', $this->_config); ConnectionManager::create('mongo_test', $this->_config); $this->Mongo = new MongodbSource($this->_config); @@ -145,10 +140,15 @@ public function startTest() { $this->Post = ClassRegistry::init('Post'); $this->Post->setDataSource('mongo_test'); - $this->mongodb =& ConnectionManager::getDataSource($this->Post->useDbConfig); + $this->mongodb = ConnectionManager::getDataSource('mongo_test'); $this->mongodb->connect(); $this->dropData(); + + $this->MongoArticle = ClassRegistry::init('MongoArticle'); + $this->MongoArticle->setDataSource('mongo_test'); + + //syslog(LOG_NOTICE, 'setUp '.$this->getName()); } /** @@ -157,7 +157,7 @@ public function startTest() { * @return void * @access public */ - public function endTest() { + public function tearDown() { $this->dropData(); unset($this->Post); unset($this->Mongo); @@ -188,9 +188,9 @@ public function insertData($data) { try { $this->mongodb ->connection - ->selectDB($this->_config['database']) + ->selectDatabase($this->_config['database']) ->selectCollection($this->Post->table) - ->insert($data, true); + ->insertOne($data); } catch (MongoException $e) { trigger_error($e->getMessage()); } @@ -206,10 +206,18 @@ public function dropData() { try { $db = $this->mongodb ->connection - ->selectDB($this->_config['database']); + ->selectDatabase($this->_config['database']); foreach($db->listCollections() as $collection) { - $collection->drop(); + $collectionName = $collection->getName(); + // Don't drop system collections. + if (! preg_match("/^system\./", $collectionName)) { + $result = $this->mongodb + ->connection + ->selectDatabase($this->_config['database']) + ->selectCollection($collectionName) + ->drop(); + } } } catch (MongoException $e) { trigger_error($e->getMessage()); @@ -223,54 +231,35 @@ public function dropData() { * @return void * @access public */ - public function testCreateConnectionName() { - $config = array( - 'datasource' => 'mongodb', - 'host' => 'localhost', - 'login' => '', - 'password' => '', - 'database' => 'test_mongo', - 'port' => 27017, - 'prefix' => '', - 'persistent' => false, - ); - $version = '1.2.2'; + public function testCreateConnectionName() { + $config = array( + 'datasource' => 'MongoDBLib.MongodbSource', + 'host' => 'localhost', + 'login' => '', + 'password' => '', + 'database' => 'test_mongo', + 'port' => 27017, + 'prefix' => '', + 'persistent' => false, + ); $expect = 'mongodb://localhost:27017'; - $host = $this->mongodb->createConnectionName($config, $version); + $host = $this->mongodb->createConnectionString($config); $this->assertIdentical($expect, $host); - - $config = array( - 'datasource' => 'mongodb', - 'host' => 'localhost', - 'login' => 'user', - 'password' => 'pass', - 'database' => 'test_mongo', - 'port' => 27017, - 'prefix' => '', - 'persistent' => false, - ); - $version = '1.2.2'; + $config = array( + 'datasource' => 'MongoDBLib.MongodbSource', + 'host' => 'localhost', + 'login' => 'user', + 'password' => 'pass', + 'database' => 'test_mongo', + 'port' => 27017, + 'prefix' => '', + 'persistent' => false, + ); $expect = 'mongodb://user:pass@localhost:27017/test_mongo'; - $host = $this->mongodb->createConnectionName($config, $version); + $host = $this->mongodb->createConnectionString($config); $this->assertIdentical($expect, $host); - - - $config = array( - 'datasource' => 'mongodb', - 'host' => 'localhost', - 'login' => 'user', - 'password' => 'pass', - 'database' => 'test_mongo', - 'port' => 27017, - 'prefix' => '', - 'persistent' => false, - ); - $version = '1.0.0'; - $expect = 'user:pass@localhost:27017/test_mongo'; - $host = $this->mongodb->createConnectionName($config, $version); - $this->assertIdentical($expect, $host); - } + } /** * Tests connection @@ -281,7 +270,6 @@ public function testCreateConnectionName() { public function testConnect() { $result = $this->Mongo->connect(); $this->assertTrue($result); - $this->assertTrue($this->Mongo->connected); $this->assertTrue($this->Mongo->isConnected()); } @@ -305,7 +293,7 @@ public function testDisconnect() { * @access public */ public function testListSources() { - $this->assertTrue(is_array($this->mongodb->listSources())); + $this->assertTrue($this->mongodb->listSources()); } /** @@ -318,11 +306,11 @@ public function testGetMongoDb() { $obj = $this->mongodb->getMongoDb(); $this->assertTrue(is_object($obj)); $objName = get_class($obj); - $this->assertEqual('MongoDB', $objName); + $this->assertEqual('MongoDB\Database', $objName); } /** - * Tests the Model::getMongoDb() call MongodbSource::getMongoDb + * Tests the Model::getMongoDb() call Mongodb::getMongoDb * * @return void * @access public @@ -331,7 +319,7 @@ public function testGetMongoDbFromModel() { $obj = $this->Post->getMongoDb(); $this->assertTrue(is_object($obj)); $objName = get_class($obj); - $this->assertEqual('MongoDB', $objName); + $this->assertEqual('MongoDB\Database', $objName); } /** @@ -344,7 +332,7 @@ public function testGetMongoCollection() { $obj = $this->mongodb->getMongoCollection($this->Post); $this->assertTrue(is_object($obj)); $objName = get_class($obj); - $this->assertEqual('MongoCollection', $objName); + $this->assertEqual('MongoDB\Collection', $objName); } /** @@ -354,7 +342,7 @@ public function testGetMongoCollection() { * @access public */ public function testDescribe() { - $mockObj = new MockPost(); + $mockObj = $this->getMock('AppModel'); $result = $this->mongodb->describe($mockObj); $expected = array( @@ -420,7 +408,7 @@ public function testSave() { $this->Post->create(); $saveResult = $this->Post->save($saveData); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $result = $this->Post->find('all'); @@ -433,8 +421,8 @@ public function testSave() { $this->assertEqual($data['body'], $resultData['body']); $this->assertEqual($data['text'], $resultData['text']); - $this->assertTrue(is_a($resultData['created'], 'MongoDate')); - $this->assertTrue(is_a($resultData['modified'], 'MongoDate')); + $this->assertTrue(is_a($resultData['created'], 'MongoDB\BSON\UTCDateTime')); + $this->assertTrue(is_a($resultData['modified'], 'MongoDB\BSON\UTCDateTime')); } /** @@ -452,7 +440,7 @@ public function testCheckInsertIdAfterSaving() { $this->Post->create(); $saveResult = $this->Post->save($saveData); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $this->assertEqual($this->Post->id, $this->Post->getInsertId()); $this->assertTrue(is_string($this->Post->id)); @@ -468,7 +456,7 @@ public function testCheckInsertIdAfterSaving() { $this->Post->create(); $saveResult = $this->Post->save($saveData); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $this->assertEqual($saveData['Post']['_id'] ,$this->Post->id); $this->assertEqual($this->Post->id, $this->Post->getInsertId()); @@ -482,8 +470,6 @@ public function testCheckInsertIdAfterSaving() { } - - /** * Tests saveAll method. * @@ -517,8 +503,8 @@ public function testSaveAll() { $this->assertEqual($data['body'], $resultData['body']); $this->assertEqual($data['text'], $resultData['text']); - $this->assertTrue(is_a($resultData['created'], 'MongoDate')); - $this->assertTrue(is_a($resultData['modified'], 'MongoDate')); + $this->assertTrue(is_a($resultData['created'], 'MongoDB\BSON\UTCDateTime')); + $this->assertTrue(is_a($resultData['modified'], 'MongoDB\BSON\UTCDateTime')); $resultData = $result[1]['Post']; $this->assertEqual(6, count($resultData)); @@ -528,8 +514,8 @@ public function testSaveAll() { $this->assertEqual($data['body'], $resultData['body']); $this->assertEqual($data['text'], $resultData['text']); - $this->assertTrue(is_a($resultData['created'], 'MongoDate')); - $this->assertTrue(is_a($resultData['modified'], 'MongoDate')); + $this->assertTrue(is_a($resultData['created'], 'MongoDB\BSON\UTCDateTime')); + $this->assertTrue(is_a($resultData['modified'], 'MongoDB\BSON\UTCDateTime')); } /** @@ -556,8 +542,8 @@ public function testUpdate() { $count1 = $this->Post->find('count'); $this->assertIdentical($count1 - $count0, 1, 'Save failed to create one row'); - $this->assertTrue($saveResult); - $this->assertTrue($postId); + $this->assertNotEmpty($saveResult); + $this->assertNotEmpty($postId); $findresult = $this->Post->find('all'); $this->assertEqual(0, $findresult[0]['Post']['count']); @@ -573,7 +559,7 @@ public function testUpdate() { $count2 = $this->Post->find('count'); $this->assertIdentical($count2 - $count1, 0, 'Save test 2 created another row, it did not update the existing row'); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $this->assertIdentical($this->Post->id, $postId); $this->Post->create(); @@ -589,7 +575,7 @@ public function testUpdate() { $count3 = $this->Post->find('count'); $this->assertIdentical($count3 - $count2, 0, 'Saving with the id in the data created another row'); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $this->assertIdentical($this->Post->id, $postId); $this->Post->create(); @@ -605,7 +591,7 @@ public function testUpdate() { $count4 = $this->Post->find('count'); $this->assertIdentical($count4 - $count3, 0, 'Saving with $Model->id set and no id in the data created another row'); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $this->assertIdentical($this->Post->id, $postId); $result = $this->Post->find('all'); @@ -620,7 +606,6 @@ public function testUpdate() { $this->assertEqual($updatedata['text'], $resultData['text']); $this->assertEqual(0, $resultData['count']); - // using $inc operator $this->Post->mongoNoSetOperator = '$inc'; $this->Post->create(); @@ -631,7 +616,7 @@ public function testUpdate() { $saveData['Post'] = $updatedataIncrement; $saveResult = $this->Post->save($saveData); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $this->assertIdentical($this->Post->id, $postId); $result = $this->Post->find('all'); @@ -689,8 +674,8 @@ public function testUpdateAll() { $this->assertEqual('ichikawa', $resultData['name']); $this->assertEqual($data['body'], $resultData['body']); $this->assertEqual($data['text'], $resultData['text']); - $this->assertTrue(is_a($resultData['created'], 'MongoDate')); - $this->assertTrue(is_a($resultData['modified'], 'MongoDate')); + $this->assertTrue(is_a($resultData['created'], 'MongoDB\BSON\UTCDateTime')); + $this->assertTrue(is_a($resultData['modified'], 'MongoDB\BSON\UTCDateTime')); $resultData = $result[1]['Post']; @@ -701,8 +686,8 @@ public function testUpdateAll() { $this->assertEqual('ichikawa', $resultData['name']); $this->assertEqual($data['body'], $resultData['body']); $this->assertEqual($data['text'], $resultData['text']); - $this->assertTrue(is_a($resultData['created'], 'MongoDate')); - $this->assertTrue(is_a($resultData['modified'], 'MongoDate')); + $this->assertTrue(is_a($resultData['created'], 'MongoDB\BSON\UTCDateTime')); + $this->assertTrue(is_a($resultData['modified'], 'MongoDB\BSON\UTCDateTime')); } /** @@ -778,10 +763,9 @@ public function testSetMongoUpdateOperator() { $result = $ds->setMongoUpdateOperator($this->Post, $data); $this->assertEqual($expect, $result); - + $this->Post->mongoNoSetOperator = null; } - /** * Tests update method without $set operator. * @@ -789,78 +773,78 @@ public function testSetMongoUpdateOperator() { * @access public */ public function testUpdateWithoutMongoSchemaProperty() { - $MongoArticle = ClassRegistry::init('MongoArticle'); + $this->MongoArticle = ClassRegistry::init('MongoArticle'); + syslog(LOG_NOTICE, "MongoArticle ".json_encode($this->MongoArticle)); + $data = array( 'title' => 'test', 'body' => 'aaaa', 'text' => 'bbbb', 'count' => 0, - 'created' => new mongoDate(), - 'modified' => new mongoDate(), + 'created' => new MongoDB\BSON\UTCDateTime(time()), + 'modified' => new MongoDB\BSON\UTCDateTime(time()), ); $saveData['MongoArticle'] = $data; - $MongoArticle->create(); - $saveResult = $MongoArticle->save($saveData); - $postId = $MongoArticle->id; + $this->MongoArticle->create(); + $saveResult = $this->MongoArticle->save($saveData); + $postId = $this->MongoArticle->id; //using $set operator - $MongoArticle->create(); + $this->MongoArticle->create(); $updatedata = array( '_id' => $postId, 'title' => 'test3', 'body' => 'aaaa3', ); $saveData['MongoArticle'] = $updatedata; - $saveResult = $MongoArticle->save($saveData); // using $set operator + $saveResult = $this->MongoArticle->save($saveData); // using $set operator - $this->assertTrue($saveResult); - $this->assertIdentical($MongoArticle->id, $postId); + $this->assertNotEmpty($saveResult); + $this->assertIdentical($this->MongoArticle->id, $postId); $result = null; - $result = $MongoArticle->find('all'); + $result = $this->MongoArticle->find('all'); $this->assertEqual(1, count($result)); $resultData = $result[0]['MongoArticle']; - $this->assertEqual($MongoArticle->id, $resultData['_id']); + $this->assertEqual($this->MongoArticle->id, $resultData['_id']); $this->assertEqual($updatedata['title'], $resultData['title']); //update $this->assertEqual($updatedata['body'], $resultData['body']); //update $this->assertEqual($data['text'], $resultData['text']); //not update $this->assertEqual($data['count'], $resultData['count']); //not update - - //using $inc operator insted of $set operator - $MongoArticle->create(); + $this->MongoArticle->create(); $updatedataInc = array( '_id' => $postId, '$inc' => array('count' => 1), ); $saveData['MongoArticle'] = $updatedataInc; - $saveResult = $MongoArticle->save($saveData); // using $set operator + $saveResult = $this->MongoArticle->save($saveData); // using $set operator - $this->assertTrue($saveResult); - $this->assertIdentical($MongoArticle->id, $postId); + $this->assertNotEmpty($saveResult); + $this->assertIdentical($this->MongoArticle->id, $postId); $result = null; - $result = $MongoArticle->find('all'); + $result = $this->MongoArticle->find('all'); $this->assertEqual(1, count($result)); $resultData = $result[0]['MongoArticle']; - $this->assertEqual($MongoArticle->id, $resultData['_id']); + $this->assertEqual($this->MongoArticle->id, $resultData['_id']); $this->assertEqual($updatedata['title'], $resultData['title']); //not update $this->assertEqual($updatedata['body'], $resultData['body']); //not update $this->assertEqual($data['text'], $resultData['text']); //not update $this->assertEqual(1, $resultData['count']); //increment //using $inc and $push - $MongoArticle->create(); + $this->MongoArticle->create(); $updatedataInc = array( '_id' => $postId, '$push' => array( 'comments' => array( - '_id' => new MongoId(), - 'created' => new MongoDate(), + '_id' => new MongoDB\BSON\ObjectID(), + 'created' => new MongoDB\BSON\UTCDateTime(time()), 'vote_count' => 0, 'user' => 'user1', 'body' => 'comment', @@ -869,16 +853,16 @@ public function testUpdateWithoutMongoSchemaProperty() { '$inc' => array('count' => 1), ); $saveData['MongoArticle'] = $updatedataInc; - $saveResult = $MongoArticle->save($saveData); // using $set operator + $saveResult = $this->MongoArticle->save($saveData); // using $set operator - $this->assertTrue($saveResult); - $this->assertIdentical($MongoArticle->id, $postId); + $this->assertNotEmpty($saveResult); + $this->assertIdentical($this->MongoArticle->id, $postId); $result = null; - $result = $MongoArticle->find('all'); + $result = $this->MongoArticle->find('all'); $this->assertEqual(1, count($result)); $resultData = $result[0]['MongoArticle']; - $this->assertEqual($MongoArticle->id, $resultData['_id']); + $this->assertEqual($this->MongoArticle->id, $resultData['_id']); $this->assertEqual($updatedata['title'], $resultData['title']); //not update $this->assertEqual($updatedata['body'], $resultData['body']); //not update $this->assertEqual($data['text'], $resultData['text']); //not update @@ -889,11 +873,10 @@ public function testUpdateWithoutMongoSchemaProperty() { $this->assertTrue(!empty($resultData['created'])); $this->assertTrue(!empty($resultData['modified'])); - //no $set operator - $MongoArticle->mongoNoSetOperator = true; + $this->MongoArticle->mongoNoSetOperator = true; - $MongoArticle->create(); + $this->MongoArticle->create(); $updatedata = array( '_id' => $postId, 'title' => 'test4', @@ -901,67 +884,62 @@ public function testUpdateWithoutMongoSchemaProperty() { 'count' => '1', ); $saveData['MongoArticle'] = $updatedata; - $saveResult = $MongoArticle->save($saveData); + $saveResult = $this->MongoArticle->save($saveData); - $this->assertTrue($saveResult); - $this->assertIdentical($MongoArticle->id, $postId); + $this->assertNotEmpty($saveResult); + $this->assertIdentical($this->MongoArticle->id, $postId); $result = null; - $result = $MongoArticle->find('all'); + $result = $this->MongoArticle->find('all'); $this->assertEqual(1, count($result)); $resultData = $result[0]['MongoArticle']; - $this->assertEqual($MongoArticle->id, $resultData['_id']); + $this->assertEqual($this->MongoArticle->id, $resultData['_id']); $this->assertEqual($updatedata['title'], $resultData['title']); //update $this->assertEqual($updatedata['body'], $resultData['body']); //update $this->assertTrue(empty($resultData['text'])); $this->assertEqual(1, $resultData['count']); - $MongoArticle->mongoNoSetOperator = null; - + $this->MongoArticle->mongoNoSetOperator = null; //use $push - $MongoArticle->create(); + $this->MongoArticle->create(); $updatedata = array( '_id' => $postId, 'push_column' => array('push1'), ); $saveData['MongoArticle'] = $updatedata; - $saveResult = $MongoArticle->save($saveData); //use $set + $saveResult = $this->MongoArticle->save($saveData); //use $set - $result = $MongoArticle->find('all'); + $result = $this->MongoArticle->find('all'); $resultData = $result[0]['MongoArticle']; $this->assertEqual('test4', $resultData['title']); // no update $this->assertEqual(array('push1'), $resultData['push_column']); - - $MongoArticle->mongoNoSetOperator = '$push'; - $MongoArticle->create(); + $this->MongoArticle->mongoNoSetOperator = '$push'; + $this->MongoArticle->create(); $updatedata = array( '_id' => $postId, 'push_column' => 'push2', ); $saveData['MongoArticle'] = $updatedata; - $saveResult = $MongoArticle->save($saveData); //use $push + $saveResult = $this->MongoArticle->save($saveData); //use $push - $this->assertTrue($saveResult); - $this->assertIdentical($MongoArticle->id, $postId); + $this->assertNotEmpty($saveResult); + $this->assertIdentical($this->MongoArticle->id, $postId); $result = null; - $result = $MongoArticle->find('all'); - + $result = $this->MongoArticle->find('all'); $this->assertEqual(1, count($result)); $resultData = $result[0]['MongoArticle']; - $this->assertEqual($MongoArticle->id, $resultData['_id']); + $this->assertEqual($this->MongoArticle->id, $resultData['_id']); $this->assertEqual('test4', $resultData['title']); // no update $this->assertEqual(array('push1','push2'), $resultData['push_column']); //update - $MongoArticle->mongoNoSetOperator = null; - - - unset($MongoArticle); + $this->MongoArticle->mongoNoSetOperator = null; + unset($this->MongoArticle); } @@ -972,13 +950,13 @@ public function testUpdateWithoutMongoSchemaProperty() { * @access public */ public function testGroupBy() { - for($i = 0 ; $i < 30 ; $i++) { + for ($i = 0 ; $i < 30 ; $i++) { $saveData[$i]['Post'] = array( - 'title' => 'test'.$i, - 'body' => 'aaaa'.$i, - 'text' => 'bbbb'.$i, - 'count' => $i, - ); + 'title' => 'test'.$i, + 'body' => 'aaaa'.$i, + 'text' => 'bbbb'.$i, + 'count' => $i, + ); } $saveData[30]['Post'] = array( @@ -999,16 +977,14 @@ public function testGroupBy() { $cond_count = 5; $query = array( - 'key' => array('title' => true ), - 'initial' => array('csum' => 0), - 'reduce' => 'function(obj, prev){prev.csum += 1;}', - 'options' => array( - 'condition' => array('count' => array('$lt' => $cond_count)), - ), - ); + 'key' => array('title' => true ), + 'initial' => array('csum' => 0), + 'reduce' => 'function(obj, result){result.csum += 1;}', + 'conditions' => array('count' => array('$lt' => $cond_count)), + ); $mongo = $this->Post->getDataSource(); - $result = $mongo->group($this->Post, $query); + $result = $mongo->group($query, $this->Post); $this->assertTrue($result['ok'] == 1 && count($result['retval']) > 0); $this->assertEqual($cond_count, count($result['retval'])); @@ -1018,10 +994,7 @@ public function testGroupBy() { $this->assertEqual(1, $result['retval'][0]['csum']); $this->assertEqual(2, $result['retval'][1]['csum']); $this->assertEqual(2, $result['retval'][2]['csum']); - -} - - + } /** * Tests query @@ -1033,11 +1006,11 @@ public function testGroupBy() { public function testQuery() { for($i = 0 ; $i < 30 ; $i++) { $saveData[$i]['Post'] = array( - 'title' => 'test'.$i, - 'body' => 'aaaa'.$i, - 'text' => 'bbbb'.$i, - 'count' => $i, - ); + 'title' => 'test'.$i, + 'body' => 'aaaa'.$i, + 'text' => 'bbbb'.$i, + 'count' => $i, + ); } $saveData[30]['Post'] = array( @@ -1063,7 +1036,6 @@ public function testQuery() { $this->Post->create(); $saveResult = $this->Post->saveAll($saveData); - //using query() Distinct $params = array( 'distinct' => 'posts', @@ -1074,19 +1046,18 @@ public function testQuery() { $this->assertEqual(2, $result['values'][2]); $this->assertEqual(32, $result['values'][30]); - //using query() group $cond_count = 5; $reduce = "function(obj,prev){prev.csum++;}"; $params = array( - 'group'=>array( - 'ns'=>'posts', - 'cond'=>array('count' => array('$lt' => $cond_count)), - 'key'=>array('title'=>true), - 'initial'=>array('csum'=>0), - '$reduce'=>$reduce - ) - ); + 'group' => array( + 'ns' => 'posts', + 'cond' => array('count' => array('$lt' => $cond_count)), + 'key' => array('title' => true), + 'initial' => array('csum' => 0), + '$reduce' => $reduce + ) + ); $result = $this->Post->query( $params ); @@ -1098,8 +1069,7 @@ public function testQuery() { $this->assertEqual(1, $result['retval'][0]['csum']); $this->assertEqual(2, $result['retval'][1]['csum']); $this->assertEqual(2, $result['retval'][2]['csum']); - -} + } /** * Tests MapReduce @@ -1107,107 +1077,85 @@ public function testQuery() { * @return void * @access public */ -public function testMapReduce() { - for($i = 0 ; $i < 30 ; $i++) { - $saveData[$i]['Post'] = array( + public function testMapReduce() { + for ($i = 0 ; $i < 30 ; $i++) { + $saveData[$i]['Post'] = array( 'title' => 'test'.$i, 'body' => 'aaaa'.$i, 'text' => 'bbbb'.$i, 'count' => $i, - ); - } + ); + } - $saveData[30]['Post'] = array( + $saveData[30]['Post'] = array( 'title' => 'test1', 'body' => 'aaaa1', 'text' => 'bbbb1', 'count' => 1, - ); - $saveData[31]['Post'] = array( + ); + + $saveData[31]['Post'] = array( 'title' => 'test2', 'body' => 'aaaa2', 'text' => 'bbbb2', 'count' => 2, - ); + ); - $saveData[32]['Post'] = array( + $saveData[32]['Post'] = array( 'title' => 'test2', 'body' => 'aaaa2', 'text' => 'bbbb2', 'count' => 32, - ); + ); - $this->Post->create(); - $saveResult = $this->Post->saveAll($saveData); + $this->Post->create(); + $saveResult = $this->Post->saveAll($saveData); - $map = new MongoCode("function() { emit(this.title,1); }"); - $reduce = new MongoCode("function(k, vals) { ". - "var sum = 0;". - "for (var i in vals) {". - "sum += vals[i];". - "}". - "return sum; }" + $map = new MongoDB\BSON\Javascript("function() { emit(this.title,1); }"); + $reduce = new MongoDB\BSON\Javascript("function(k, vals) { ". + "var sum = 0;". + "for (var i in vals) {". + "sum += vals[i];". + "}". + "return sum; }" ); - $params = array( - "mapreduce" => "posts", + // Results output to collection. + $params = array( + "mapReduce" => "posts", "map" => $map, "reduce" => $reduce, "query" => array( "count" => array('$gt' => -2), - ), + ), 'out' => 'test_mapreduce_posts', - ); - - $mongo = $this->Post->getDataSource(); - $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']); - - - //set timeout - $results = $mongo->mapReduce($params, 100); //set timeout 100msec - $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']); + ); + $mongo = $this->Post->getDataSource(); + $reply = $mongo->mapReduce($params); + $this->assertEqual($reply['result'], 'test_mapreduce_posts'); + $this->assertEqual($reply['ok'], 1); - //get results as inline data - $version = $this->getMongodVersion(); - if( $version >= '1.7.4') { + // Results inline $params = array( - "mapreduce" => "posts", - "map" => $map, - "reduce" => $reduce, - "query" => array( - "count" => array('$gt' => -2), - ), - 'out' => array('inline' => 1), - ); - - $results = $mongo->mapReduce($params); + "mapReduce" => "posts", + "map" => $map, + "reduce" => $reduce, + "query" => array( + "count" => array('$gt' => -2), + ), + 'out' => array('inline' => 1), + ); + + $reply = $this->Post->query($params); + $this->assertTrue(is_array($reply['results'])); + $this->assertEqual($reply['ok'], 1); $posts = array(); - foreach ($results as $post) { + foreach ($reply['results'] as $post) { $posts[$post['_id']] = $post['value']; } - + $this->assertEqual(30, count($posts)); $this->assertEqual(1, $posts['test0']); $this->assertEqual(2, $posts['test1']); @@ -1215,11 +1163,6 @@ public function testMapReduce() { $this->assertEqual(1, $posts['test3']); } - -} - - - /** * testSort method * @@ -1306,12 +1249,13 @@ public function testSchemaless() { 'created' => null ); - $MongoArticle = ClassRegistry::init('MongoArticle'); - $MongoArticle->create(); - $this->assertTrue($MongoArticle->save($toSave), 'Saving with no defined schema failed'); + $this->MongoArticle = ClassRegistry::init('MongoArticle'); + $this->MongoArticle->create(); + $this->assertNotEmpty($this->MongoArticle->save($toSave), 'Saving with no defined schema failed'); $expected = array_intersect_key($toSave, array_flip(array('title', 'body', 'tags'))); - $result = $MongoArticle->read(array('title', 'body', 'tags')); + $result = $this->MongoArticle->read(array('title', 'body', 'tags')); + syslog(LOG_NOTICE, 'Schemaless read '.json_encode(array_keys($result['MongoArticle']))); unset ($result['MongoArticle']['_id']); // prevent auto added field from screwing things up $this->assertEqual($expected, $result['MongoArticle']); @@ -1327,9 +1271,9 @@ public function testSchemaless() { 'modified' => null, 'created' => null ); - $MongoArticle->create(); - $this->assertTrue($MongoArticle->save($toSave), 'Saving with no defined schema failed'); - $starts = $MongoArticle->field('starts'); + $this->MongoArticle->create(); + $this->assertNotEmpty($this->MongoArticle->save($toSave), 'Saving with no defined schema failed'); + $starts = $this->MongoArticle->field('starts'); $this->assertEqual($toSave['starts'], $starts); } @@ -1352,7 +1296,7 @@ public function testSpecificId() { $this->Post->create(); $saveResult = $this->Post->save($saveData); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $found = $this->Post->find('first', array( 'fields' => array('_id', 'title', 'body', 'text'), @@ -1371,7 +1315,7 @@ public function testSpecificId() { $this->Post->create(); $saveResult = $this->Post->save($saveData); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $found = $this->Post->find('first', array( 'fields' => array('_id', 'title', 'body', 'text'), @@ -1393,8 +1337,8 @@ public function testOr() { return; } - $MongoArticle = ClassRegistry::init('MongoArticle'); - $MongoArticle->create(); + $this->MongoArticle = ClassRegistry::init('MongoArticle'); + $this->MongoArticle->create(); for ($i = 1; $i <= 20; $i++) { $data = array( @@ -1402,18 +1346,18 @@ public function testOr() { 'subtitle' => "Sub Article $i", ); $saveData['MongoArticle'] = $data; - $MongoArticle->create(); - $MongoArticle->save($saveData); + $this->MongoArticle->create(); + $this->MongoArticle->save($saveData); } - $expected = $MongoArticle->find('all', array( + $expected = $this->MongoArticle->find('all', array( 'conditions' => array( 'title' => array('$in' => array('Article 1', 'Article 10')) ), 'order' => array('number' => 'ASC') )); - $this->assertTrue(count($expected), 2); + $this->assertEqual(count($expected), 2); - $result = $MongoArticle->find('all', array( + $result = $this->MongoArticle->find('all', array( 'conditions' => array( '$or' => array( array('title' => 'Article 1'), @@ -1432,31 +1376,27 @@ public function testOr() { * @access public */ function testDeleteAll($cascade = true) { - $MongoArticle = ClassRegistry::init('MongoArticle'); - $MongoArticle->create(array('title' => 'Article 1', 'cat' => 1)); - $MongoArticle->save(); - - $MongoArticle->create(array('title' => 'Article 2', 'cat' => 1)); - $MongoArticle->save(); - - $MongoArticle->create(array('title' => 'Article 3', 'cat' => 2)); - $MongoArticle->save(); - - $MongoArticle->create(array('title' => 'Article 4', 'cat' => 2)); - $MongoArticle->save(); - - $count = $MongoArticle->find('count'); + $this->MongoArticle = ClassRegistry::init('MongoArticle'); + + $this->MongoArticle->create(array('title' => 'Article 1', 'cat' => 1)); + $this->MongoArticle->save(); + $this->MongoArticle->create(array('title' => 'Article 2', 'cat' => 1)); + $this->MongoArticle->save(); + $this->MongoArticle->create(array('title' => 'Article 3', 'cat' => 2)); + $this->MongoArticle->save(); + $this->MongoArticle->create(array('title' => 'Article 4', 'cat' => 2)); + $this->MongoArticle->save(); + + $count = $this->MongoArticle->find('count'); $this->assertEqual($count, 4); - $MongoArticle->deleteAll(array('cat' => 2), $cascade); - - $count = $MongoArticle->find('count'); + $this->MongoArticle->deleteAll(array('cat' => 2), $cascade); + $count = $this->MongoArticle->find('count'); $this->assertEqual($count, 2); - - $MongoArticle->deleteAll(true, $cascade); - - $count = $MongoArticle->find('count'); - $this->assertFalse($count); + + $this->MongoArticle->deleteAll(true, $cascade); + $count = $this->MongoArticle->find('count'); + $this->assertEqual($count, 0); } /** @@ -1476,24 +1416,27 @@ function testDeleteAllNoCascade() { * @access public */ public function testRegexSearch() { - $MongoArticle = ClassRegistry::init('MongoArticle'); - $MongoArticle->create(array('title' => 'Article 1', 'cat' => 1)); - $MongoArticle->save(); - $MongoArticle->create(array('title' => 'Article 2', 'cat' => 1)); - $MongoArticle->save(); - $MongoArticle->create(array('title' => 'Article 3', 'cat' => 2)); - $MongoArticle->save(); - - $count=$MongoArticle->find('count',array( - 'conditions'=>array( - 'title'=>'Article 2' + + $this->MongoArticle = ClassRegistry::init('MongoArticle'); + $this->MongoArticle->create(array('title' => 'Article 1', 'cat' => 1)); + $this->MongoArticle->save(); + + $this->MongoArticle->create(array('title' => 'Article 2', 'cat' => 1)); + $this->MongoArticle->save(); + + $this->MongoArticle->create(array('title' => 'Article 3', 'cat' => 2)); + $this->MongoArticle->save(); + + $count = $this->MongoArticle->find('count', array( + 'conditions' => array( + 'title' => 'Article 2' ) )); $this->assertEqual($count, 1); - $count = $MongoArticle->find('count',array( - 'conditions'=>array( - 'title'=> new MongoRegex('/^Article/') + $count = $this->MongoArticle->find('count', array( + 'conditions' => array( + 'title' => new MongoDB\BSON\Regex('^Article', '') ) )); $this->assertEqual($count, 3); @@ -1506,21 +1449,25 @@ public function testRegexSearch() { * @access public */ public function testEmptyReturn() { - $MongoArticle = ClassRegistry::init('MongoArticle'); - $MongoArticle->create(array('title' => 'Article 1', 'cat' => 1)); - $MongoArticle->save(); - $articles=$MongoArticle->find('all',array( - 'conditions'=>array( - 'title'=>'Article 2' + $this->MongoArticle = ClassRegistry::init('MongoArticle'); + $this->MongoArticle->create(array('title' => 'Article 1', 'cat' => 1)); + $this->MongoArticle->save(); + $articles = $this->MongoArticle->find('all', array( + 'conditions' => array( + 'title' => 'Article 2' ) )); $this->assertTrue(is_array($articles)); - $articles=$MongoArticle->find('first',array( - 'conditions'=>array( - 'title'=>'Article 2' + + $articles = $this->MongoArticle->find('first', array( + 'conditions' => array( + 'title' => 'Article 2' ) )); - $this->assertFalse(is_array($articles)); + // http://www.codeproject.com/Articles/987758/Changes-to-Model-find-first-in-CakePHP + // find('first') with not matching conditions resutns array(). + $this->assertTrue(is_array($articles)); + $this->assertEmpty($articles); } /** @@ -1538,10 +1485,10 @@ public function testSaveUniques() { ); $saveData['Post'] = $data; - $this->Post->Behaviors->attach('Mongodb.SqlCompatible'); + $this->Post->Behaviors->attach('MongoDBLib.SqlCompatible'); $this->Post->create(); $saveResult = $this->Post->save($saveData); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $data = array( 'title' => 'test', @@ -1572,7 +1519,7 @@ public function testSaveUniquesCustom() { $saveData['Post'] = $data; $this->Post->create(); $saveResult = $this->Post->save($saveData); - $this->assertTrue($saveResult); + $this->assertNotEmpty($saveResult); $data = array( 'title' => 'test', 'body' => 'asdf', @@ -1586,88 +1533,89 @@ public function testSaveUniquesCustom() { } public function testReturn() { - $MongoArticle = ClassRegistry::init('MongoArticle'); - $MongoArticle->create(array('title' => 'Article 1', 'cat' => 1)); - $MongoArticle->save(); - $MongoArticle->create(array('title' => 'Article 2', 'cat' => 1)); - $MongoArticle->save(); + $this->MongoArticle = ClassRegistry::init('MongoArticle'); + $this->MongoArticle->create(array('title' => 'Article 1', 'cat' => 1)); + $this->MongoArticle->save(); + $this->MongoArticle->create(array('title' => 'Article 2', 'cat' => 1)); + $this->MongoArticle->save(); - $return = $MongoArticle->find('all', array( + $return = $this->MongoArticle->find('all', array( 'conditions' => array( 'title' => 'Article 2' ) )); $this->assertTrue(is_array($return)); - $return = $MongoArticle->find('first', array( + $return = $this->MongoArticle->find('first', array( 'conditions' => array( 'title' => 'Article 2' ) )); $this->assertTrue(is_array($return)); - $return = $MongoArticle->find('first', array( + $return = $this->MongoArticle->find('first', array( 'conditions' => array( 'title' => 'Article 2' ) )); $this->assertTrue(is_array($return)); - $return = $MongoArticle->find('count', array( + $return = $this->MongoArticle->find('count', array( 'conditions' => array( 'title' => 'Article 2' ) )); $this->assertTrue(is_int($return)); - $return = $MongoArticle->find('neighbors', array( + $return = $this->MongoArticle->find('neighbors', array( 'conditions' => array( 'title' => 'Article 2' ) )); $this->assertTrue(is_array($return)); - $return = $MongoArticle->find('list', array( + $return = $this->MongoArticle->find('list', array( 'conditions' => array( 'title' => 'Article 2' ) )); $this->assertTrue(is_array($return)); - $return = $MongoArticle->find('all', array( + $return = $this->MongoArticle->find('all', array( 'conditions' => array( 'title' => 'Doesn\'t exist' ) )); $this->assertTrue(is_array($return)); - $return = $MongoArticle->find('first', array( + $return = $this->MongoArticle->find('first', array( 'conditions' => array( 'title' => 'Doesn\'t exist' ) )); - $this->assertFalse($return); + $this->assertTrue(is_array($return)); + $this->assertEmpty($return); - $return = $MongoArticle->find('count', array( + $return = $this->MongoArticle->find('count', array( 'conditions' => array( 'title' => 'Doesn\'t exist' ) )); $this->assertTrue(is_int($return)); + $this->assertEqual(0, $return); - $return = $MongoArticle->find('neighbors', array( + $return = $this->MongoArticle->find('neighbors', array( 'conditions' => array( 'title' => 'Doesn\'t exist' ) )); $this->assertTrue(is_array($return)); - $return = $MongoArticle->find('list', array( + $return = $this->MongoArticle->find('list', array( 'conditions' => array( 'title' => 'Doesn\'t exist' ) )); $this->assertTrue(is_array($return)); - } } \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..21be56a --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "handsetdetection/mongodblib", + "description": "A CakePHP Driver from MongoDB using the new mongodb extension. For CakePHP 2.8+ (Not CakePHP 3.x)", + "homepage": "https://github.com/HandsetDetection/MongoDBLib", + "keywords": ["cakephp", "mongodb", "php", "mongo"], + "minimum-stability": "dev", + "license": "MIT", + "type": "cakephp-plugin", + "require": { + "php": ">=5.6", + "mongodb/mongodb": ">=1.0.0" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, + "authors": [ + { + "name": "Richard Uren", + "email": "elricho66@gmail.com", + "homepage": "http://www.handsetdetection.com/" + } + ] +} \ No newline at end of file diff --git a/docs/__filesource/fsource_mongodb_mongodb-models-behaviors_behaviorsschemaless.php.html b/docs/__filesource/fsource_mongodb_mongodb-models-behaviors_behaviorsschemaless.php.html deleted file mode 100644 index 5f8ef30..0000000 --- a/docs/__filesource/fsource_mongodb_mongodb-models-behaviors_behaviorsschemaless.php.html +++ /dev/null @@ -1,109 +0,0 @@ - - - - - - File Source for schemaless.php - - - - -

Source for file schemaless.php

-

Documentation is available at schemaless.php

-
-
  1. <?php
  2. -
  3. /**
  4. -
  5.  * Schemaless behavior.
  6. -
  7.  *
  8. -
  9.  * Adds functionality specific to MongoDB/schemaless dbs
  10. -
  11.  * Allow /not/ specifying the model's schema, and derive it (for cake-compatibility) from the data
  12. -
  13.  * being saved. Note that used carelessly this is a pretty dangerous thing to allow - means a user
  14. -
  15.  * can modify input forms adding whatever fields they like (unless you'er using the security
  16. -
  17.  * component) and fill your db with their junk.
  18. -
  19.  *
  20. -
  21.  * PHP version 5
  22. -
  23.  *
  24. -
  25.  * Copyright (c) 2010, Andy Dawson
  26. -
  27.  *
  28. -
  29.  * Licensed under The MIT License
  30. -
  31.  * Redistributions of files must retain the above copyright notice.
  32. -
  33.  *
  34. -
  35.  * @filesource
  36. -
  37.  * @copyright     Copyright (c) 2010, Andy Dawson
  38. -
  39.  * @link          www.ad7six.com
  40. -
  41.  * @package       mongodb
  42. -
  43.  * @subpackage    mongodb.models.behaviors
  44. -
  45.  * @since         v 1.0 (24-May-2010)
  46. -
  47.  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
  48. -
  49.  */
  50. -
  51.  
  52. -
  53. /**
  54. -
  55.  * SchemalessBehavior class
  56. -
  57.  *
  58. -
  59.  * @uses          ModelBehavior
  60. -
  61.  * @package       mongodb
  62. -
  63.  * @subpackage    mongodb.models.behaviors
  64. -
  65.  */
  66. -
  67. class SchemalessBehavior extends ModelBehavior {
  68. -
  69.  
  70. -
  71. /**
  72. -
  73.  * name property
  74. -
  75.  *
  76. -
  77.  * @var string 'Schemaless'
  78. -
  79.  * @access public
  80. -
  81.  */
  82. -
  83.     public $name = 'Schemaless';
  84. -
  85.  
  86. -
  87. /**
  88. -
  89.  * settings property
  90. -
  91.  *
  92. -
  93.  * @var array 
  94. -
  95.  * @access public
  96. -
  97.  */
  98. -
  99.     public $settings = array();
  100. -
  101.  
  102. -
  103. /**
  104. -
  105.  * defaultSettings property
  106. -
  107.  *
  108. -
  109.  * @var array 
  110. -
  111.  * @access protected
  112. -
  113.  */
  114. -
  115.     protected $_defaultSettings = array(
  116. -
  117.     );
  118. -
  119.  
  120. -
  121. /**
  122. -
  123.  * setup method
  124. -
  125.  *
  126. -
  127.  * Don't currently have any settings at all - disabled
  128. -
  129.  *
  130. -
  131.  * @param mixed $Model 
  132. -
  133.  * @param array $config array()
  134. -
  135.  * @return void 
  136. -
  137.  * @access public
  138. -
  139.  */
  140. -
  141.     public function setup(&$Model$config array()) {
  142. -
  143.         //$this->settings[$Model->alias] = array_merge($this->_defaultSettings, $config);
  144. -
  145.     }
  146. -
  147.  
  148. -
  149. /**
  150. -
  151.  * beforeSave method
  152. -
  153.  *
  154. -
  155.  * Set the schema to allow saving whatever has been passed
  156. -
  157.  *
  158. -
  159.  * @param mixed $Model 
  160. -
  161.  * @return void 
  162. -
  163.  * @access public
  164. -
  165.  */
  166. -
  167.     public function beforeSave(&$Model{
  168. -
  169.         $Model->cacheSources false;
  170. -
  171.         $Model->schema(true);
  172. -
  173.         return true;
  174. -
  175.     }
  176. -
  177. }
  178. -
-
-

- Documentation generated on Tue, 26 Jul 2011 01:09:01 +0900 by phpDocumentor 1.4.3 -

- - \ No newline at end of file diff --git a/docs/__filesource/fsource_mongodb_mongodb-models-behaviors_behaviorssql_compatible.php.html b/docs/__filesource/fsource_mongodb_mongodb-models-behaviors_behaviorssql_compatible.php.html deleted file mode 100644 index 8181790..0000000 --- a/docs/__filesource/fsource_mongodb_mongodb-models-behaviors_behaviorssql_compatible.php.html +++ /dev/null @@ -1,290 +0,0 @@ - - - - - - File Source for sql_compatible.php - - - - -

Source for file sql_compatible.php

-

Documentation is available at sql_compatible.php

-
-
  1. <?php
  2. -
  3. /**
  4. -
  5.  * Sql Compatible.
  6. -
  7.  *
  8. -
  9.  * Attach this behavior to be able to query mongo DBs without using mongo specific syntax.
  10. -
  11.  * If you don't need this behavior don't attach it and save a few cycles
  12. -
  13.  *
  14. -
  15.  * PHP version 5
  16. -
  17.  *
  18. -
  19.  * Copyright (c) 2010, Andy Dawson
  20. -
  21.  *
  22. -
  23.  * Licensed under The MIT License
  24. -
  25.  * Redistributions of files must retain the above copyright notice.
  26. -
  27.  *
  28. -
  29.  * @filesource
  30. -
  31.  * @copyright     Copyright (c) 2010, Andy Dawson
  32. -
  33.  * @link          www.ad7six.com
  34. -
  35.  * @package       mongodb
  36. -
  37.  * @subpackage    mongodb.models.behaviors
  38. -
  39.  * @since         v 1.0 (24-May-2010)
  40. -
  41.  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
  42. -
  43.  */
  44. -
  45.  
  46. -
  47. /**
  48. -
  49.  * SqlCompatibleBehavior class
  50. -
  51.  *
  52. -
  53.  * @uses          ModelBehavior
  54. -
  55.  * @package       mongodb
  56. -
  57.  * @subpackage    mongodb.models.behaviors
  58. -
  59.  */
  60. -
  61. class SqlCompatibleBehavior extends ModelBehavior {
  62. -
  63.  
  64. -
  65. /**
  66. -
  67.  * name property
  68. -
  69.  *
  70. -
  71.  * @var string 'SqlCompatible'
  72. -
  73.  * @access public
  74. -
  75.  */
  76. -
  77.     public $name = 'SqlCompatible';
  78. -
  79.  
  80. -
  81. /**
  82. -
  83.  * Runtime settings
  84. -
  85.  *
  86. -
  87.  * Keyed on model alias
  88. -
  89.  *
  90. -
  91.  * @var array 
  92. -
  93.  * @access public
  94. -
  95.  */
  96. -
  97.     public $settings = array();
  98. -
  99.  
  100. -
  101. /**
  102. -
  103.  * defaultSettings property
  104. -
  105.  *
  106. -
  107.  * @var array 
  108. -
  109.  * @access protected
  110. -
  111.  */
  112. -
  113.     protected $_defaultSettings = array(
  114. -
  115.         'convertDates' => true,
  116. -
  117.         'operators' => array(
  118. -
  119.             '!=' => '$ne',
  120. -
  121.             '>' => '$gt',
  122. -
  123.             '>=' => '$gte',
  124. -
  125.             '<' => '$lt',
  126. -
  127.             '<=' => '$lte',
  128. -
  129.             'IN' => '$in',
  130. -
  131.             'NOT' => '$not',
  132. -
  133.             'NOT IN' => '$nin'
  134. -
  135.         )
  136. -
  137.     );
  138. -
  139.  
  140. -
  141. /**
  142. -
  143.  * setup method
  144. -
  145.  *
  146. -
  147.  * Allow overriding the operator map
  148. -
  149.  *
  150. -
  151.  * @param mixed $Model 
  152. -
  153.  * @param array $config array()
  154. -
  155.  * @return void 
  156. -
  157.  * @access public
  158. -
  159.  */
  160. -
  161.     public function setup(&$Model$config array()) {
  162. -
  163.         $this->settings[$Model->aliasarray_merge($this->_defaultSettings$config);
  164. -
  165.     }
  166. -
  167.  
  168. -
  169. /**
  170. -
  171.  * If requested, convert dates from MongoDate objects to standard date strings
  172. -
  173.  *
  174. -
  175.  * @param mixed $Model 
  176. -
  177.  * @param mixed $results 
  178. -
  179.  * @param mixed $primary 
  180. -
  181.  * @return void 
  182. -
  183.  * @access public
  184. -
  185.  */
  186. -
  187.     public function afterFind(&$Model$results$primary{
  188. -
  189.         if ($this->settings[$Model->alias]['convertDates']{
  190. -
  191.             $this->convertDates($results);
  192. -
  193.         }
  194. -
  195.         return $results;
  196. -
  197.     }
  198. -
  199.  
  200. -
  201. /**
  202. -
  203.  * beforeFind method
  204. -
  205.  *
  206. -
  207.  * If conditions are an array ensure they are mongified
  208. -
  209.  *
  210. -
  211.  * @param mixed $Model 
  212. -
  213.  * @param mixed $query 
  214. -
  215.  * @return void 
  216. -
  217.  * @access public
  218. -
  219.  */
  220. -
  221.     public function beforeFind(&$Model$query{
  222. -
  223.         if (is_array($query['conditions']&& $this->_translateConditions($Model$query['conditions'])) {
  224. -
  225.             return $query;
  226. -
  227.         }
  228. -
  229.         return true;
  230. -
  231.     }
  232. -
  233.  
  234. -
  235. /**
  236. -
  237.  * Convert MongoDate objects to strings for the purpose of view simplicity
  238. -
  239.  *
  240. -
  241.  * @param mixed $results 
  242. -
  243.  * @return void 
  244. -
  245.  * @access public
  246. -
  247.  */
  248. -
  249.     public function convertDates(&$results{
  250. -
  251.         if (is_array($results)) {
  252. -
  253.             foreach($results as &$row{
  254. -
  255.                 $this->convertDates($row);
  256. -
  257.             }
  258. -
  259.         elseif (is_a($results'MongoDate')) {
  260. -
  261.             $results date('Y-M-d h:i:s'$results->sec);
  262. -
  263.         }
  264. -
  265.     }
  266. -
  267.  
  268. -
  269. /**
  270. -
  271.  * translateConditions method
  272. -
  273.  *
  274. -
  275.  * Loop on conditions and desqlify them
  276. -
  277.  *
  278. -
  279.  * @param mixed $Model 
  280. -
  281.  * @param mixed $conditions 
  282. -
  283.  * @return void 
  284. -
  285.  * @access protected
  286. -
  287.  */
  288. -
  289.     protected function _translateConditions(&$Model&$conditions{
  290. -
  291.         $return false;
  292. -
  293.         foreach($conditions as $key => &$value{
  294. -
  295.             $uKey strtoupper($key);
  296. -
  297.             if (substr($uKey-5=== 'NOT IN'{
  298. -
  299.                 // 'Special' case because it has a space in it, and it's the whole key
  300. -
  301.                 $conditions[substr($key0-5)]['$nin'$value;
  302. -
  303.                 unset($conditions[$key]);
  304. -
  305.                 $return true;
  306. -
  307.                 continue;
  308. -
  309.             }
  310. -
  311.             if ($uKey === 'OR'{
  312. -
  313.                 unset($conditions[$key]);
  314. -
  315.                 foreach($value as $key => $part{
  316. -
  317.                     $part array($key => $part);
  318. -
  319.                     $this->_translateConditions($Model$part);
  320. -
  321.                     $conditions['$or'][$part;
  322. -
  323.                 }
  324. -
  325.                 $return true;
  326. -
  327.                 continue;
  328. -
  329.             }
  330. -
  331.             if ($key === $Model->primaryKey && is_array($value)) {
  332. -
  333.                 //_id=>array(1,2,3) pattern, set  $in operator
  334. -
  335.                 $isMongoOperator false;
  336. -
  337.                 foreach($value as $idKey => $idValue{
  338. -
  339.                     //check a mongo operator exists
  340. -
  341.                     if(substr($idKey,0,1=== '$'{
  342. -
  343.                         $isMongoOperator true;
  344. -
  345.                         continue;
  346. -
  347.                     }
  348. -
  349.                 }
  350. -
  351.                 unset($idKey$idValue);
  352. -
  353.                 if($isMongoOperator === false{
  354. -
  355.                     $conditions[$keyarray('$in' => $value);
  356. -
  357.                 }
  358. -
  359.                 $return true;
  360. -
  361.                 continue;
  362. -
  363.             }
  364. -
  365.             if (substr($uKey-3=== 'NOT'{
  366. -
  367.                 // 'Special' case because it's awkward
  368. -
  369.                 $childKey key($value);
  370. -
  371.                 $childValue current($value);
  372. -
  373.  
  374. -
  375.                 if (in_array(substr($childKey-1)array('>''<''='))) {
  376. -
  377.                     $parts explode(' '$childKey);
  378. -
  379.                     $operator array_pop($parts);
  380. -
  381.                     if ($operator $this->_translateOperator($Model$operator)) {
  382. -
  383.                         $childKey implode(' '$parts);
  384. -
  385.                     }
  386. -
  387.                 else {
  388. -
  389.                     $conditions[$childKey]['$nin'= (array)$childValue;
  390. -
  391.                     unset($conditions['NOT']);
  392. -
  393.                     $return true;
  394. -
  395.                     continue;
  396. -
  397.                 }
  398. -
  399.  
  400. -
  401.                 $conditions[$childKey]['$not'][$operator$childValue;
  402. -
  403.                 unset($conditions['NOT']);
  404. -
  405.                 $return true;
  406. -
  407.                 continue;
  408. -
  409.             }
  410. -
  411.             if (substr($uKey-5=== ' LIKE'{
  412. -
  413.                 // 'Special' case because it's awkward
  414. -
  415.                 if ($value[0=== '%'{
  416. -
  417.                     $value substr($value1);
  418. -
  419.                 else {
  420. -
  421.                     $value '^' $value;
  422. -
  423.                 }
  424. -
  425.                 if (substr($value-1=== '%'{
  426. -
  427.                     $value substr($value0-1);
  428. -
  429.                 else {
  430. -
  431.                     $value .= '$';
  432. -
  433.                 }
  434. -
  435.                 $value str_replace('%''.*'$value);
  436. -
  437.  
  438. -
  439.                 $conditions[substr($key0-5)new MongoRegex("/$value/i");
  440. -
  441.                 unset($conditions[$key]);
  442. -
  443.                 $return true;
  444. -
  445.                 continue;
  446. -
  447.             }
  448. -
  449.  
  450. -
  451.             if (!in_array(substr($key-1)array('>''<''='))) {
  452. -
  453.                 $return true;
  454. -
  455.                 continue;
  456. -
  457.             }
  458. -
  459.             if (is_numeric($key && is_array($value))) {
  460. -
  461.                 if ($this->_translateConditions($Model$value)) {
  462. -
  463.                     $return true;
  464. -
  465.                     continue;
  466. -
  467.                 }
  468. -
  469.             }
  470. -
  471.             $parts explode(' '$key);
  472. -
  473.             $operator array_pop($parts);
  474. -
  475.             if ($operator $this->_translateOperator($Model$operator)) {
  476. -
  477.                 $newKey implode(' '$parts);
  478. -
  479.                 $conditions[$newKey][$operator$value;
  480. -
  481.                 unset($conditions[$key]);
  482. -
  483.                 $return true;
  484. -
  485.             }
  486. -
  487.             if (is_array($value)) {
  488. -
  489.                 if ($this->_translateConditions($Model$value)) {
  490. -
  491.                     $return true;
  492. -
  493.                     continue;
  494. -
  495.                 }
  496. -
  497.             }
  498. -
  499.         }
  500. -
  501.         return $return;
  502. -
  503.     }
  504. -
  505.  
  506. -
  507. /**
  508. -
  509.  * translateOperator method
  510. -
  511.  *
  512. -
  513.  * Use the operator map for the model and return what the db really wants to hear
  514. -
  515.  *
  516. -
  517.  * @param mixed $Model 
  518. -
  519.  * @param mixed $operator 
  520. -
  521.  * @return string 
  522. -
  523.  * @access protected
  524. -
  525.  */
  526. -
  527.     protected function _translateOperator($Model$operator{
  528. -
  529.         if (!empty($this->settings[$Model->alias]['operators'][$operator])) {
  530. -
  531.             return $this->settings[$Model->alias]['operators'][$operator];
  532. -
  533.         }
  534. -
  535.         return '';
  536. -
  537.     }
  538. -
  539. }
  540. -
-
-

- Documentation generated on Tue, 26 Jul 2011 01:09:02 +0900 by phpDocumentor 1.4.3 -

- - \ No newline at end of file diff --git a/docs/blank.html b/docs/blank.html deleted file mode 100644 index 927a8fd..0000000 --- a/docs/blank.html +++ /dev/null @@ -1,13 +0,0 @@ - - - Generated Documentation - - - - -

Generated Documentation

-Welcome to mongodb!
-
-This documentation was generated by phpDocumentor v1.4.3
- - \ No newline at end of file diff --git a/docs/classtrees_mongodb.html b/docs/classtrees_mongodb.html deleted file mode 100644 index 722d39b..0000000 --- a/docs/classtrees_mongodb.html +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - -

- -

-

Root class DboSource

- - -

Root class ModelBehavior

- - -

- Documentation generated on Tue, 26 Jul 2011 01:09:01 +0900 by phpDocumentor 1.4.3 -

- - \ No newline at end of file diff --git a/docs/elementindex.html b/docs/elementindex.html deleted file mode 100644 index 7406437..0000000 --- a/docs/elementindex.html +++ /dev/null @@ -1,674 +0,0 @@ - - - - - - - - - - - -

Full index

-

Package indexes

- -
-
- a - b - c - d - e - g - i - l - m - n - q - r - s - t - u - v - _ -
- - -
-
a
-
top
-
-
-
-
- afterFind -
-
- -
If requested, convert dates from MongoDate objects to standard date strings
-
-
- -
-
b
-
top
-
-
-
-
- beforeFind -
-
- -
beforeFind method
-
-
- beforeSave -
-
- -
beforeSave method
-
-
- begin -
-
-
MongodbSource::begin() in mongodb_source.php
-
begin method
-
-
- -
-
c
-
top
-
-
-
-
- $columns -
-
-
MongodbSource::$columns in mongodb_source.php
-
column definition
-
-
- $connected -
-
-
MongodbSource::$connected in mongodb_source.php
-
Are we connected to the DataSource?
-
-
- calculate -
-
-
MongodbSource::calculate() in mongodb_source.php
-
Calculate
-
-
- close -
-
-
MongodbSource::close() in mongodb_source.php
-
Close database connection
-
-
- commit -
-
-
MongodbSource::commit() in mongodb_source.php
-
commit method
-
-
- connect -
-
-
MongodbSource::connect() in mongodb_source.php
-
Connect to the database
-
-
- convertDates -
-
- -
Convert MongoDate objects to strings for the purpose of view simplicity
-
-
- create -
-
-
MongodbSource::create() in mongodb_source.php
-
Create Data
-
-
- createConnectionName -
-
- -
create connection name.
-
-
- createSchema -
-
-
MongodbSource::createSchema() in mongodb_source.php
-
createSchema method
-
-
- -
-
d
-
top
-
-
-
-
- delete -
-
-
MongodbSource::delete() in mongodb_source.php
-
Delete Data
-
-
- deriveSchemaFromData -
-
- -
deriveSchemaFromData method
-
-
- describe -
-
-
MongodbSource::describe() in mongodb_source.php
-
Describe
-
-
- disconnect -
-
-
MongodbSource::disconnect() in mongodb_source.php
-
Disconnect from the database
-
-
- distinct -
-
-
MongodbSource::distinct() in mongodb_source.php
-
distinct method
-
-
- dropSchema -
-
-
MongodbSource::dropSchema() in mongodb_source.php
-
dropSchema method
-
-
- -
-
e
-
top
-
-
-
-
- ensureIndex -
-
-
MongodbSource::ensureIndex() in mongodb_source.php
-
ensureIndex method
-
-
- execute -
-
-
MongodbSource::execute() in mongodb_source.php
-
execute method
-
-
- -
-
g
-
top
-
-
-
-
- getMongoCollection -
-
- -
get MongoDB Collection Object
-
-
- getMongoDb -
-
-
MongodbSource::getMongoDb() in mongodb_source.php
-
get MongoDB Object
-
-
- group -
-
-
MongodbSource::group() in mongodb_source.php
-
group method
-
-
- -
-
i
-
top
-
-
-
-
- insertMulti -
-
-
MongodbSource::insertMulti() in mongodb_source.php
-
Inserts multiple values into a table
-
-
- isConnected -
-
-
MongodbSource::isConnected() in mongodb_source.php
-
check connection to the database
-
-
- isInterfaceSupported -
-
- -
isInterfaceSupported method
-
-
- -
-
l
-
top
-
-
-
-
- listSources -
-
-
MongodbSource::listSources() in mongodb_source.php
-
Get list of available Collections
-
-
- logQuery -
-
-
MongodbSource::logQuery() in mongodb_source.php
-
logQuery method
-
-
- -
-
m
-
top
-
-
-
-
- mongodb_source.php -
-
-
mongodb_source.php in mongodb_source.php
-
-
- mapReduce -
-
-
MongodbSource::mapReduce() in mongodb_source.php
-
mapReduce
-
-
- MongoDbDateFormatter -
-
-
MongoDbDateFormatter() in mongodb_source.php
-
MongoDbDateFormatter method
-
-
- MongodbSource -
-
-
MongodbSource in mongodb_source.php
-
MongoDB Source
-
-
- -
-
n
-
top
-
-
-
-
- $name -
-
-
SqlCompatibleBehavior::$name in sql_compatible.php
-
name property
-
-
- $name -
-
-
SchemalessBehavior::$name in schemaless.php
-
name property
-
-
- name -
-
-
MongodbSource::name() in mongodb_source.php
-
Quotes identifiers.
-
-
- -
-
q
-
top
-
-
-
-
- query -
-
-
MongodbSource::query() in mongodb_source.php
-
query method If call getMongoDb() from model, this method call getMongoDb().
-
-
- -
-
r
-
top
-
-
-
-
- read -
-
-
MongodbSource::read() in mongodb_source.php
-
Read Data
-
-
- rollback -
-
-
MongodbSource::rollback() in mongodb_source.php
-
rollback method
-
-
- -
-
s
-
top
-
-
-
-
- $settings -
-
-
SqlCompatibleBehavior::$settings in sql_compatible.php
-
Runtime settings
-
-
- $settings -
-
- -
settings property
-
-
- schemaless.php -
-
-
schemaless.php in schemaless.php
-
-
- sql_compatible.php -
-
-
sql_compatible.php in sql_compatible.php
-
-
- SchemalessBehavior -
-
-
SchemalessBehavior in schemaless.php
-
SchemalessBehavior class
-
-
- setTimeout -
-
-
MongodbSource::setTimeout() in mongodb_source.php
-
setTimeout Method
-
-
- setup -
-
-
SqlCompatibleBehavior::setup() in sql_compatible.php
-
setup method
-
-
- setup -
-
-
SchemalessBehavior::setup() in schemaless.php
-
setup method
-
-
- SqlCompatibleBehavior -
-
-
SqlCompatibleBehavior in sql_compatible.php
-
SqlCompatibleBehavior class
-
-
- -
-
t
-
top
-
-
-
-
- truncate -
-
-
MongodbSource::truncate() in mongodb_source.php
-
Deletes all the records in a table
-
-
- -
-
u
-
top
-
-
-
-
- update -
-
-
MongodbSource::update() in mongodb_source.php
-
Update Data
-
-
- updateAll -
-
-
MongodbSource::updateAll() in mongodb_source.php
-
Update multiple Record
-
-
- -
-
v
-
top
-
-
-
-
- value -
-
-
MongodbSource::value() in mongodb_source.php
-
Prepares a value, or an array of values for database queries by quoting and escaping them.
-
-
- -
-
_
-
top
-
-
-
-
- $_baseConfig -
-
-
MongodbSource::$_baseConfig in mongodb_source.php
-
Base Config
-
-
- $_db -
-
-
MongodbSource::$_db in mongodb_source.php
-
Database Instance
-
-
- $_defaultSchema -
-
-
MongodbSource::$_defaultSchema in mongodb_source.php
-
Default schema for the mongo models
-
-
- $_defaultSettings -
-
- -
defaultSettings property
-
-
- $_defaultSettings -
-
- -
defaultSettings property
-
-
- $_driverVersion -
-
-
MongodbSource::$_driverVersion in mongodb_source.php
-
Mongo Driver Version
-
-
- $_startTime -
-
-
MongodbSource::$_startTime in mongodb_source.php
-
startTime property
-
-
- _convertId -
-
-
MongodbSource::_convertId() in mongodb_source.php
-
convertId method
-
-
- _prepareLogQuery -
-
-
MongodbSource::_prepareLogQuery() in mongodb_source.php
-
prepareLogQuery method
-
-
- _setEmptyValues -
-
-
MongodbSource::_setEmptyValues() in mongodb_source.php
-
Set empty values, arrays or integers, for the variables Mongo uses
-
-
- _stringify -
-
-
MongodbSource::_stringify() in mongodb_source.php
-
stringify method
-
-
- _stripAlias -
-
-
MongodbSource::_stripAlias() in mongodb_source.php
-
Convert automatically array('Model.field' => 'foo') to array('field' => 'foo')
-
-
- _translateConditions -
-
- -
translateConditions method
-
-
- _translateOperator -
-
- -
translateOperator method
-
-
- __construct -
-
-
MongodbSource::__construct() in mongodb_source.php
-
construct method
-
-
- __destruct -
-
-
MongodbSource::__destruct() in mongodb_source.php
-
Destruct
-
-
- -
- a - b - c - d - e - g - i - l - m - n - q - r - s - t - u - v - _ -
- \ No newline at end of file diff --git a/docs/elementindex_mongodb.html b/docs/elementindex_mongodb.html deleted file mode 100644 index 89e5bc7..0000000 --- a/docs/elementindex_mongodb.html +++ /dev/null @@ -1,671 +0,0 @@ - - - - - - - - - - - -

[mongodb] element index

-All elements -
-
- a - b - c - d - e - g - i - l - m - n - q - r - s - t - u - v - _ -
- - -
-
_
-
top
-
-
-
-
- $_defaultSettings -
-
- -
defaultSettings property
-
-
- $_defaultSettings -
-
- -
defaultSettings property
-
-
- _translateConditions -
-
- -
translateConditions method
-
-
- _translateOperator -
-
- -
translateOperator method
-
-
- $_baseConfig -
-
-
MongodbSource::$_baseConfig in mongodb_source.php
-
Base Config
-
-
- $_db -
-
-
MongodbSource::$_db in mongodb_source.php
-
Database Instance
-
-
- $_defaultSchema -
-
-
MongodbSource::$_defaultSchema in mongodb_source.php
-
Default schema for the mongo models
-
-
- $_driverVersion -
-
-
MongodbSource::$_driverVersion in mongodb_source.php
-
Mongo Driver Version
-
-
- $_startTime -
-
-
MongodbSource::$_startTime in mongodb_source.php
-
startTime property
-
-
- _convertId -
-
-
MongodbSource::_convertId() in mongodb_source.php
-
convertId method
-
-
- _prepareLogQuery -
-
-
MongodbSource::_prepareLogQuery() in mongodb_source.php
-
prepareLogQuery method
-
-
- _setEmptyValues -
-
-
MongodbSource::_setEmptyValues() in mongodb_source.php
-
Set empty values, arrays or integers, for the variables Mongo uses
-
-
- _stringify -
-
-
MongodbSource::_stringify() in mongodb_source.php
-
stringify method
-
-
- _stripAlias -
-
-
MongodbSource::_stripAlias() in mongodb_source.php
-
Convert automatically array('Model.field' => 'foo') to array('field' => 'foo')
-
-
- __construct -
-
-
MongodbSource::__construct() in mongodb_source.php
-
construct method
-
-
- __destruct -
-
-
MongodbSource::__destruct() in mongodb_source.php
-
Destruct
-
-
- -
-
a
-
top
-
-
-
-
- afterFind -
-
- -
If requested, convert dates from MongoDate objects to standard date strings
-
-
- -
-
b
-
top
-
-
-
-
- beforeFind -
-
- -
beforeFind method
-
-
- beforeSave -
-
- -
beforeSave method
-
-
- begin -
-
-
MongodbSource::begin() in mongodb_source.php
-
begin method
-
-
- -
-
c
-
top
-
-
-
-
- convertDates -
-
- -
Convert MongoDate objects to strings for the purpose of view simplicity
-
-
- $columns -
-
-
MongodbSource::$columns in mongodb_source.php
-
column definition
-
-
- $connected -
-
-
MongodbSource::$connected in mongodb_source.php
-
Are we connected to the DataSource?
-
-
- calculate -
-
-
MongodbSource::calculate() in mongodb_source.php
-
Calculate
-
-
- close -
-
-
MongodbSource::close() in mongodb_source.php
-
Close database connection
-
-
- commit -
-
-
MongodbSource::commit() in mongodb_source.php
-
commit method
-
-
- connect -
-
-
MongodbSource::connect() in mongodb_source.php
-
Connect to the database
-
-
- create -
-
-
MongodbSource::create() in mongodb_source.php
-
Create Data
-
-
- createConnectionName -
-
- -
create connection name.
-
-
- createSchema -
-
-
MongodbSource::createSchema() in mongodb_source.php
-
createSchema method
-
-
- -
-
d
-
top
-
-
-
-
- delete -
-
-
MongodbSource::delete() in mongodb_source.php
-
Delete Data
-
-
- deriveSchemaFromData -
-
- -
deriveSchemaFromData method
-
-
- describe -
-
-
MongodbSource::describe() in mongodb_source.php
-
Describe
-
-
- disconnect -
-
-
MongodbSource::disconnect() in mongodb_source.php
-
Disconnect from the database
-
-
- distinct -
-
-
MongodbSource::distinct() in mongodb_source.php
-
distinct method
-
-
- dropSchema -
-
-
MongodbSource::dropSchema() in mongodb_source.php
-
dropSchema method
-
-
- -
-
e
-
top
-
-
-
-
- ensureIndex -
-
-
MongodbSource::ensureIndex() in mongodb_source.php
-
ensureIndex method
-
-
- execute -
-
-
MongodbSource::execute() in mongodb_source.php
-
execute method
-
-
- -
-
g
-
top
-
-
-
-
- getMongoCollection -
-
- -
get MongoDB Collection Object
-
-
- getMongoDb -
-
-
MongodbSource::getMongoDb() in mongodb_source.php
-
get MongoDB Object
-
-
- group -
-
-
MongodbSource::group() in mongodb_source.php
-
group method
-
-
- -
-
i
-
top
-
-
-
-
- insertMulti -
-
-
MongodbSource::insertMulti() in mongodb_source.php
-
Inserts multiple values into a table
-
-
- isConnected -
-
-
MongodbSource::isConnected() in mongodb_source.php
-
check connection to the database
-
-
- isInterfaceSupported -
-
- -
isInterfaceSupported method
-
-
- -
-
l
-
top
-
-
-
-
- listSources -
-
-
MongodbSource::listSources() in mongodb_source.php
-
Get list of available Collections
-
-
- logQuery -
-
-
MongodbSource::logQuery() in mongodb_source.php
-
logQuery method
-
-
- -
-
m
-
top
-
-
-
-
- mongodb_source.php -
-
-
mongodb_source.php in mongodb_source.php
-
-
- mapReduce -
-
-
MongodbSource::mapReduce() in mongodb_source.php
-
mapReduce
-
-
- MongoDbDateFormatter -
-
-
MongoDbDateFormatter() in mongodb_source.php
-
MongoDbDateFormatter method
-
-
- MongodbSource -
-
-
MongodbSource in mongodb_source.php
-
MongoDB Source
-
-
- -
-
n
-
top
-
-
-
-
- $name -
-
-
SqlCompatibleBehavior::$name in sql_compatible.php
-
name property
-
-
- $name -
-
-
SchemalessBehavior::$name in schemaless.php
-
name property
-
-
- name -
-
-
MongodbSource::name() in mongodb_source.php
-
Quotes identifiers.
-
-
- -
-
q
-
top
-
-
-
-
- query -
-
-
MongodbSource::query() in mongodb_source.php
-
query method If call getMongoDb() from model, this method call getMongoDb().
-
-
- -
-
r
-
top
-
-
-
-
- read -
-
-
MongodbSource::read() in mongodb_source.php
-
Read Data
-
-
- rollback -
-
-
MongodbSource::rollback() in mongodb_source.php
-
rollback method
-
-
- -
-
s
-
top
-
-
-
-
- $settings -
-
-
SqlCompatibleBehavior::$settings in sql_compatible.php
-
Runtime settings
-
-
- $settings -
-
- -
settings property
-
-
- schemaless.php -
-
-
schemaless.php in schemaless.php
-
-
- sql_compatible.php -
-
-
sql_compatible.php in sql_compatible.php
-
-
- SchemalessBehavior -
-
-
SchemalessBehavior in schemaless.php
-
SchemalessBehavior class
-
-
- setup -
-
-
SqlCompatibleBehavior::setup() in sql_compatible.php
-
setup method
-
-
- setup -
-
-
SchemalessBehavior::setup() in schemaless.php
-
setup method
-
-
- SqlCompatibleBehavior -
-
-
SqlCompatibleBehavior in sql_compatible.php
-
SqlCompatibleBehavior class
-
-
- setTimeout -
-
-
MongodbSource::setTimeout() in mongodb_source.php
-
setTimeout Method
-
-
- -
-
t
-
top
-
-
-
-
- truncate -
-
-
MongodbSource::truncate() in mongodb_source.php
-
Deletes all the records in a table
-
-
- -
-
u
-
top
-
-
-
-
- update -
-
-
MongodbSource::update() in mongodb_source.php
-
Update Data
-
-
- updateAll -
-
-
MongodbSource::updateAll() in mongodb_source.php
-
Update multiple Record
-
-
- -
-
v
-
top
-
-
-
-
- value -
-
-
MongodbSource::value() in mongodb_source.php
-
Prepares a value, or an array of values for database queries by quoting and escaping them.
-
-
- -
- a - b - c - d - e - g - i - l - m - n - q - r - s - t - u - v - _ -
- \ No newline at end of file diff --git a/docs/errors.html b/docs/errors.html deleted file mode 100644 index 0459e1c..0000000 --- a/docs/errors.html +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - phpDocumentor Parser Errors and Warnings - - - - - Post-parsing
- -

Post-parsing

-

Warnings:


-Warning - Class SchemalessBehavior parent ModelBehavior not found
-Warning - Class SqlCompatibleBehavior parent ModelBehavior not found
-Warning - Class MongodbSource parent DboSource not found
-

- Documentation generated on Tue, 26 Jul 2011 01:09:02 +0900 by phpDocumentor 1.4.3 -

- - \ No newline at end of file diff --git a/docs/index.html b/docs/index.html deleted file mode 100644 index aafb23d..0000000 --- a/docs/index.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - Generated Documentation - - - - - - - - - - - <H2>Frame Alert</H2> - <P>This document is designed to be viewed using the frames feature. - If you see this message, you are using a non-frame-capable web client.</P> - - - \ No newline at end of file diff --git a/docs/li_mongodb.html b/docs/li_mongodb.html deleted file mode 100644 index d770d8b..0000000 --- a/docs/li_mongodb.html +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - -
mongodb
-
- -
- -
Description
-
- Class trees
- Index of elements
-
- - - - - - - -
mongodb-models-behaviors
-
-
-
Classes
-
SchemalessBehavior
-
SqlCompatibleBehavior
-
Files
-
schemaless.php
-
sql_compatible.php
-
-
- - - - -
mongodb-models-datasources
-
-
-
Classes
-
MongodbSource
-
Functions
-
MongoDbDateFormatter
-
Files
-
mongodb_source.php
-
-
- - -
-
-

phpDocumentor v 1.4.3

- - \ No newline at end of file diff --git a/docs/media/banner.css b/docs/media/banner.css deleted file mode 100644 index ba1a7ba..0000000 --- a/docs/media/banner.css +++ /dev/null @@ -1,32 +0,0 @@ -body -{ - background-color: #DDDDDD; - margin: 0px; - padding: 0px; -} - -/* Banner (top bar) classes */ - -.banner { } - -.banner-menu -{ - clear: both; - padding: .5em; - border-top: 2px solid #999999; -} - -.banner-title -{ - text-align: right; - font-size: 20pt; - font-weight: bold; - margin: .2em; -} - -.package-selector -{ - background-color: #CCCCCC; - border: 1px solid black; - color: blue; -} diff --git a/docs/media/stylesheet.css b/docs/media/stylesheet.css deleted file mode 100644 index 051586b..0000000 --- a/docs/media/stylesheet.css +++ /dev/null @@ -1,142 +0,0 @@ -a { color: #0000FF; text-decoration: none; } -a:hover { color: #FF0000; text-decoration: underline; } -a:active { color: #FF0000; text-decoration: underline; } - -body { background-color: #EEEEEE; font-family: Verdana, Arial, sans-serif } -body, table { font-size: 10pt } -a img { border: 0px; } -dd { margin-left: 0px; padding-left: 1em; } - -/* Page layout/boxes */ - -.info-box {} -.info-box-title { margin: 1em 0em 0em 0em; padding: .25em; font-weight: normal; font-size: 14pt; border: 2px solid #999999; background-color: #DDDDDD } -.info-box-body { border: 1px solid #999999; padding: .5em; background-color: #F8F8F8; } -.nav-bar { font-size: 8pt; white-space: nowrap; text-align: right; padding: .2em; margin: 0em 0em 1em 0em; } - -.oddrow { background-color: #DDDDDD; border: 1px solid #999999; padding: .5em; margin-bottom: 1em} -.evenrow { background-color: #DDDDDD; border: 1px solid #999999; padding: .5em; margin-bottom: 1em} - -.page-body { max-width: 800px; margin: auto; } -.tree dl { margin: 0px } - -/* Index formatting classes */ - -.index-item-body { margin-top: .5em; margin-bottom: .5em} -.index-item-description { margin-top: .25em } -.index-item-details { font-weight: normal; font-style: italic; font-size: 8pt } -.index-letter-section { background-color: #EEEEEE; border: 1px dotted #999999; padding: .5em; margin-bottom: 1em} -.index-letter-title { font-size: 12pt; font-weight: bold } -.index-letter-menu { text-align: center; margin: 1em } -.index-letter { font-size: 12pt } - -/* Docbook classes */ - -.description {} -.short-description { font-weight: bold; color: #666666; } -.tags { padding-left: 0em; margin-left: 3em; color: #666666; list-style-type: square; } -.parameters { padding-left: 0em; margin-left: 3em; font-style: italic; list-style-type: square; } -.redefinitions { font-size: 8pt; padding-left: 0em; margin-left: 2em; } -.package { } -.package-title { font-weight: bold; font-size: 14pt; border-bottom: 1px solid black } -.package-details { font-size: 85%; } -.sub-package { font-weight: bold; font-size: 120% } -.tutorial { border-width: thin; border-color: #0066ff } -.tutorial-nav-box { width: 100%; border: 2px solid #999999; background-color: #DDDDDD } -.nav-button-disabled { color: #999999; } -.nav-button:active, -.nav-button:focus, -.nav-button:hover { background-color: #AAAAAA; outline: 1px solid #666666; text-decoration: none } -.folder-title { font-style: italic } - -/* Generic formatting */ - -.field { font-weight: bold; } -.detail { font-size: 8pt; } -.notes { font-style: italic; font-size: 8pt; } -.separator { background-color: #999999; height: 2px; } -.warning { color: #FF6600; } -.disabled { font-style: italic; color: #999999; } - -/* Code elements */ - -.line-number { } - -.class-table { width: 100%; } -.class-table-header { border-bottom: 1px dotted #666666; text-align: left} -.class-name { color: #000000; font-weight: bold; } - -.method-summary { padding-left: 1em; font-size: 8pt } -.method-header { } -.method-definition { margin-bottom: .3em } -.method-title { font-weight: bold; } -.method-name { font-weight: bold; } -.method-signature { font-size: 85%; color: #0066BB; margin: .5em 0em } -.method-result { font-style: italic; } - -.var-summary { padding-left: 1em; font-size: 8pt; } -.var-header { } -.var-title { margin-bottom: .3em } -.var-type { color: red; font-weight: bold } -.var-name { font-weight: bold; } -.var-default {} -.var-description { font-weight: normal; color: #000000; } - -.include-title { } -.include-type { font-style: italic; } -.include-name { font-weight: bold; } - -.const-title { } -.const-name { font-weight: bold; } - -/* Syntax highlighting */ - -.src-code { border: 1px solid #336699; padding: 1em; background-color: #EEEEEE; - font-family: 'Courier New', Courier, monospace; font-weight: normal; } -.src-line { font-family: 'Courier New', Courier, monospace; font-weight: normal; } - -.src-comm { color: #666666; } -.src-id { } -.src-inc { color: #0000FF; } -.src-key { color: #0000FF; } -.src-num { color: #CC0000; } -.src-str { color: #66cccc; } -.src-sym { font-weight: bold; } -.src-var { } - -.src-php { font-weight: bold; } - -.src-doc { color: #009999 } -.src-doc-close-template { color: #0000FF } -.src-doc-coretag { color: #0099FF; font-weight: bold } -.src-doc-inlinetag { color: #0099FF } -.src-doc-internal { color: #6699cc } -.src-doc-tag { color: #0080CC } -.src-doc-template { color: #0000FF } -.src-doc-type { font-style: italic } -.src-doc-var { font-style: italic } - -.tute-tag { color: #009999 } -.tute-attribute-name { color: #0000FF } -.tute-attribute-value { color: #0099FF } -.tute-entity { font-weight: bold; } -.tute-comment { font-style: italic } -.tute-inline-tag { color: #636311; font-weight: bold } - -/* tutorial */ - -.authors { } -.author { font-style: italic; font-weight: bold } -.author-blurb { margin: .5em 0em .5em 2em; font-size: 85%; font-weight: normal; font-style: normal } -.example { background-color: #DDDDDD; border: 1px solid #999999; padding: .5em; } -.listing { background-color: #DDDDDD; border: 1px solid #999999; padding: .5em; white-space: nowrap; } -.release-info { font-size: 85%; font-style: italic; margin: 1em 0em } -.ref-title-box { } -.ref-title { } -.ref-purpose { font-style: italic; color: #666666 } -.ref-synopsis { } -.title { font-weight: bold; border-bottom: 1px solid #888888; color: #888888; } -.cmd-synopsis { margin: 1em 0em } -.cmd-title { font-weight: bold } -.toc { margin-left: 2em; padding-left: 0em } - diff --git a/docs/mongodb/mongodb-models-behaviors/SchemalessBehavior.html b/docs/mongodb/mongodb-models-behaviors/SchemalessBehavior.html deleted file mode 100644 index 1e2e88c..0000000 --- a/docs/mongodb/mongodb-models-behaviors/SchemalessBehavior.html +++ /dev/null @@ -1,273 +0,0 @@ - - - - - - Docs For Class SchemalessBehavior - - - - -
-

Class SchemalessBehavior

- - -
-
Description
- -
- -

SchemalessBehavior class

-
    -
  • uses: ModelBehavior
  • -
-

- Located in /behaviors/schemaless.php (line 34) -

- - -
ModelBehavior
-   |
-   --SchemalessBehavior
- -
-
- - - - -
-
Variable Summary
- -
-
-
- string - $name -
-
- array - $settings -
-
- array - $_defaultSettings -
-
-
-
- - -
-
Method Summary
- -
-
- -
- void - beforeSave - ( &$Model, mixed $Model) -
- -
- void - setup - ( &$Model, [array $config = array()], mixed $Model) -
-
-
-
- - -
-
Variables
- -
- - -
- -
- - string - $name - = 'Schemaless' (line 42) - -
- - -

name property

-
    -
  • var: 'Schemaless'
  • -
  • access: public
  • -
- - - - - -
- -
- -
- - array - $settings - = array() (line 50) - -
- - -

settings property

-
    -
  • access: public
  • -
- - - - - -
- -
- -
- - array - $_defaultSettings - = array(
)
(line 58) -
-
- - -

defaultSettings property

-
    -
  • access: protected
  • -
- - - - - -
- -
-
- - -
-
Methods
- -
- - -
- -
- beforeSave (line 84) -
- - -

beforeSave method

-

Set the schema to allow saving whatever has been passed

-
    -
  • access: public
  • -
- -
- void - - beforeSave - - ( &$Model, mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - - &$Model
  • -
- - -
- -
- -
- setup (line 71) -
- - -

setup method

-

Don't currently have any settings at all - disabled

-
    -
  • access: public
  • -
- -
- void - - setup - - ( &$Model, [array $config = array()], mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - array - $config: array()
  • -
  • - - &$Model
  • -
- - -
- -
-
- - -

- Documentation generated on Tue, 26 Jul 2011 01:09:01 +0900 by phpDocumentor 1.4.3 -

-
- \ No newline at end of file diff --git a/docs/mongodb/mongodb-models-behaviors/SqlCompatibleBehavior.html b/docs/mongodb/mongodb-models-behaviors/SqlCompatibleBehavior.html deleted file mode 100644 index 18d8a10..0000000 --- a/docs/mongodb/mongodb-models-behaviors/SqlCompatibleBehavior.html +++ /dev/null @@ -1,443 +0,0 @@ - - - - - - Docs For Class SqlCompatibleBehavior - - - - -
-

Class SqlCompatibleBehavior

- - -
-
Description
- -
- -

SqlCompatibleBehavior class

-
    -
  • uses: ModelBehavior
  • -
-

- Located in /behaviors/sql_compatible.php (line 31) -

- - -
ModelBehavior
-   |
-   --SqlCompatibleBehavior
- -
-
- - - - -
-
Variable Summary
- -
-
-
- string - $name -
-
- array - $settings -
-
- array - $_defaultSettings -
-
-
-
- - -
-
Method Summary
- -
-
- -
- void - afterFind - ( &$Model, mixed $results, mixed $primary, mixed $Model) -
- -
- void - beforeFind - ( &$Model, mixed $query, mixed $Model) -
- -
- void - convertDates - ( &$results, mixed $results) -
- -
- void - setup - ( &$Model, [array $config = array()], mixed $Model) -
- -
- void - _translateConditions - ( &$Model,  &$conditions, mixed $Model, mixed $conditions) -
- -
- string - _translateOperator - (mixed $Model, mixed $operator) -
-
-
-
- - -
-
Variables
- -
- - -
- -
- - string - $name - = 'SqlCompatible' (line 39) - -
- - -

name property

-
    -
  • var: 'SqlCompatible'
  • -
  • access: public
  • -
- - - - - -
- -
- -
- - array - $settings - = array() (line 49) - -
- - -

Runtime settings

-

Keyed on model alias

-
    -
  • access: public
  • -
- - - - - -
- -
- -
- - array - $_defaultSettings - = array(
'convertDates' => true,
'operators' => array(
'!=' => '$ne',
'>' => '$gt',
'>=' => '$gte',
'<' => '$lt',
'<=' => '$lte',
'IN' => '$in',
'NOT' => '$not',
'NOT IN' => '$nin'
))
(line 57) -
-
- - -

defaultSettings property

-
    -
  • access: protected
  • -
- - - - - -
- -
-
- - -
-
Methods
- -
- - -
- -
- afterFind (line 94) -
- - -

If requested, convert dates from MongoDate objects to standard date strings

-
    -
  • access: public
  • -
- -
- void - - afterFind - - ( &$Model, mixed $results, mixed $primary, mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - mixed - $results
  • -
  • - mixed - $primary
  • -
  • - - &$Model
  • -
- - -
- -
- -
- beforeFind (line 111) -
- - -

beforeFind method

-

If conditions are an array ensure they are mongified

-
    -
  • access: public
  • -
- -
- void - - beforeFind - - ( &$Model, mixed $query, mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - mixed - $query
  • -
  • - - &$Model
  • -
- - -
- -
- -
- convertDates (line 125) -
- - -

Convert MongoDate objects to strings for the purpose of view simplicity

-
    -
  • access: public
  • -
- -
- void - - convertDates - - ( &$results, mixed $results) -
- -
    -
  • - mixed - $results
  • -
  • - - &$results
  • -
- - -
- -
- -
- setup (line 81) -
- - -

setup method

-

Allow overriding the operator map

-
    -
  • access: public
  • -
- -
- void - - setup - - ( &$Model, [array $config = array()], mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - array - $config: array()
  • -
  • - - &$Model
  • -
- - -
- -
- -
- _translateConditions (line 145) -
- - -

translateConditions method

-

Loop on conditions and desqlify them

-
    -
  • access: protected
  • -
- -
- void - - _translateConditions - - ( &$Model,  &$conditions, mixed $Model, mixed $conditions) -
- -
    -
  • - mixed - $Model
  • -
  • - mixed - $conditions
  • -
  • - - &$Model
  • -
  • - - &$conditions
  • -
- - -
- -
- -
- _translateOperator (line 264) -
- - -

translateOperator method

-

Use the operator map for the model and return what the db really wants to hear

-
    -
  • access: protected
  • -
- -
- string - - _translateOperator - - (mixed $Model, mixed $operator) -
- -
    -
  • - mixed - $Model
  • -
  • - mixed - $operator
  • -
- - -
- -
-
- - -

- Documentation generated on Tue, 26 Jul 2011 01:09:02 +0900 by phpDocumentor 1.4.3 -

-
- \ No newline at end of file diff --git a/docs/mongodb/mongodb-models-behaviors/_behaviors---schemaless.php.html b/docs/mongodb/mongodb-models-behaviors/_behaviors---schemaless.php.html deleted file mode 100644 index 0cea78d..0000000 --- a/docs/mongodb/mongodb-models-behaviors/_behaviors---schemaless.php.html +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - Docs for page schemaless.php - - - - -
-

/behaviors/schemaless.php

- - -
-
Description
- -
- -

Schemaless behavior.

-

Adds functionality specific to MongoDB/schemaless dbs Allow /not/ specifying the model's schema, and derive it (for cake-compatibility) from the data being saved. Note that used carelessly this is a pretty dangerous thing to allow - means a user can modify input forms adding whatever fields they like (unless you'er using the security component) and fill your db with their junk.

PHP version 5

Copyright (c) 2010, Andy Dawson

Licensed under The MIT License Redistributions of files must retain the above copyright notice.

- - -
-
- - -
-
Classes
- -
- - - - - - - - - -
ClassDescription
- SchemalessBehavior - - SchemalessBehavior class -
-
-
- - - - - -

- Documentation generated on Tue, 26 Jul 2011 01:09:01 +0900 by phpDocumentor 1.4.3 -

-
- \ No newline at end of file diff --git a/docs/mongodb/mongodb-models-behaviors/_behaviors---sql_compatible.php.html b/docs/mongodb/mongodb-models-behaviors/_behaviors---sql_compatible.php.html deleted file mode 100644 index 320f314..0000000 --- a/docs/mongodb/mongodb-models-behaviors/_behaviors---sql_compatible.php.html +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - Docs for page sql_compatible.php - - - - -
-

/behaviors/sql_compatible.php

- - -
-
Description
- -
- -

Sql Compatible.

-

Attach this behavior to be able to query mongo DBs without using mongo specific syntax. If you don't need this behavior don't attach it and save a few cycles

PHP version 5

Copyright (c) 2010, Andy Dawson

Licensed under The MIT License Redistributions of files must retain the above copyright notice.

- - -
-
- - -
-
Classes
- -
- - - - - - - - - -
ClassDescription
- SqlCompatibleBehavior - - SqlCompatibleBehavior class -
-
-
- - - - - -

- Documentation generated on Tue, 26 Jul 2011 01:09:02 +0900 by phpDocumentor 1.4.3 -

-
- \ No newline at end of file diff --git a/docs/mongodb/mongodb-models-datasources/MongodbSource.html b/docs/mongodb/mongodb-models-datasources/MongodbSource.html deleted file mode 100644 index 832ecf0..0000000 --- a/docs/mongodb/mongodb-models-datasources/MongodbSource.html +++ /dev/null @@ -1,1875 +0,0 @@ - - - - - - Docs For Class MongodbSource - - - - -
-

Class MongodbSource

- - -
-
Description
- -
- -

MongoDB Source

-

- Located in /datasources/mongodb_source.php (line 35) -

- - -
DboSource
-   |
-   --MongodbSource
- -
-
- - - - -
-
Variable Summary
- -
-
-
- array - $columns -
-
- boolean - $connected -
-
- array - $_baseConfig -
-
- resource - $_db -
-
- array - $_defaultSchema -
-
- string - $_driverVersion -
-
- mixed - $_startTime -
-
-
-
- - -
-
Method Summary
- -
-
- -
- void - __construct - ([array $config = array()], [bool $autoConnect = false]) -
- -
- void - __destruct - () -
- -
- void - begin - () -
- -
- array - calculate - ( &$Model, Model $Model) -
- -
- boolean - close - () -
- -
- void - commit - () -
- -
- boolean - connect - () -
- -
- boolean - create - ( &$Model, [array $fields = null], [array $values = null], Model $Model) -
- -
- void - createConnectionName - (array $config, string $version) -
- -
- void - createSchema - (mixed $schema, [mixed $tableName = null]) -
- -
- boolean - delete - ( &$Model, [array $conditions = null], Model $Model) -
- -
- void - deriveSchemaFromData - (mixed $Model, [array $data = array()]) -
- -
- array - describe - ( &$Model, [ $field = null], Model $Model) -
- -
- boolean - disconnect - () -
- -
- void - distinct - ( &$Model, [array $keys = array()], [array $params = array()], mixed $Model) -
- -
- void - dropSchema - (mixed $schema, [mixed $tableName = null]) -
- -
- void - ensureIndex - ( &$Model, [array $keys = array()], [array $params = array()], mixed $Model) -
- -
- void - execute - (mixed $query, [array $params = array()]) -
- -
- mixed - getMongoCollection - ( &$Model) -
- -
- mixed - getMongoDb - () -
- -
- void - group - ( &$Model, [array $params = array()], mixed $Model) -
- -
- void - insertMulti - (string $table, string $fields, array $values) -
- -
- boolean - isConnected - () -
- -
- void - isInterfaceSupported - (mixed $interface) -
- -
- array - listSources - ([array $data = null]) -
- -
- void - logQuery - (mixed $query, [array $args = array()]) -
- -
- mixed - mapReduce - (mixed $query, [integer $timeout = null]) -
- -
- string - name - (string $name) -
- -
- void - query - (mixed $query, [array $params = array()]) -
- -
- array - read - ( &$Model, [array $query = array()], Model $Model) -
- -
- void - rollback - () -
- -
- boolean - setTimeout - (int $ms) -
- -
- boolean - truncate - (mixed $table) -
- -
- boolean - update - ( &$Model, [array $fields = null], [array $values = null], [ $conditions = null], Model $Model) -
- -
- boolean - updateAll - ( &$Model, [array $fields = null], [array $conditions = null], Model $Model) -
- -
- mixed - value - (mixed $data, [string $column = null], [boolean $read = true]) -
- -
- void - _convertId - ( &$mixed, [bool $conditions = false], mixed $mixed) -
- -
- void - _prepareLogQuery - ( &$Model, mixed $Model) -
- -
- void - _setEmptyValues - ( &$data, [array $integers = array('limit', 'offset')], mixed $data) -
- -
- array - _stringify - ([ &$args = array()], [int $level = 0], array $args) -
- -
- void - _stripAlias - ([ &$args = array()], [string $alias = 'Model'], [bool $recurse = true], [string $check = 'key'], array $args) -
-
-
-
- - -
-
Variables
- -
- - -
- -
- - array - $columns - = array(
'boolean' => array('name' => 'boolean'),'string'=>array('name'=>'varchar'),'text'=>array('name'=>'text'),'integer'=>array('name'=>'integer','format'=>null,'formatter'=>'intval'),'float'=>array('name'=>'float','format'=>null,'formatter'=>'floatval'),'datetime'=>array('name'=>'datetime','format'=>null,'formatter'=>'MongodbDateFormatter'),'timestamp'=>array('name'=>'timestamp','format'=>null,'formatter'=>'MongodbDateFormatter'),'time'=>array('name'=>'time','format'=>null,'formatter'=>'MongodbDateFormatter'),'date'=>array('name'=>'date','format'=>null,'formatter'=>'MongodbDateFormatter'),)
(line 101) -
-
- - -

column definition

-
    -
  • access: public
  • -
- - - - - -
- -
- -
- - boolean - $connected - = null (line 47) - -
- - -

Are we connected to the DataSource?

-

true - yes null - haven't tried yet false - nope, and we can't connect

-
    -
  • access: public
  • -
- - - - - -
- -
- -
- - array - $_baseConfig - = array(
'set_string_id' => true,
'persistent' => true,
'host' => 'localhost',
'database' => '',
'port' => '27017',
'login' => '',
'password' => ''
)
(line 86) -
-
- - -

Base Config

-

set_string_id: true: In read() method, convert MongoId object to string and set it to array 'id'. false: not convert and set.

-
    -
  • access: public
  • -
- - - - - -
- -
- -
- - resource - $_db - = null (line 55) - -
- - -

Database Instance

-
    -
  • access: protected
  • -
- - - - - -
- -
- -
- - array - $_defaultSchema - = array(
'_id' => array('type' => 'string', 'length' => 24, 'key' => 'primary'),'created'=>array('type'=>'datetime','default'=>null))
(line 119) -
-
- - -

Default schema for the mongo models

-
    -
  • access: protected
  • -
- - - - - -
- -
- -
- - string - $_driverVersion - = Mongo::VERSION (line 63) - -
- - -

Mongo Driver Version

-
    -
  • access: protected
  • -
- - - - - -
- -
- -
- - mixed - $_startTime - = null (line 73) - -
- - -

startTime property

-

If debugging is enabled, stores the (micro)time the current query started

-
    -
  • var: null
  • -
  • access: protected
  • -
- - - - - -
- -
-
- - -
-
Methods
- -
- - -
- -
- Constructor __construct (line 134) -
- - -

construct method

-

By default don't try to connect until you need to

-
    -
  • access: public
  • -
- -
- void - - __construct - - ([array $config = array()], [bool $autoConnect = false]) -
- -
    -
  • - array - $config: Configuration array
  • -
  • - bool - $autoConnect: false
  • -
- - -
- -
- -
- Destructor __destruct (line 143) -
- - -

Destruct

-
    -
  • access: public
  • -
- -
- void - - __destruct - - () -
- - - -
- -
- -
- begin (line 411) -
- - -

begin method

-

Mongo doesn't support transactions

-
    -
  • access: public
  • -
- -
- void - - begin - - () -
- - - -
- -
- -
- calculate (line 422) -
- - -

Calculate

-
    -
  • access: public
  • -
- -
- array - - calculate - - ( &$Model, Model $Model) -
- -
    -
  • - Model - $Model
  • -
  • - - &$Model
  • -
- - -
- -
- -
- close (line 332) -
- - -

Close database connection

-
    -
  • return: Connected
  • -
  • access: public
  • -
- -
- boolean - - close - - () -
- - - -
- -
- -
- commit (line 157) -
- - -

commit method

-

MongoDB doesn't support transactions

-
    -
  • access: public
  • -
- -
- void - - commit - - () -
- - - -
- -
- -
- connect (line 172) -
- - -

Connect to the database

-

If using 1.0.2 or above use the mongodb:// format to connect The connect syntax changed in version 1.0.2 - so check for that too

If authentication information in present then authenticate the connection

-
    -
  • return: Connected
  • -
  • access: public
  • -
- -
- boolean - - connect - - () -
- - - -
- -
- -
- create (line 447) -
- - -

Create Data

-
    -
  • return: Insert result
  • -
  • access: public
  • -
- -
- boolean - - create - - ( &$Model, [array $fields = null], [array $values = null], Model $Model) -
- -
    -
  • - Model - $Model: Model Instance
  • -
  • - array - $fields: Field data
  • -
  • - array - $values: Save data
  • -
  • - - &$Model
  • -
- - -
- -
- -
- createConnectionName (line 210) -
- - -

create connection name.

-
    -
  • access: public
  • -
- -
- void - - createConnectionName - - (array $config, string $version) -
- -
    -
  • - array - $config
  • -
  • - string - $version: version of MongoDriver
  • -
- - -
- -
- -
- createSchema (line 497) -
- - -

createSchema method

-

Mongo no care for creating schema. Mongo work with no schema.

-
    -
  • access: public
  • -
- -
- void - - createSchema - - (mixed $schema, [mixed $tableName = null]) -
- -
    -
  • - mixed - $schema
  • -
  • - mixed - $tableName: null
  • -
- - -
- -
- -
- delete (line 851) -
- - -

Delete Data

-

For deleteAll(true, false) calls - conditions will arrive here as true - account for that and convert to an empty array For deleteAll(array('some conditions')) calls - conditions will arrive here as: array( Alias._id => array(1, 2, 3, ...) )

This format won't be understood by mongodb, it'll find 0 rows. convert to:

array( Alias._id => array('$in' => array(1, 2, 3, ...)) )

-
    -
  • return: Update result
  • -
  • TODO: bench remove() v drop. if it's faster to drop - just drop the collection taking into account existing indexes (recreate just the indexes)
  • -
  • access: public
  • -
- -
- boolean - - delete - - ( &$Model, [array $conditions = null], Model $Model) -
- -
    -
  • - Model - $Model: Model Instance
  • -
  • - array - $conditions
  • -
  • - - &$Model
  • -
- - -
- -
- -
- deriveSchemaFromData (line 804) -
- - -

deriveSchemaFromData method

-
    -
  • access: public
  • -
- -
- void - - deriveSchemaFromData - - (mixed $Model, [array $data = array()]) -
- -
    -
  • - mixed - $Model
  • -
  • - array - $data: array()
  • -
- - -
- -
- -
- describe (line 386) -
- - -

Describe

-

Automatically bind the schemaless behavior if there is no explicit mongo schema. When called, if there is model data it will be used to derive a schema. a row is plucked out of the db and the data obtained used to derive the schema.

-
    -
  • return: if model instance has mongoSchema, return it.
  • -
  • access: public
  • -
- -
- array - - describe - - ( &$Model, [ $field = null], Model $Model) -
- -
    -
  • - Model - $Model
  • -
  • - - &$Model
  • -
  • - - $field
  • -
- - -
- -
- -
- disconnect (line 342) -
- - -

Disconnect from the database

-
    -
  • return: Connected
  • -
  • access: public
  • -
- -
- boolean - - disconnect - - () -
- - - -
- -
- -
- distinct (line 550) -
- - -

distinct method

-
    -
  • access: public
  • -
- -
- void - - distinct - - ( &$Model, [array $keys = array()], [array $params = array()], mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - array - $keys: array()
  • -
  • - array - $params: array()
  • -
  • - - &$Model
  • -
- - -
- -
- -
- dropSchema (line 511) -
- - -

dropSchema method

-

Return a command to drop each table

-
    -
  • access: public
  • -
- -
- void - - dropSchema - - (mixed $schema, [mixed $tableName = null]) -
- -
    -
  • - mixed - $schema
  • -
  • - mixed - $tableName: null
  • -
- - -
- -
- -
- ensureIndex (line 635) -
- - -

ensureIndex method

-
    -
  • access: public
  • -
- -
- void - - ensureIndex - - ( &$Model, [array $keys = array()], [array $params = array()], mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - array - $keys: array()
  • -
  • - array - $params: array()
  • -
  • - - &$Model
  • -
- - -
- -
- -
- execute (line 1142) -
- - -

execute method

-

If there is no query or the query is true, execute has probably been called as part of a db-agnostic process which does not have a mongo equivalent, don't do anything.

-
    -
  • access: public
  • -
- -
- void - - execute - - (mixed $query, [array $params = array()]) -
- -
    -
  • - mixed - $query
  • -
  • - array - $params: array()
  • -
- - -
- -
- -
- getMongoCollection (line 297) -
- - -

get MongoDB Collection Object

-
    -
  • return: MongoDB Collection Object
  • -
  • access: public
  • -
- -
- mixed - - getMongoCollection - - ( &$Model) -
- -
    -
  • - - &$Model
  • -
- - -
- -
- -
- getMongoDb (line 284) -
- - -

get MongoDB Object

-
    -
  • return: MongoDB Object
  • -
  • access: public
  • -
- -
- mixed - - getMongoDb - - () -
- - - -
- -
- -
- group (line 596) -
- - -

group method

-
    -
  • access: public
  • -
- -
- void - - group - - ( &$Model, [array $params = array()], mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - array - $params:

    array() Set params same as MongoCollection::group() key,initial, reduce, options(conditions, finalize)

    Ex. $params = array( 'key' => array('field' => true), 'initial' => array('csum' => 0), 'reduce' => 'function(obj, prev){prev.csum += 1;}', 'options' => array( 'condition' => array('age' => array('$gt' => 20)), 'finalize' => array(), ), );

  • -
  • - - &$Model
  • -
- - -
- -
- -
- insertMulti (line 238) -
- - -

Inserts multiple values into a table

-
    -
  • access: public
  • -
- -
- void - - insertMulti - - (string $table, string $fields, array $values) -
- -
    -
  • - string - $table
  • -
  • - string - $fields
  • -
  • - array - $values
  • -
- - -
- -
- -
- isConnected (line 271) -
- - -

check connection to the database

-
    -
  • return: Connected
  • -
  • access: public
  • -
- -
- boolean - - isConnected - - () -
- - - -
- -
- -
- isInterfaceSupported (line 319) -
- - -

isInterfaceSupported method

-

listSources is infact supported, however: cake expects it to return a complete list of all possible sources in the selected db - the possible list of collections is infinte, so it's faster and simpler to tell cake that the interface is /not/ supported so it assumes that <insert name of your table here> exist

-
    -
  • access: public
  • -
- -
- void - - isInterfaceSupported - - (mixed $interface) -
- -
    -
  • - mixed - $interface
  • -
- - -
- -
- -
- listSources (line 358) -
- - -

Get list of available Collections

-
    -
  • return: Collections
  • -
  • access: public
  • -
- -
- array - - listSources - - ([array $data = null]) -
- -
    -
  • - array - $data
  • -
- - -
- -
- -
- logQuery (line 1241) -
- - -

logQuery method

-

Set timers, errors and refer to the parent If there are arguments passed - inject them into the query Show MongoIds in a copy-and-paste-into-mongo format

-
    -
  • access: public
  • -
- -
- void - - logQuery - - (mixed $query, [array $args = array()]) -
- -
    -
  • - mixed - $query
  • -
  • - array - $args: array()
  • -
- - -
- -
- -
- mapReduce (line 1094) -
- - -

mapReduce

-
    -
  • return: false or array
  • -
  • access: public
  • -
- -
- mixed - - mapReduce - - (mixed $query, [integer $timeout = null]) -
- -
    -
  • - mixed - $query
  • -
  • - integer - $timeout: (milli second)
  • -
- - -
- -
- -
- name (line 434) -
- - -

Quotes identifiers.

-

MongoDb does not need identifiers quoted, so this method simply returns the identifier.

-
    -
  • return: The quoted identifier.
  • -
  • access: public
  • -
- -
- string - - name - - (string $name) -
- -
    -
  • - string - $name: The identifier to quote.
  • -
- - -
- -
- -
- query (line 1067) -
- - -

query method If call getMongoDb() from model, this method call getMongoDb().

-
    -
  • access: public
  • -
- -
- void - - query - - (mixed $query, [array $params = array()]) -
- -
    -
  • - mixed - $query
  • -
  • - array - $params: array()
  • -
- - -
- -
- -
- read (line 908) -
- - -

Read Data

-

For deleteAll(true) calls - the conditions will arrive here as true - account for that and switch to an empty array

-
    -
  • return: Results
  • -
  • access: public
  • -
- -
- array - - read - - ( &$Model, [array $query = array()], Model $Model) -
- -
    -
  • - Model - $Model: Model Instance
  • -
  • - array - $query: Query data
  • -
  • - - &$Model
  • -
- - -
- -
- -
- rollback (line 1039) -
- - -

rollback method

-

MongoDB doesn't support transactions

-
    -
  • access: public
  • -
- -
- void - - rollback - - () -
- - - -
- -
- -
- setTimeout (line 1222) -
- - -

setTimeout Method

-

Sets the MongoCursor timeout so long queries (like map / reduce) can run at will. Expressed in milliseconds, for an infinite timeout, set to -1

-
    -
  • access: public
  • -
- -
- boolean - - setTimeout - - (int $ms) -
- -
    -
  • - int - $ms
  • -
- - -
- -
- -
- truncate (line 1050) -
- - -

Deletes all the records in a table

-
    -
  • access: public
  • -
- -
- boolean - - truncate - - (mixed $table) -
- -
    -
  • - mixed - $table: A string or model class representing the table to be truncated
  • -
- - -
- -
- -
- update (line 677) -
- - -

Update Data

-

This method uses $set operator automatically with MongoCollection::update(). If you don't want to use $set operator, you can chose any one as follw.

  1. Set TRUE in Model::mongoNoSetOperator property.
  2. Set a mongodb operator in a key of save data as follow. - Model->save(array('_id' => $id, '$inc' => array('count' => 1))); - Don't use Model::mongoSchema property, - CakePHP delete '$inc' data in Model::Save().
  3. Set a Mongo operator in Model::mongoNoSetOperator property. - Model->mongoNoSetOperator = '$inc'; - Model->save(array('_id' => $id, array('count' => 1)));

-
    -
  • return: Update result
  • -
  • access: public
  • -
- -
- boolean - - update - - ( &$Model, [array $fields = null], [array $values = null], [ $conditions = null], Model $Model) -
- -
    -
  • - Model - $Model: Model Instance
  • -
  • - array - $fields: Field data
  • -
  • - array - $values: Save data
  • -
  • - - &$Model
  • -
  • - - $conditions
  • -
- - -
- -
- -
- updateAll (line 768) -
- - -

Update multiple Record

-
    -
  • return: Update result
  • -
  • access: public
  • -
- -
- boolean - - updateAll - - ( &$Model, [array $fields = null], [array $conditions = null], Model $Model) -
- -
    -
  • - Model - $Model: Model Instance
  • -
  • - array - $fields: Field data
  • -
  • - array - $conditions
  • -
  • - - &$Model
  • -
- - -
- -
- -
- value (line 1123) -
- - -

Prepares a value, or an array of values for database queries by quoting and escaping them.

-
    -
  • return: Prepared value or array of values.
  • -
  • access: public
  • -
- -
- mixed - - value - - (mixed $data, [string $column = null], [boolean $read = true]) -
- -
    -
  • - mixed - $data: A value or an array of values to prepare.
  • -
  • - string - $column: The column into which this data will be inserted
  • -
  • - boolean - $read: Value to be used in READ or WRITE context
  • -
- - -
- -
- -
- _convertId (line 1271) -
- - -

convertId method

-

$conditions is used to determine if it should try to auto correct _id => array() queries it only appies to conditions, hence the param name

-
    -
  • access: protected
  • -
- -
- void - - _convertId - - ( &$mixed, [bool $conditions = false], mixed $mixed) -
- -
    -
  • - mixed - $mixed
  • -
  • - bool - $conditions: false
  • -
  • - - &$mixed
  • -
- - -
- -
- -
- _prepareLogQuery (line 1200) -
- - -

prepareLogQuery method

-

Any prep work to log a query

-
    -
  • access: protected
  • -
- -
- void - - _prepareLogQuery - - ( &$Model, mixed $Model) -
- -
    -
  • - mixed - $Model
  • -
  • - - &$Model
  • -
- - -
- -
- -
- _setEmptyValues (line 1176) -
- - -

Set empty values, arrays or integers, for the variables Mongo uses

-
    -
  • access: protected
  • -
- -
- void - - _setEmptyValues - - ( &$data, [array $integers = array('limit', 'offset')], mixed $data) -
- -
    -
  • - mixed - $data
  • -
  • - array - $integers: array('limit', 'offset')
  • -
  • - - &$data
  • -
- - -
- -
- -
- _stringify (line 1299) -
- - -

stringify method

-

Takes an array of args as an input and returns an array of json-encoded strings. Takes care of any objects the arrays might be holding (MongoID);

-
    -
  • access: protected
  • -
- -
- array - - _stringify - - ([ &$args = array()], [int $level = 0], array $args) -
- -
    -
  • - array - $args: array()
  • -
  • - int - $level: 0 internal recursion counter
  • -
  • - - &$args
  • -
- - -
- -
- -
- _stripAlias (line 1339) -
- - -

Convert automatically array('Model.field' => 'foo') to array('field' => 'foo')

-

This introduces the limitation that you can't have a (nested) field with the same name as the model But it's a small price to pay to be able to use other behaviors/functionality with mongoDB

-
    -
  • access: protected
  • -
- -
- void - - _stripAlias - - ([ &$args = array()], [string $alias = 'Model'], [bool $recurse = true], [string $check = 'key'], array $args) -
- -
    -
  • - array - $args: array()
  • -
  • - string - $alias: 'Model'
  • -
  • - bool - $recurse: true
  • -
  • - string - $check: 'key', 'value' or 'both'
  • -
  • - - &$args
  • -
- - -
- -
-
- - -

- Documentation generated on Tue, 26 Jul 2011 01:09:01 +0900 by phpDocumentor 1.4.3 -

-
- \ No newline at end of file diff --git a/docs/mongodb/mongodb-models-datasources/_datasources---mongodb_source.php.html b/docs/mongodb/mongodb-models-datasources/_datasources---mongodb_source.php.html deleted file mode 100644 index 2211b85..0000000 --- a/docs/mongodb/mongodb-models-datasources/_datasources---mongodb_source.php.html +++ /dev/null @@ -1,109 +0,0 @@ - - - - - - Docs for page mongodb_source.php - - - - -
-

/datasources/mongodb_source.php

- - -
-
Description
- -
- -

A CakePHP datasource for the mongoDB (http://www.mongodb.org/) document-oriented database.

-

This datasource uses Pecl Mongo (http://php.net/mongo) and is thus dependent on PHP 5.0 and greater.

Original implementation by ichikaway(Yasushi Ichikawa) http://github.com/ichikaway/

Reference: Nate Abele's lithium mongoDB datasource (http://li3.rad-dev.org/) Joél Perras' divan(http://github.com/jperras/divan/)

Copyright 2010, Yasushi Ichikawa http://github.com/ichikaway/

Contributors: Predominant, Jrbasso, tkyk, AD7six

Licensed under The MIT License Redistributions of files must retain the above copyright notice.

-
    -
  • copyright: Copyright 2010, Yasushi Ichikawa http://github.com/ichikaway/
  • -
  • license: The MIT License
  • -
- -
-
- - -
-
Classes
- -
- - - - - - - - - -
ClassDescription
- MongodbSource - - MongoDB Source -
-
-
- - - - - -
-
Functions
- -
- -
- -
- MongoDbDateFormatter (line 1375) -
- - -

MongoDbDateFormatter method

-

This function cannot be in the class because of the way model save is written

-
    -
  • access: public
  • -
- -
- void - - MongoDbDateFormatter - - ([mixed $date = null]) -
- -
    -
  • - mixed - $date: null
  • -
- - -
-
-
- -

- Documentation generated on Tue, 26 Jul 2011 01:09:01 +0900 by phpDocumentor 1.4.3 -

-
- \ No newline at end of file diff --git a/docs/packages.html b/docs/packages.html deleted file mode 100644 index 8d3e753..0000000 --- a/docs/packages.html +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/controllers/geos_controller.php b/samples/controllers/geos_controller.php deleted file mode 100644 index 33ee7d6..0000000 --- a/samples/controllers/geos_controller.php +++ /dev/null @@ -1,101 +0,0 @@ - 35, - 'page' => 1, - ); - - if(!empty($type) && !empty($lat) && !empty($long)) { - $lat = floatval($lat); - $long = floatval($long); - $opt1 = floatval($opt1); - $opt2 = floatval($opt2); - - switch($type) { - case('near'): - if(!empty($opt1)){ - $cond = array('loc' => array('$near' => array($lat, $long), '$maxDistance' => $opt1)); - } else { - $cond = array('loc' => array('$near' => array($lat, $long))); - } - break; - case('box'): - $lowerLeft = array($lat, $long); - $upperRight = array($opt1, $opt2); - $cond = array('loc' => array('$within' => array('$box' => array($lowerLeft, $upperRight)))); - break; - case('circle'): - $center = array($lat, $long); - $radius = $opt1; - $cond = array('loc' => array('$within' => array('$center' => array($center, $radius)))); - break; - } - $params['conditions'] = $cond; - - } else { - $params['order'] = array('_id' => -1); - } - - $results = $this->Geo->find('all', $params); - $this->set(compact('results')); - } - - /** - * add method - * - * @return void - * @access public - */ - public function add() { - if (!empty($this->data)) { - - $this->Geo->create(); - if ($this->Geo->save($this->data)) { - $this->flash(__('Geo saved.', true), array('action' => 'index')); - } else { - } - } - } - - /** - * delete method - * - * @param mixed $id null - * @return void - * @access public - */ - public function delete($id = null) { - if (!$id) { - $this->flash(__('Invalid Geo', true), array('action' => 'index')); - } - if ($this->Geo->delete($id)) { - $this->flash(__('Geo deleted', true), array('action' => 'index')); - } else { - $this->flash(__('Geo deleted Fail', true), array('action' => 'index')); - } - } -} diff --git a/samples/controllers/posts_controller.php b/samples/controllers/posts_controller.php deleted file mode 100644 index 58266cb..0000000 --- a/samples/controllers/posts_controller.php +++ /dev/null @@ -1,142 +0,0 @@ - array('title', 'body', 'hoge'), - //'fields' => array('Post.title', ), - //'conditions' => array('title' => 'hehe'), - //'conditions' => array('hoge' => array('$gt' => '10', '$lt' => '34')), - //'order' => array('title' => 1, 'body' => 1), - 'order' => array('_id' => -1), - 'limit' => 35, - 'page' => 1, - ); - $results = $this->Post->find('all', $params); - //$result = $this->Post->find('count', $params); - $this->set(compact('results')); - } - -/** - * add method - * - * @return void - * @access public - */ - public function add() { - if (!empty($this->data)) { - - $this->Post->create(); - if ($this->Post->save($this->data)) { - $this->flash(__('Post saved.', true), array('action' => 'index')); - } else { - } - } - } - -/** - * edit method - * - * @param mixed $id null - * @return void - * @access public - */ - public function edit($id = null) { - if (!$id && empty($this->data)) { - $this->flash(__('Invalid Post', true), array('action' => 'index')); - } - if (!empty($this->data)) { - - if ($this->Post->save($this->data)) { - $this->flash(__('The Post has been saved.', true), array('action' => 'index')); - } else { - } - } - if (empty($this->data)) { - $this->data = $this->Post->read(null, $id); - //$this->data = $this->Post->find('first', array('conditions' => array('_id' => $id))); - } - } - -/** - * delete method - * - * @param mixed $id null - * @return void - * @access public - */ - public function delete($id = null) { - if (!$id) { - $this->flash(__('Invalid Post', true), array('action' => 'index')); - } - if ($this->Post->delete($id)) { - $this->flash(__('Post deleted', true), array('action' => 'index')); - } else { - $this->flash(__('Post deleted Fail', true), array('action' => 'index')); - } - } - -/** - * deleteall method - * - * @return void - * @access public - */ - public function deleteall() { - $conditions = array('title' => 'aa'); - if ($this->Post->deleteAll($conditions)) { - $this->flash(__('Post deleteAll success', true), array('action' => 'index')); - - } else { - $this->flash(__('Post deleteAll Fail', true), array('action' => 'index')); - } - } - -/** - * updateall method - * - * @return void - * @access public - */ - public function updateall() { - $conditions = array('title' => 'ichi2' ); - - $field = array('title' => 'ichi' ); - - if ($this->Post->updateAll($field, $conditions)) { - $this->flash(__('Post updateAll success', true), array('action' => 'index')); - - } else { - $this->flash(__('Post updateAll Fail', true), array('action' => 'index')); - } - } - - public function createindex() { - $mongo = ConnectionManager::getDataSource($this->Post->useDbConfig); - $mongo->ensureIndex($this->Post, array('title' => 1)); - - } -} \ No newline at end of file diff --git a/samples/controllers/subdocuments_controller.php b/samples/controllers/subdocuments_controller.php deleted file mode 100644 index 5c1b63d..0000000 --- a/samples/controllers/subdocuments_controller.php +++ /dev/null @@ -1,91 +0,0 @@ - array('_id' => -1), - 'limit' => 35, - 'page' => 1, - ); - $results = $this->Subdocument->find('all', $params); - $this->set(compact('results')); - } - -/** - * add method - * - * @return void - * @access public - */ - public function add() { - if (!empty($this->data)) { - $this->Subdocument->create(); - if ($this->Subdocument->save($this->data)) { - $this->flash(__('Subdocument saved.', true), array('action' => 'index')); - } else { - } - } - } - -/** - * edit method - * - * @param mixed $id null - * @return void - * @access public - */ - public function edit($id = null) { - if (!$id && empty($this->data)) { - $this->flash(__('Invalid Subdocument', true), array('action' => 'index')); - } - if (!empty($this->data)) { - - if ($this->Subdocument->save($this->data)) { - $this->flash(__('The Subdocument has been saved.', true), array('action' => 'index')); - } else { - } - } - if (empty($this->data)) { - $this->data = $this->Subdocument->read(null, $id); - } - } - -/** - * delete method - * - * @param mixed $id null - * @return void - * @access public - */ - public function delete($id = null) { - if (!$id) { - $this->flash(__('Invalid Subdocument', true), array('action' => 'index')); - } - if ($this->Subdocument->delete($id)) { - $this->flash(__('Subdocument deleted', true), array('action' => 'index')); - } else { - $this->flash(__('Subdocument deleted Fail', true), array('action' => 'index')); - } - } -} \ No newline at end of file diff --git a/samples/models/geo.php b/samples/models/geo.php deleted file mode 100644 index b78ce4d..0000000 --- a/samples/models/geo.php +++ /dev/null @@ -1,34 +0,0 @@ - array('type'=>'string'), - 'body'=>array('type'=>'string'), - 'loc'=>array( - 'lat' => array('type'=>'float'), - 'long' => array('type'=>'float'), - ), - 'created'=>array('type'=>'datetime'), - 'modified'=>array('type'=>'datetime'), - ); -*/ - - function beforeSave() { - if(!empty($this->data[$this->alias]['loc'])) { - //convert location info from string to float - $this->data[$this->alias]['loc']['lat'] = floatval($this->data[$this->alias]['loc']['lat']); - $this->data[$this->alias]['loc']['long'] = floatval($this->data[$this->alias]['loc']['long']); - } - return true; - } - - - function afterSave($created) { - //create Gespatial Index - $mongo = $this->getDataSource(); - $mongo->ensureIndex($this, array('loc' => "2d")); - return true; - } - -} diff --git a/samples/models/post.php b/samples/models/post.php deleted file mode 100644 index 2add2ed..0000000 --- a/samples/models/post.php +++ /dev/null @@ -1,14 +0,0 @@ - array('type'=>'string'), - 'body'=>array('type'=>'string'), - 'hoge'=>array('type'=>'string'), - 'created'=>array('type'=>'datetime'), - 'modified'=>array('type'=>'datetime'), - ); -*/ -} \ No newline at end of file diff --git a/samples/models/subdocument.php b/samples/models/subdocument.php deleted file mode 100644 index f34c72f..0000000 --- a/samples/models/subdocument.php +++ /dev/null @@ -1,15 +0,0 @@ - array('type'=>'string'), - 'body'=>array('type'=>'string'), - 'subdoc'=>array( - 'name' => array('type'=>'string'), - 'age' => array('type'=>'integer') - ), - 'created'=>array('type'=>'datetime'), - 'modified'=>array('type'=>'datetime'), - ); -*/ -} \ No newline at end of file diff --git a/samples/views/geos/add.ctp b/samples/views/geos/add.ctp deleted file mode 100755 index c53726d..0000000 --- a/samples/views/geos/add.ctp +++ /dev/null @@ -1,27 +0,0 @@ -
-Form->create('Geo' , array( 'type' => 'post' ));?> -
- - Form->input('title'); - echo $this->Form->input('body'); - echo $this->Form->input('Geo.loc.lat', array('label' => 'latitude')); - echo $this->Form->input('Geo.loc.long', array('label' => 'longitude')); - ?> -
-Form->end('Submit');?> -
-
- -
- - -Form->end('Submit');?> - -
- -
diff --git a/samples/views/geos/index.ctp b/samples/views/geos/index.ctp deleted file mode 100644 index fd9a1f8..0000000 --- a/samples/views/geos/index.ctp +++ /dev/null @@ -1,23 +0,0 @@ -Html->link('Add data', 'add'); ?> -
-
- - - id: - [Html->link('delete','delete/'.$result['Geo']['_id']); ?>] - [ 'index', 'near', $result['Geo']['loc']['lat'], $result['Geo']['loc']['long']); - echo $this->Html->link('near here', $url); - ?>] - [ 'index', 'circle', $result['Geo']['loc']['lat'], $result['Geo']['loc']['long'], 10); - echo $this->Html->link('around here', $url); - ?>] -
- title:
- body:
- latitude:
- longitude:
- -
- diff --git a/samples/views/posts/add.ctp b/samples/views/posts/add.ctp deleted file mode 100644 index ceebdc1..0000000 --- a/samples/views/posts/add.ctp +++ /dev/null @@ -1,17 +0,0 @@ -
-Form->create('Post' , array( 'type' => 'post' ));?> -
- - Form->input('title'); - echo $this->Form->input('body'); - echo $this->Form->input('hoge'); - ?> -
-Form->end('Submit');?> -
-
- -
diff --git a/samples/views/posts/createindex.ctp b/samples/views/posts/createindex.ctp deleted file mode 100644 index 7e46943..0000000 --- a/samples/views/posts/createindex.ctp +++ /dev/null @@ -1,5 +0,0 @@ -

Sample of create index in MongoDB

-
-
- -Creating index.... \ No newline at end of file diff --git a/samples/views/posts/edit.ctp b/samples/views/posts/edit.ctp deleted file mode 100644 index 897479f..0000000 --- a/samples/views/posts/edit.ctp +++ /dev/null @@ -1,18 +0,0 @@ -
-Form->create('Post' , array( 'type' => 'post' ));?> -
- - Form->hidden('_id'); - echo $this->Form->input('title'); - echo $this->Form->input('body'); - echo $this->Form->input('hoge'); - ?> -
-Form->end('Submit');?> -
-
- -
diff --git a/samples/views/posts/index.ctp b/samples/views/posts/index.ctp deleted file mode 100644 index 05d05ff..0000000 --- a/samples/views/posts/index.ctp +++ /dev/null @@ -1,12 +0,0 @@ -Html->link('Add data', 'add'); ?> -
-
- - - id: [Html->link('edit','edit/'.$result['Post']['_id']); ?>] [Html->link('delete','delete/'.$result['Post']['_id']); ?>]
- title:
- body:
- hoge:
- -
- diff --git a/samples/views/subdocuments/add.ctp b/samples/views/subdocuments/add.ctp deleted file mode 100644 index a0469b8..0000000 --- a/samples/views/subdocuments/add.ctp +++ /dev/null @@ -1,31 +0,0 @@ -
-Form->create('Subdocument' , array( 'type' => 'post' ));?> -
- - Form->input('title'); - echo $this->Form->input('body'); - //echo $this->Form->input('subdoc.name'); - //echo $this->Form->input('subdoc.age'); - echo $this->Form->input('Subdocument.subdoc.0.name'); - echo $this->Form->input('Subdocument.subdoc.0.age'); - echo $this->Form->input('Subdocument.subdoc.1.name'); - echo $this->Form->input('Subdocument.subdoc.1.age'); - ?> -
-Form->end('Submit');?> -
-
- -
- - -Form->end('Submit');?> - -
- -
diff --git a/samples/views/subdocuments/edit.ctp b/samples/views/subdocuments/edit.ctp deleted file mode 100644 index 2b1be58..0000000 --- a/samples/views/subdocuments/edit.ctp +++ /dev/null @@ -1,21 +0,0 @@ -
-Form->create('Subdocument' , array( 'type' => 'post' ));?> -
- - Form->hidden('_id'); - echo $this->Form->input('title'); - echo $this->Form->input('body'); - echo $this->Form->input('Subdocument.subdoc.0.name'); - echo $this->Form->input('Subdocument.subdoc.0.age'); - echo $this->Form->input('Subdocument.subdoc.1.name'); - echo $this->Form->input('Subdocument.subdoc.1.age'); - ?> -
-Form->end('Submit');?> -
-
- -
diff --git a/samples/views/subdocuments/index.ctp b/samples/views/subdocuments/index.ctp deleted file mode 100644 index 1bc8b6c..0000000 --- a/samples/views/subdocuments/index.ctp +++ /dev/null @@ -1,15 +0,0 @@ -Html->link('Add data', 'add'); ?> -
-
- - - id: [Html->link('edit','edit/'.$result['Subdocument']['_id']); ?>] [Html->link('delete','delete/'.$result['Subdocument']['_id']); ?>]
- title:
- body:
- $val): ?> - subdoc_name:
- subdoc_age:
- - -
- diff --git a/tests/cases/1st/first_mongodb_source.test.php b/tests/cases/1st/first_mongodb_source.test.php deleted file mode 100644 index 68a35df..0000000 --- a/tests/cases/1st/first_mongodb_source.test.php +++ /dev/null @@ -1,171 +0,0 @@ - 'mongodb', - 'host' => 'localhost', - 'login' => '', - 'password' => '', - 'database' => 'test_mongo', - 'port' => 27017, - 'prefix' => '', - 'persistent' => false, - ); - -/** - * Sets up the environment for each test method - * - * @return void - * @access public - */ - public function startTest() { - $connections = ConnectionManager::enumConnectionObjects(); - - if (!empty($connections['test']['classname']) && $connections['test']['classname'] === 'mongodbSource') { - $config = new DATABASE_CONFIG(); - $this->_config = $config->test; - } - - ConnectionManager::create('mongo_test', $this->_config); - - } - -/** - * Destroys the environment after each test method is run - * - * @return void - * @access public - */ - public function endTest() { - $this->mongodb = new MongodbSource($this->_config); - $this->mongodb->connect(); - $this->dropData(); - unset($this->mongodb); - } - - -/** - * Drop database - * - * @return void - * @access public - */ - public function dropData() { - try { - $db = $this->mongodb - ->connection - ->selectDB($this->_config['database']); - - foreach($db->listCollections() as $collection) { - $collection->drop(); - } - } catch (MongoException $e) { - trigger_error($e->getMessage()); - } - } - - -/** - * testSchemaless method - * - * Test you can save to a model without specifying mongodb. - * - * @return void - * @access public - */ - public function testSchemaless() { - $toSave = array( - 'title' => 'A test article', - 'body' => str_repeat('Lorum ipsum ', 100), - 'tags' => array( - 'one', - 'two', - 'three' - ), - 'modified' => null, - 'created' => null - ); - - $MongoArticle = ClassRegistry::init('MongoArticleSchemafree'); - $MongoArticle->create(); - $this->assertTrue($MongoArticle->save($toSave), 'Saving with no defined schema failed'); - - $expected = array_intersect_key($toSave, array_flip(array('title', 'body', 'tags'))); - $result = $MongoArticle->read(array('title', 'body', 'tags')); - unset ($result['MongoArticleSchemafree']['_id']); // prevent auto added field from screwing things up - $this->assertEqual($expected, $result['MongoArticleSchemafree']); - - $toSave = array( - 'title' => 'Another test article', - 'body' => str_repeat('Lorum pipsum ', 100), - 'tags' => array( - 'four', - 'five', - 'six' - ), - 'starts' => date('Y-M-d H:i:s'), - 'modified' => null, - 'created' => null - ); - $MongoArticle->create(); - $this->assertTrue($MongoArticle->save($toSave), 'Saving with no defined schema failed'); - $starts = $MongoArticle->field('starts'); - $this->assertEqual($toSave['starts'], $starts); - } - -} diff --git a/tests/cases/behaviors/sql_compatible.test.php b/tests/cases/behaviors/sql_compatible.test.php deleted file mode 100644 index 4874716..0000000 --- a/tests/cases/behaviors/sql_compatible.test.php +++ /dev/null @@ -1,314 +0,0 @@ -lastQuery = $query; - return $query; - } -} - -/** - * SqlCompatibleTest class - * - * @uses CakeTestCase - * @package mongodb - * @subpackage mongodb.tests.cases.behaviors - */ -class SqlCompatibleTest extends CakeTestCase { - -/** - * Default db config. overriden by test db connection if present - * - * @var array - * @access protected - */ - protected $_config = array( - 'datasource' => 'mongodb', - 'host' => 'localhost', - 'login' => '', - 'password' => '', - 'database' => 'test_mongo', - 'port' => 27017, - 'prefix' => '', - 'persistent' => false, - ); - -/** - * Sets up the environment for each test method - * - * @return void - * @access public - */ - public function startTest() { - $connections = ConnectionManager::enumConnectionObjects(); - - if (!empty($connections['test']['classname']) && $connections['test']['classname'] === 'mongodbSource') { - $config = new DATABASE_CONFIG(); - $this->_config = $config->test; - } - - ConnectionManager::create('mongo_test', $this->_config); - $this->Mongo = new MongodbSource($this->_config); - - $this->Post = ClassRegistry::init(array('class' => 'SqlCompatiblePost', 'alias' => 'Post', 'ds' => 'mongo_test')); - - $this->_setupData(); - } - -/** - * Destroys the environment after each test method is run - * - * @return void - * @access public - */ - public function endTest() { - $this->Post->deleteAll(true); - unset($this->Post); - } - -/** - * testNOT method - * - * @return void - * @access public - */ - public function testNOT() { - $expected = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20); - $result = $this->Post->find('all', array( - 'conditions' => array( - 'title !=' => 10, - ), - 'fields' => array('_id', 'title', 'number'), - 'order' => array('number' => 'ASC') - )); - $result = Set::extract($result, '/Post/title'); - $this->assertEqual($expected, $result); - - $conditions = array( - 'title' => array('$ne' => 10) - ); - $this->assertEqual($conditions, $this->Post->lastQuery['conditions']); - - $result = $this->Post->find('all', array( - 'conditions' => array( - 'NOT' => array( - 'title' => 10 - ), - ), - 'fields' => array('_id', 'title', 'number'), - 'order' => array('number' => 'ASC') - )); - $result = Set::extract($result, '/Post/title'); - $this->assertEqual($expected, $result); - - $conditions = array( - 'title' => array('$nin' => array(10)) - ); - $this->assertEqual($conditions, $this->Post->lastQuery['conditions']); - } - -/** - * testGTLT method - * - * @return void - * @access public - */ - public function testGTLT() { - $expected = array(8, 9, 10, 11, 12, 13); - $result = $this->Post->find('all', array( - 'conditions' => array( - 'title >' => 7, - 'title <' => 14, - ), - 'fields' => array('_id', 'title', 'number'), - 'order' => array('number' => 'ASC') - )); - $result = Set::extract($result, '/Post/title'); - $this->assertEqual($expected, $result); - - $conditions = array( - 'title' => array( - '$gt' => 7, - '$lt' => 14 - ) - ); - $this->assertEqual($conditions, $this->Post->lastQuery['conditions']); - } - -/** - * testGTE method - * - * @return void - * @access public - */ - public function testGTE() { - $expected = array(19, 20); - $result = $this->Post->find('all', array( - 'conditions' => array( - 'title >=' => 19, - ), - 'fields' => array('_id', 'title', 'number'), - 'order' => array('number' => 'ASC') - )); - $result = Set::extract($result, '/Post/title'); - $this->assertEqual($expected, $result); - - $conditions = array( - 'title' => array('$gte' => 19) - ); - $this->assertEqual($conditions, $this->Post->lastQuery['conditions']); - } - -/** - * testOR method - * - * @return void - * @access public - */ - public function testOR() { - $expected = array(1, 2, 19, 20); - $result = $this->Post->find('all', array( - 'conditions' => array( - 'OR' => array( - 'title <=' => 2, - 'title >=' => 19, - ) - ), - 'fields' => array('_id', 'title', 'number'), - 'order' => array('number' => 'ASC') - )); - $result = Set::extract($result, '/Post/title'); - $this->assertEqual($expected, $result); - - $conditions = array( - '$or' => array( - array('title' => array('$lte' => 2)), - array('title' => array('$gte' => 19)) - ) - ); - $this->assertEqual($conditions, $this->Post->lastQuery['conditions']); - } - - - /** - * Tests find method with conditions _id=>array() - * - * @return void - * @access public - */ - public function testFindConditionIn() { - - for ($i = 1; $i <= 5; $i++) { - $data = array( - '_id' => 'A1' . $i, - 'title' => $i, - ); - $saveData['Post'] = $data; - $this->Post->create(); - $this->Post->save($saveData); - } - - $params = array('conditions' => array('_id' => array('A11', 'A12'))); - $result = $this->Post->find('all', $params); - - $expected = array('A11','A12'); - $result = Set::extract($result, '/Post/_id'); - $this->assertEqual($expected, $result); - $this->assertEqual(2, count($result)); - - $conditions = array( - '_id' => array( - '$in' => array('A11', 'A12') - ) - ); - $this->assertEqual($conditions, $this->Post->lastQuery['conditions']); - - - $params = array('conditions' => array('_id' => array('$nin' => array('A11', 'A12')))); - $result = $this->Post->find('all', $params); - //$expected = array('A13','A14'); - $result = Set::extract($result, '/Post/_id'); - $this->assertTrue(in_array('A13', $result)); - $this->assertFalse(in_array('A11', $result)); - $this->assertFalse(in_array('A12', $result)); - $this->assertEqual(23, count($result)); - - - $conditions = array( - '_id' => array( - '$nin' => array('A11', 'A12') - ) - ); - $this->assertEqual($conditions, $this->Post->lastQuery['conditions']); - } - - -/** - * setupData method - * - * @return void - * @access protected - */ - protected function _setupData() { - $this->Post->deleteAll(true); - for ($i = 1; $i <= 20; $i++) { - $data = array( - 'title' => $i, - ); - $saveData['Post'] = $data; - $this->Post->create(); - $this->Post->save($saveData); - } - } -}