Skip to content
This repository has been archived by the owner on Nov 21, 2019. It is now read-only.

replicaSet and long int value use MongoInt64 and Compatible with PECL Mongoclient 1.3 #68

Open
wants to merge 6 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions EMongoCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -348,27 +353,18 @@ 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!
}

/**
* @since v1.3.1
*/
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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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'])
Expand Down
37 changes: 28 additions & 9 deletions EMongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -225,4 +244,4 @@ public function dropDb()
{
$this->_mongoDb->drop();
}
}
}
45 changes: 35 additions & 10 deletions EMongoDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* PHP version 5.2+
*
* @author Dariusz Górecki <[email protected]>
* @author Dariusz G璐竢ecki <[email protected]>
* @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
Expand Down Expand Up @@ -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);
Expand All @@ -836,22 +834,19 @@ 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)
$cursor->limit($criteria->getLimit());
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());
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}