Commit 0e023f53 by Paul Klimov

Merge pull request #2480 from mongosoft/mongodb

Mongodb extension refactorign
parents 2a7df8f1 c65b171e
...@@ -37,7 +37,7 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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);
......
...@@ -260,6 +260,18 @@ class Collection extends Object ...@@ -260,6 +260,18 @@ class Collection extends Object
} }
/** /**
* 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.
* @param array $options list of options in format: optionName => optionValue. * @param array $options list of options in format: optionName => optionValue.
...@@ -372,11 +384,12 @@ class Collection extends Object ...@@ -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 {
......
...@@ -73,6 +73,11 @@ use Yii; ...@@ -73,6 +73,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
* *
* Correct syntax is: * Correct syntax is:
...@@ -233,6 +238,7 @@ class Connection extends Component ...@@ -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 ...@@ -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);
}
} }
\ No newline at end of file
...@@ -91,27 +91,21 @@ class Session extends \yii\web\Session ...@@ -91,27 +91,21 @@ 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 ...@@ -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 ...@@ -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)
->where(['id' => $id])
->one($this->db);
if ($exists === false) {
$this->db->getCollection($this->sessionCollection)
->insert([
'id' => $id, 'id' => $id,
'data' => $data, 'data' => $data,
'expire' => $expire, 'expire' => time() + $this->getTimeout(),
]); ],
} else { ['upsert' => true]
$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 ...@@ -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 ...@@ -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;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment