Browse Source

Mongo Active Record created as draft.

tags/2.0.0-beta
Klimov Paul 11 years ago
parent
commit
37664fff9b
  1. 22
      extensions/mongo/ActiveQuery.php
  2. 1165
      extensions/mongo/ActiveRecord.php
  3. 22
      extensions/mongo/ActiveRelation.php
  4. 22
      extensions/mongo/Collection.php
  5. 37
      extensions/mongo/QueryBuilder.php

22
extensions/mongo/ActiveQuery.php

@ -0,0 +1,22 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mongo;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
/**
* Class ActiveQuery
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class ActiveQuery extends Query implements ActiveQueryInterface
{
use ActiveQueryTrait;
}

1165
extensions/mongo/ActiveRecord.php

File diff suppressed because it is too large Load Diff

22
extensions/mongo/ActiveRelation.php

@ -0,0 +1,22 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mongo;
use yii\db\ActiveRelationInterface;
use yii\db\ActiveRelationTrait;
/**
* ActiveRelation represents a relation to Mongo Active Record class.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class ActiveRelation extends ActiveQuery implements ActiveRelationInterface
{
use ActiveRelationTrait;
}

22
extensions/mongo/Collection.php

@ -106,7 +106,7 @@ class Collection extends Object
* @param array $condition description of the objects to update. * @param array $condition description of the objects to update.
* @param array $newData the object with which to update the matching records. * @param array $newData the object with which to update the matching records.
* @param array $options list of options in format: optionName => optionValue. * @param array $options list of options in format: optionName => optionValue.
* @return boolean whether operation was successful. * @return integer|boolean number of updated documents or whether operation was successful.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
public function update($condition, $newData, $options = []) public function update($condition, $newData, $options = [])
@ -115,9 +115,14 @@ class Collection extends Object
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$this->mongoCollection->update($this->buildCondition($condition), $newData, $options); $result = $this->mongoCollection->update($this->buildCondition($condition), $newData, $options);
$this->tryResultError($result);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return true; if (is_array($result) && array_key_exists('n', $result)) {
return $result['n'];
} else {
return true;
}
} catch (\Exception $e) { } catch (\Exception $e) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
@ -150,7 +155,7 @@ class Collection extends Object
* Removes data from the collection. * Removes data from the collection.
* @param array $condition description of records to remove. * @param array $condition description of records to remove.
* @param array $options list of options in format: optionName => optionValue. * @param array $options list of options in format: optionName => optionValue.
* @return boolean whether operation was successful. * @return integer|boolean number of updated documents or whether operation was successful.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
public function remove($condition = [], $options = []) public function remove($condition = [], $options = [])
@ -159,9 +164,14 @@ class Collection extends Object
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$this->tryResultError($this->mongoCollection->remove($this->buildCondition($condition), $options)); $result = $this->mongoCollection->remove($this->buildCondition($condition), $options);
$this->tryResultError($result);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return true; if (is_array($result) && array_key_exists('n', $result)) {
return $result['n'];
} else {
return true;
}
} catch (\Exception $e) { } catch (\Exception $e) {
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); throw new Exception($e->getMessage(), (int)$e->getCode(), $e);

37
extensions/mongo/QueryBuilder.php

@ -1,37 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mongo;
use yii\base\Object;
/**
* Class QueryBuilder
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class QueryBuilder extends Object
{
/**
* @var Connection the Mongo connection.
*/
public $db;
/**
* Constructor.
* @param Connection $connection the Mongo connection.
* @param array $config name-value pairs that will be used to initialize the object properties
*/
public function __construct($connection, $config = [])
{
$this->db = $connection;
parent::__construct($config);
}
// TODO
}
Loading…
Cancel
Save