Permalink
Browse files

Merge branch 'trunk' into luckysmack

  • Loading branch information...
2 parents 125d5f1 + b633533 commit 7c8df2fa90e177303e6e0a727f8aadcb63d29e6e LuckySMack committed Dec 2, 2010
Showing with 49 additions and 30 deletions.
  1. +21 −13 EMongoCriteria.php
  2. +5 −0 EMongoDocument.php
  3. +23 −17 EMongoDocumentDataProvider.php
View
@@ -4,18 +4,19 @@ class EMongoCriteria extends CComponent
{
public static $operators = array(
'greater' => '$gt',
- 'greaterEq' => '$gte',
+ 'greatereq' => '$gte',
'less' => '$lt',
- 'lessEq' => '$lte',
- 'notEq' => '$ne',
+ 'lesseq' => '$lte',
+ 'noteq' => '$ne',
'in' => '$in',
- 'notIn' => '$nin',
+ 'notin' => '$nin',
'all' => '$all',
'size' => '$size',
'exists' => '$exists',
- 'notExists' => '$exists',
+ 'notexists' => '$exists',
'mod' => '$mod',
- 'equals' => '$$eq'
+ 'equals' => '$$eq',
+ 'where' => '$where'
);
const SORT_ASC = 1;
@@ -46,6 +47,7 @@ class EMongoCriteria extends CComponent
* 'fieldName10'=>array('exists'),
* 'fieldName11'=>array('notExists'),
* 'fieldName12'=>array('mod', array(10, 9)),
+ * 'fieldName13'=>array('equals', 1)
* ),
* 'select'=>array('fieldName', 'fieldName2'),
* 'limit'=>10,
@@ -62,7 +64,9 @@ public function __construct($criteria=null)
if(isset($criteria['conditions']))
foreach($criteria['conditions'] as $fieldName=>$cond)
{
- call_user_func_array(array($this, array_shift($cond)), array($fieldName, array_shift($cond)));
+ $operator = strtolower(array_shift($cond));
+ $value = array_shift($cond);
+ call_user_func_array(array($this, $operator), array($fieldName, $value));
}
if(isset($criteria['select']))
$this->select($criteria['select']);
@@ -120,20 +124,23 @@ public function mergeWith($criteria)
* If we have operator add it otherwise call parent implementation
* @see CComponent::__call()
*/
- public function __call($name, $parameters)
+ public function __call($operatorName, $parameters)
{
- if(in_array($name, array_keys(self::$operators)))
+ $fieldName = array_shift($parameters);
+ $value = array_shift($parameters);
+ $operatorName = strtolower($operatorName);
+ if(in_array($operatorName, array_keys(self::$operators)))
{
- switch($name)
+ switch($operatorName)
{
case 'exists':
- $this->addCond($parameters[0], self::$operators[$name], true);
+ $this->addCond($fieldName, $operatorName, true);
break;
case 'notExists':
- $this->addCond($parameters[0], self::$operators[$name], false);
+ $this->addCond($fieldName, $operatorName, false);
break;
default:
- $this->addCond($parameters[0], self::$operators[$name], $parameters[1]);
+ $this->addCond($fieldName, $operatorName, $value);
}
return $this;
}
@@ -244,6 +251,7 @@ public function sort($fieldName, $order)
*/
protected function addCond($fieldName, $op, $value)
{
+ $op = self::$operators[$op];
if(!isset($this->_conditions[$fieldName]) && $op != self::$operators['equals'])
$this->_conditions[$fieldName] = array();
View
@@ -185,6 +185,11 @@ public function scopes()
return array();
}
+ public function resetScopes()
+ {
+ $this->_criteria = new EMongoCriteria();
+ }
+
/**
* Saves the current record.
*
@@ -31,29 +31,33 @@ class EMongoDocumentDataProvider extends CDataProvider
*/
public $model;
- private $_query;
+ private $_criteria;
/**
* Constructor.
* @param mixed $modelClass the model class (e.g. 'Post') or the model finder instance
* (e.g. <code>Post::model()</code>, <code>Post::model()->published()</code>).
- * @param array $query query array witch will be passed to MongoDB collection find() method
* @param array $config configuration (name=>value) to be applied as the initial property values of this class.
*/
- public function __construct($modelClass, $query = array(), $config = array())
+ public function __construct($modelClass, $config = array())
{
if(is_string($modelClass))
{
$this->modelClass = $modelClass;
$this->model = EMongoDocument::model($modelClass);
}
- else if($modelClass instanceof EMongoRecord)
+ else if($modelClass instanceof EMongoDocument)
{
$this->modelClass = get_class($modelClass);
$this->model = $modelClass;
}
- $this->_query = $query;
+ $this->_criteria = $this->model->getDbCriteria();
+ if(isset($config['criteria']))
+ {
+ $this->_criteria->mergeWith($config['criteria']);
+ unset($config['criteria']);
+ }
$this->setId($this->modelClass);
foreach($config as $key=>$value)
@@ -69,21 +73,24 @@ public function __construct($modelClass, $query = array(), $config = array())
}
/**
- * Returns the query criteria.
+ * Returns the criteria.
* @return array the query criteria
*/
- public function getQuery()
+ public function getCriteria()
{
- return $this->_query;
+ return $this->_criteria;
}
/**
* Sets the query criteria.
* @param array $value the query criteria. Array representing the MongoDB query criteria.
*/
- public function setQuery(array $query)
+ public function setCriteria($criteria)
{
- $this->_query = $query;
+ if(is_array($criteria))
+ $this->_criteria = new EMongoCriteria($criteria);
+ else if($criteria instanceof EMongoCriteria)
+ $this->_criteria = $criteria;
}
/**
@@ -92,26 +99,25 @@ public function setQuery(array $query)
*/
protected function fetchData()
{
- $criteria = array('query'=>$this->_query, 'sort'=>null, 'limit'=>null, 'offset'=>null);
if(($pagination=$this->getPagination())!==false)
{
$pagination->setItemCount($this->getTotalItemCount());
- $criteria['limit']=$pagination->getLimit();
- $criteria['offset']=$pagination->getOffset();
+ $this->_criteria->setLimit($pagination->getLimit());
+ $this->_criteria->setOffset($pagination->getOffset());
}
if(($sort=$this->getSort())!==false && ($order=$sort->getOrderBy())!='')
{
$sort=array();
foreach($this->getSortDirections($order) as $name=>$descending)
{
- $sort[$name]=$descending ? -1 : 1;
+ $sort[$name]=$descending ? EMongoCriteria::SORT_DESC : EMongoCriteria::SORT_ASC;
}
- $criteria['sort']=$sort;
+ $this->_criteria->setSort($sort);
}
- return $this->model->findAll($criteria['query'], $criteria['sort'], $criteria['limit'], $criteria['offset']);
+ return $this->model->findAll($this->_criteria);
}
/**
@@ -134,7 +140,7 @@ protected function fetchKeys()
*/
public function calculateTotalItemCount()
{
- return $this->model->getCollection()->count($this->_query);
+ return $this->model->count($this->_criteria);
}
/**

0 comments on commit 7c8df2f

Please sign in to comment.