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
* 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
......@@ -78,7 +78,7 @@ abstract class ActiveRecord extends BaseActiveRecord
* 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.
......@@ -88,10 +88,6 @@ abstract class ActiveRecord extends BaseActiveRecord
*/
public static function deleteAll($condition = [], $options = [])
{
$options['w'] = 1;
if (!array_key_exists('multiple', $options)) {
$options['multiple'] = true;
}
return static::getCollection()->remove($condition, $options);
}
......@@ -237,8 +233,7 @@ abstract class ActiveRecord extends BaseActiveRecord
$values[$key] = isset($currentAttributes[$key]) ? $currentAttributes[$key] : null;
}
}
$collection = static::getCollection();
$newId = $collection->insert($values);
$newId = static::getCollection()->insert($values);
$this->setAttribute('_id', $newId);
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $value);
......@@ -348,4 +343,4 @@ abstract class ActiveRecord extends BaseActiveRecord
}
return $this->collectionName() === $record->collectionName() && (string)$this->getPrimaryKey() === (string)$record->getPrimaryKey();
}
}
}
\ No newline at end of file
......@@ -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.
* @param array|object $data data to be inserted.
* @param array $options list of options in format: optionName => optionValue.
......@@ -372,11 +384,12 @@ class Collection extends Object
* @param array $options list of options in format: optionName => optionValue.
* @return integer|boolean number of updated documents or whether operation was successful.
* @throws Exception on failure.
* @see http://www.php.net/manual/en/mongocollection.remove.php
*/
public function remove($condition = [], $options = [])
{
$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]);
Yii::info($token, __METHOD__);
try {
......@@ -522,7 +535,7 @@ class Collection extends Object
* Argument will be automatically cast to [[\MongoCode]].
* @param string|array $out output collection name. It could be a string for simple output
* ('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 $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.
......
......@@ -73,6 +73,11 @@ use Yii;
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
*
* Correct syntax is:
......@@ -233,6 +238,7 @@ class Connection extends Component
$options['db'] = $this->defaultDatabaseName;
}
$this->mongoClient = new \MongoClient($this->dsn, $options);
$this->initConnection();
Yii::endProfile($token, __METHOD__);
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
......@@ -253,4 +259,14 @@ class Connection extends Component
$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,28 +91,22 @@ class Session extends \yii\web\Session
parent::regenerateID(false);
$newID = session_id();
$query = new Query;
$row = $query->from($this->sessionCollection)
->where(['id' => $oldID])
->one($this->db);
if ($row !== false) {
$collection = $this->db->getCollection($this->sessionCollection);
$row = $collection->findOne(['id' => $oldID]);
if ($row !== null) {
if ($deleteOldSession) {
$this->db->getCollection($this->sessionCollection)
->update(['_id' => $row['_id']], ['id' => $newID]);
$collection->update(['id' => $oldID], ['id' => $newID]);
} else {
unset($row['_id']);
$row['id'] = $newID;
$this->db->getCollection($this->sessionCollection)
->insert($row);
$collection->insert($row);
}
} else {
// shouldn't reach here normally
$this->db->getCollection($this->sessionCollection)
->insert([
'id' => $newID,
'expire' => time() + $this->getTimeout(),
'data' => '',
]);
$collection->insert([
'id' => $newID,
'expire' => time() + $this->getTimeout()
]);
}
}
......@@ -124,15 +118,15 @@ class Session extends \yii\web\Session
*/
public function readSession($id)
{
$query = new Query;
$row = $query->select(['data'])
->from($this->sessionCollection)
->where([
$collection = $this->db->getCollection($this->sessionCollection);
$doc = $collection->findOne(
[
'id' => $id,
'expire' => ['$gt' => time()],
'id' => $id
])
->one($this->db);
return $row === false ? '' : $row['data'];
],
['data' => 1, '_id' => 0]
);
return isset($doc['data']) ? $doc['data'] : '';
}
/**
......@@ -147,23 +141,15 @@ class Session extends \yii\web\Session
// exception must be caught in session write handler
// http://us.php.net/manual/en/function.session-set-save-handler.php
try {
$expire = time() + $this->getTimeout();
$query = new Query;
$exists = $query->select(['id'])
->from($this->sessionCollection)
->where(['id' => $id])
->one($this->db);
if ($exists === false) {
$this->db->getCollection($this->sessionCollection)
->insert([
'id' => $id,
'data' => $data,
'expire' => $expire,
]);
} else {
$this->db->getCollection($this->sessionCollection)
->update(['id' => $id], ['data' => $data, 'expire' => $expire]);
}
$this->db->getCollection($this->sessionCollection)->update(
['id' => $id],
[
'id' => $id,
'data' => $data,
'expire' => time() + $this->getTimeout(),
],
['upsert' => true]
);
} catch (\Exception $e) {
if (YII_DEBUG) {
echo $e->getMessage();
......@@ -182,8 +168,10 @@ class Session extends \yii\web\Session
*/
public function destroySession($id)
{
$this->db->getCollection($this->sessionCollection)
->remove(['id' => $id]);
$this->db->getCollection($this->sessionCollection)->remove(
['id' => $id],
['justOne' => true]
);
return true;
}
......@@ -196,9 +184,7 @@ class Session extends \yii\web\Session
public function gcSession($maxLifetime)
{
$this->db->getCollection($this->sessionCollection)
->remove([
'expire' => ['$lt' => time()]
]);
->remove(['expire' => ['$lt' => time()]]);
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