From 61cacdfc81294a5911eab81c67732c33c5968969 Mon Sep 17 00:00:00 2001 From: Alexander Mohorev <dev.mohorev@gmail.com> Date: Tue, 4 Mar 2014 12:36:05 +0300 Subject: [PATCH] add findAndModify() method to the Collection class --- extensions/mongodb/Collection.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/extensions/mongodb/Collection.php b/extensions/mongodb/Collection.php index 38144bc..6f595f8 100644 --- a/extensions/mongodb/Collection.php +++ b/extensions/mongodb/Collection.php @@ -260,7 +260,7 @@ class Collection extends Object } /** - * Returns a a single document. + * Returns 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. @@ -272,6 +272,32 @@ class Collection extends Object } /** + * Updates a document and returns it. + * @param array $condition query condition + * @param array $update update criteria + * @param array $fields fields to be returned + * @param array $options list of options in format: optionName => optionValue. + * @return array|null the original document, or the modified document when $options['new'] is set. + * @throws Exception on failure. + * @see http://www.php.net/manual/en/mongocollection.findandmodify.php + */ + public function findAndModify($condition, $update, $fields = [], $options = []) + { + $condition = $this->buildCondition($condition); + $token = $this->composeLogToken('findAndModify', [$condition, $update, $fields, $options]); + Yii::info($token, __METHOD__); + try { + Yii::beginProfile($token, __METHOD__); + $result = $this->mongoCollection->findAndModify($condition, $update, $fields, $options); + Yii::endProfile($token, __METHOD__); + return $result; + } catch (\Exception $e) { + Yii::endProfile($token, __METHOD__); + throw new Exception($e->getMessage(), (int)$e->getCode(), $e); + } + } + + /** * Inserts new data into collection. * @param array|object $data data to be inserted. * @param array $options list of options in format: optionName => optionValue. -- libgit2 0.27.1