mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-20 00:20:44 +08:00
Merge pull request #2480 from mongosoft/mongodb
Mongodb extension refactorign
This commit is contained in:
@@ -37,7 +37,7 @@ abstract class ActiveRecord extends BaseActiveRecord
|
|||||||
* For example, to change the status to be 1 for all customers whose status is 2:
|
* For example, to change the status to be 1 for all customers whose status is 2:
|
||||||
*
|
*
|
||||||
* ~~~
|
* ~~~
|
||||||
* Customer::updateAll(['status' => 1], ['status' = 2]);
|
* Customer::updateAll(['status' => 1], ['status' => 2]);
|
||||||
* ~~~
|
* ~~~
|
||||||
*
|
*
|
||||||
* @param array $attributes attribute values (name-value pairs) to be saved into the collection
|
* @param array $attributes attribute values (name-value pairs) to be saved into the collection
|
||||||
@@ -78,7 +78,7 @@ abstract class ActiveRecord extends BaseActiveRecord
|
|||||||
* For example, to delete all customers whose status is 3:
|
* For example, to delete all customers whose status is 3:
|
||||||
*
|
*
|
||||||
* ~~~
|
* ~~~
|
||||||
* Customer::deleteAll('status = 3');
|
* Customer::deleteAll(['status' => 3]);
|
||||||
* ~~~
|
* ~~~
|
||||||
*
|
*
|
||||||
* @param array $condition description of the objects to delete.
|
* @param array $condition description of the objects to delete.
|
||||||
@@ -88,10 +88,6 @@ abstract class ActiveRecord extends BaseActiveRecord
|
|||||||
*/
|
*/
|
||||||
public static function deleteAll($condition = [], $options = [])
|
public static function deleteAll($condition = [], $options = [])
|
||||||
{
|
{
|
||||||
$options['w'] = 1;
|
|
||||||
if (!array_key_exists('multiple', $options)) {
|
|
||||||
$options['multiple'] = true;
|
|
||||||
}
|
|
||||||
return static::getCollection()->remove($condition, $options);
|
return static::getCollection()->remove($condition, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,8 +233,7 @@ abstract class ActiveRecord extends BaseActiveRecord
|
|||||||
$values[$key] = isset($currentAttributes[$key]) ? $currentAttributes[$key] : null;
|
$values[$key] = isset($currentAttributes[$key]) ? $currentAttributes[$key] : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$collection = static::getCollection();
|
$newId = static::getCollection()->insert($values);
|
||||||
$newId = $collection->insert($values);
|
|
||||||
$this->setAttribute('_id', $newId);
|
$this->setAttribute('_id', $newId);
|
||||||
foreach ($values as $name => $value) {
|
foreach ($values as $name => $value) {
|
||||||
$this->setOldAttribute($name, $value);
|
$this->setOldAttribute($name, $value);
|
||||||
|
|||||||
@@ -259,6 +259,18 @@ class Collection extends Object
|
|||||||
return $this->mongoCollection->find($this->buildCondition($condition), $fields);
|
return $this->mongoCollection->find($this->buildCondition($condition), $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a a single document.
|
||||||
|
* @param array $condition query condition
|
||||||
|
* @param array $fields fields to be selected
|
||||||
|
* @return array|null the single document. Null is returned if the query results in nothing.
|
||||||
|
* @see http://www.php.net/manual/en/mongocollection.findone.php
|
||||||
|
*/
|
||||||
|
public function findOne($condition = [], $fields = [])
|
||||||
|
{
|
||||||
|
return $this->mongoCollection->findOne($this->buildCondition($condition), $fields);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts new data into collection.
|
* Inserts new data into collection.
|
||||||
* @param array|object $data data to be inserted.
|
* @param array|object $data data to be inserted.
|
||||||
@@ -372,11 +384,12 @@ class Collection extends Object
|
|||||||
* @param array $options list of options in format: optionName => optionValue.
|
* @param array $options list of options in format: optionName => optionValue.
|
||||||
* @return integer|boolean number of updated documents or whether operation was successful.
|
* @return integer|boolean number of updated documents or whether operation was successful.
|
||||||
* @throws Exception on failure.
|
* @throws Exception on failure.
|
||||||
|
* @see http://www.php.net/manual/en/mongocollection.remove.php
|
||||||
*/
|
*/
|
||||||
public function remove($condition = [], $options = [])
|
public function remove($condition = [], $options = [])
|
||||||
{
|
{
|
||||||
$condition = $this->buildCondition($condition);
|
$condition = $this->buildCondition($condition);
|
||||||
$options = array_merge(['w' => 1, 'multiple' => true], $options);
|
$options = array_merge(['w' => 1, 'justOne' => false], $options);
|
||||||
$token = $this->composeLogToken('remove', [$condition, $options]);
|
$token = $this->composeLogToken('remove', [$condition, $options]);
|
||||||
Yii::info($token, __METHOD__);
|
Yii::info($token, __METHOD__);
|
||||||
try {
|
try {
|
||||||
@@ -522,7 +535,7 @@ class Collection extends Object
|
|||||||
* Argument will be automatically cast to [[\MongoCode]].
|
* Argument will be automatically cast to [[\MongoCode]].
|
||||||
* @param string|array $out output collection name. It could be a string for simple output
|
* @param string|array $out output collection name. It could be a string for simple output
|
||||||
* ('outputCollection'), or an array for parametrized output (['merge' => 'outputCollection']).
|
* ('outputCollection'), or an array for parametrized output (['merge' => 'outputCollection']).
|
||||||
* You can pass ['inline' => true] to fetch the result at once without temporary collection usage.
|
* You can pass ['inline' => true] to fetch the result at once without temporary collection usage.
|
||||||
* @param array $condition criteria for including a document in the aggregation.
|
* @param array $condition criteria for including a document in the aggregation.
|
||||||
* @param array $options additional optional parameters to the mapReduce command. Valid options include:
|
* @param array $options additional optional parameters to the mapReduce command. Valid options include:
|
||||||
* - sort - array - key to sort the input documents. The sort key must be in an existing index for this collection.
|
* - sort - array - key to sort the input documents. The sort key must be in an existing index for this collection.
|
||||||
|
|||||||
@@ -72,6 +72,11 @@ use Yii;
|
|||||||
*/
|
*/
|
||||||
class Connection extends Component
|
class Connection extends Component
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @event Event an event that is triggered after a DB connection is established
|
||||||
|
*/
|
||||||
|
const EVENT_AFTER_OPEN = 'afterOpen';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string host:port
|
* @var string host:port
|
||||||
*
|
*
|
||||||
@@ -233,6 +238,7 @@ class Connection extends Component
|
|||||||
$options['db'] = $this->defaultDatabaseName;
|
$options['db'] = $this->defaultDatabaseName;
|
||||||
}
|
}
|
||||||
$this->mongoClient = new \MongoClient($this->dsn, $options);
|
$this->mongoClient = new \MongoClient($this->dsn, $options);
|
||||||
|
$this->initConnection();
|
||||||
Yii::endProfile($token, __METHOD__);
|
Yii::endProfile($token, __METHOD__);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Yii::endProfile($token, __METHOD__);
|
Yii::endProfile($token, __METHOD__);
|
||||||
@@ -253,4 +259,14 @@ class Connection extends Component
|
|||||||
$this->_databases = [];
|
$this->_databases = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the DB connection.
|
||||||
|
* This method is invoked right after the DB connection is established.
|
||||||
|
* The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
|
||||||
|
*/
|
||||||
|
protected function initConnection()
|
||||||
|
{
|
||||||
|
$this->trigger(self::EVENT_AFTER_OPEN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -91,28 +91,22 @@ class Session extends \yii\web\Session
|
|||||||
parent::regenerateID(false);
|
parent::regenerateID(false);
|
||||||
$newID = session_id();
|
$newID = session_id();
|
||||||
|
|
||||||
$query = new Query;
|
$collection = $this->db->getCollection($this->sessionCollection);
|
||||||
$row = $query->from($this->sessionCollection)
|
$row = $collection->findOne(['id' => $oldID]);
|
||||||
->where(['id' => $oldID])
|
if ($row !== null) {
|
||||||
->one($this->db);
|
|
||||||
if ($row !== false) {
|
|
||||||
if ($deleteOldSession) {
|
if ($deleteOldSession) {
|
||||||
$this->db->getCollection($this->sessionCollection)
|
$collection->update(['id' => $oldID], ['id' => $newID]);
|
||||||
->update(['_id' => $row['_id']], ['id' => $newID]);
|
|
||||||
} else {
|
} else {
|
||||||
unset($row['_id']);
|
unset($row['_id']);
|
||||||
$row['id'] = $newID;
|
$row['id'] = $newID;
|
||||||
$this->db->getCollection($this->sessionCollection)
|
$collection->insert($row);
|
||||||
->insert($row);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// shouldn't reach here normally
|
// shouldn't reach here normally
|
||||||
$this->db->getCollection($this->sessionCollection)
|
$collection->insert([
|
||||||
->insert([
|
'id' => $newID,
|
||||||
'id' => $newID,
|
'expire' => time() + $this->getTimeout()
|
||||||
'expire' => time() + $this->getTimeout(),
|
]);
|
||||||
'data' => '',
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,15 +118,15 @@ class Session extends \yii\web\Session
|
|||||||
*/
|
*/
|
||||||
public function readSession($id)
|
public function readSession($id)
|
||||||
{
|
{
|
||||||
$query = new Query;
|
$collection = $this->db->getCollection($this->sessionCollection);
|
||||||
$row = $query->select(['data'])
|
$doc = $collection->findOne(
|
||||||
->from($this->sessionCollection)
|
[
|
||||||
->where([
|
'id' => $id,
|
||||||
'expire' => ['$gt' => time()],
|
'expire' => ['$gt' => time()],
|
||||||
'id' => $id
|
],
|
||||||
])
|
['data' => 1, '_id' => 0]
|
||||||
->one($this->db);
|
);
|
||||||
return $row === false ? '' : $row['data'];
|
return isset($doc['data']) ? $doc['data'] : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -147,23 +141,15 @@ class Session extends \yii\web\Session
|
|||||||
// exception must be caught in session write handler
|
// exception must be caught in session write handler
|
||||||
// http://us.php.net/manual/en/function.session-set-save-handler.php
|
// http://us.php.net/manual/en/function.session-set-save-handler.php
|
||||||
try {
|
try {
|
||||||
$expire = time() + $this->getTimeout();
|
$this->db->getCollection($this->sessionCollection)->update(
|
||||||
$query = new Query;
|
['id' => $id],
|
||||||
$exists = $query->select(['id'])
|
[
|
||||||
->from($this->sessionCollection)
|
'id' => $id,
|
||||||
->where(['id' => $id])
|
'data' => $data,
|
||||||
->one($this->db);
|
'expire' => time() + $this->getTimeout(),
|
||||||
if ($exists === false) {
|
],
|
||||||
$this->db->getCollection($this->sessionCollection)
|
['upsert' => true]
|
||||||
->insert([
|
);
|
||||||
'id' => $id,
|
|
||||||
'data' => $data,
|
|
||||||
'expire' => $expire,
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
$this->db->getCollection($this->sessionCollection)
|
|
||||||
->update(['id' => $id], ['data' => $data, 'expire' => $expire]);
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
if (YII_DEBUG) {
|
if (YII_DEBUG) {
|
||||||
echo $e->getMessage();
|
echo $e->getMessage();
|
||||||
@@ -182,8 +168,10 @@ class Session extends \yii\web\Session
|
|||||||
*/
|
*/
|
||||||
public function destroySession($id)
|
public function destroySession($id)
|
||||||
{
|
{
|
||||||
$this->db->getCollection($this->sessionCollection)
|
$this->db->getCollection($this->sessionCollection)->remove(
|
||||||
->remove(['id' => $id]);
|
['id' => $id],
|
||||||
|
['justOne' => true]
|
||||||
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,9 +184,7 @@ class Session extends \yii\web\Session
|
|||||||
public function gcSession($maxLifetime)
|
public function gcSession($maxLifetime)
|
||||||
{
|
{
|
||||||
$this->db->getCollection($this->sessionCollection)
|
$this->db->getCollection($this->sessionCollection)
|
||||||
->remove([
|
->remove(['expire' => ['$lt' => time()]]);
|
||||||
'expire' => ['$lt' => time()]
|
|
||||||
]);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user