diff --git a/EMongoCriteria.php b/EMongoCriteria.php index 929de36d..1719bcca 100644 --- a/EMongoCriteria.php +++ b/EMongoCriteria.php @@ -124,7 +124,8 @@ public function __construct($criteria=null) { $this->setWorkingFields($fieldNameArray); $operator = strtolower($operator); - + if (is_long($value) && strlen($value) > 10) + $value = new MongoInt64($value); $this->$fieldName($operator, $value); } } @@ -179,7 +180,11 @@ public function mergeWith($criteria) $this->_conditions[$fieldName] = array(); foreach($conds as $operator => $value) + { + if (is_long($value) && strlen($value) > 10) + $value = new MongoInt64($value); $this->_conditions[$fieldName][$operator] = $value; + } } else $this->_conditions[$fieldName] = $conds; @@ -348,9 +353,10 @@ public function setUseCursor($useCursor) * the fields in this format * @since v1.3.1 */ - public function getSelect() + public function getSelect($forCursor = false) { - return $this->_select; + if (!$forCursor) return $this->_select; + return array_fill_keys($this->_select, true); // PHP 5.2.0+ required! } /** @@ -358,17 +364,7 @@ public function getSelect() */ public function setSelect(array $select) { - $this->_select = array(); - // Convert the select array to field=>true/false format - foreach ($select as $key=>$value) { - if (is_int($key)) { - $this->_select[$value] = true; - } - else - { - $this->_select[$key] = $value; - } - } + $this->_select = $select; } /** @@ -397,7 +393,7 @@ public function setWorkingFields(array $select) public function select(array $fieldList=null) { if($fieldList!==null) - $this->setSelect(array_merge($this->_select, $fieldList)); + $this->_select = array_merge($this->_select, $fieldList); return $this; } @@ -451,6 +447,8 @@ public function sort($fieldName, $order) */ public function addCond($fieldName, $op, $value) { + if (is_long($value) && strlen($value) > 10) + $value = new MongoInt64($value); $op = self::$operators[$op]; if($op == self::$operators['or']) diff --git a/EMongoDB.php b/EMongoDB.php index d1548334..43a3bbd8 100644 --- a/EMongoDB.php +++ b/EMongoDB.php @@ -47,6 +47,20 @@ class EMongoDB extends CApplicationComponent */ public $persistentConnection = false; + /** + * true or string(mongo set replica "set" name) + * @link http://php.net/manual/en/mongo.construct.php + * @var string $replicaSet The name of the replica set to connect to. + */ + public $replicaSet = false; + + /** + * + * @var for Mongo php extends 1.3.2 + */ + public $readPreference = null; + + public $timeout = 1000; /** * @var string $dbName name of the Mongo database to use * @since v1.0 @@ -135,15 +149,20 @@ public function getConnection() if(empty($this->connectionString)) throw new EMongoException(Yii::t('yii', 'EMongoDB.connectionString cannot be empty.')); - if($this->persistentConnection !== false) - $this->_mongoConnection = new Mongo($this->connectionString, array( - 'connect'=>$this->autoConnect, - 'persist'=>$this->persistentConnection - )); + $options = array('connect'=>$this->autoConnect); + if ($this->replicaSet !== false) + $options['replicaSet'] = $this->replicaSet; + if(class_exists('MongoClient', false) && $this->persistentConnection !== false) + $options['persist'] = $this->persistentConnection; + if (class_exists('MongoClient', false)) // for php Mongo extends: PECL mongoclient >=1.3.0. + { + // Read priorities from slave + if ($this->readPreference === null) + $options['readPreference'] = MongoClient::RP_SECONDARY_PREFERRED; + $this->_mongoConnection = new MongoClient($this->connectionString, $options); + } else - $this->_mongoConnection = new Mongo($this->connectionString, array( - 'connect'=>$this->autoConnect, - )); + $this->_mongoConnection = new Mongo($this->connectionString, $options); return $this->_mongoConnection; } @@ -225,4 +244,4 @@ public function dropDb() { $this->_mongoDb->drop(); } -} +} \ No newline at end of file diff --git a/EMongoDocument.php b/EMongoDocument.php index 0f72bc66..f24c7128 100644 --- a/EMongoDocument.php +++ b/EMongoDocument.php @@ -4,7 +4,7 @@ * * PHP version 5.2+ * - * @author Dariusz Górecki + * @author Dariusz G璐竢ecki * @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org * @copyright 2011 CleverIT http://www.cleverit.com.pl * @license http://www.yiiframework.com/license/ BSD license @@ -814,12 +814,10 @@ public function refresh() */ public function find($criteria=null) { - Yii::trace(get_class($this).'.find()','ext.MongoDb.EMongoDocument'); - if($this->beforeFind()) { $this->applyScopes($criteria); - + Yii::trace(get_class($this).'.findAll(),Query:'.self::getMongoQuery($criteria->getConditions()),'ext.MongoDb.EMongoDocument'); $doc = $this->getCollection()->findOne($criteria->getConditions(), $criteria->getSelect()); return $this->populateRecord($doc); @@ -836,14 +834,11 @@ public function find($criteria=null) */ public function findAll($criteria=null) { - Yii::trace(get_class($this).'.findAll()','ext.MongoDb.EMongoDocument'); - if($this->beforeFind()) { $this->applyScopes($criteria); - + Yii::trace(get_class($this).'.findAll(),Query:'.$this->getCollection().'.find:'.self::getMongoQuery($criteria->getConditions()),'ext.MongoDb.EMongoDocument'); $cursor = $this->getCollection()->find($criteria->getConditions()); - if($criteria->getSort() !== null) $cursor->sort($criteria->getSort()); if($criteria->getLimit() !== null) @@ -851,7 +846,7 @@ public function findAll($criteria=null) if($criteria->getOffset() !== null) $cursor->skip($criteria->getOffset()); if($criteria->getSelect()) - $cursor->fields($criteria->getSelect()); + $cursor->fields($criteria->getSelect(true)); if($this->getUseCursor($criteria)) return new EMongoCursor($cursor, $this->model()); @@ -873,8 +868,8 @@ public function findAll($criteria=null) */ public function findByPk($pk, $criteria=null) { - Yii::trace(get_class($this).'.findByPk()','ext.MongoDb.EMongoDocument'); $criteria = new EMongoCriteria($criteria); + Yii::trace(get_class($this).'.findByPk(),Query:'.self::getMongoQuery($criteria->getConditions()),'ext.MongoDb.EMongoDocument'); $criteria->mergeWith($this->createPkCriteria($pk)); return $this->find($criteria); @@ -1337,4 +1332,34 @@ private function createPkCriteria($pk, $multiple=false) return $criteria; } + + /** + * get Mongo query string + * @param array $conditions + */ + public static function getMongoQuery($conditions) + { + if (empty($conditions)) return false; + return CJSON::encode(self::getMongoConditions($conditions)); + } + + /** + * for Mongo Conditions Compatible with Java Long INT + * @param array $array + * @return array + */ + public static function getMongoConditions($array) + { + foreach ($array as $k => $v) + { + if (is_array($v)) + $array[$k] = self::getMongoConditions($v); + else + { + if (is_object($v) && $v instanceof MongoInt64) + $array[$k] = $v->value; + } + } + return $array; + } }