@@ -845,6 +845,44 @@ public function findOne($criteria = array(), $fields = array())
845
845
return $ this ->populateRecord ($ record , true , $ project === array () ? false : true );
846
846
}
847
847
848
+ /**
849
+ * Find one record, modifies, and returns it.
850
+ * @return null|EMongoDocument
851
+ */
852
+ public function findAndModify ($ criteria = array (), $ updateDoc = array (), $ fields = array (), $ options = array ())
853
+ {
854
+ $ this ->trace (__FUNCTION__ );
855
+
856
+ if ($ criteria instanceof EMongoCriteria) {
857
+ $ criteria = $ criteria ->getCondition ();
858
+ }
859
+ $ c = $ this ->getDbCriteria ();
860
+
861
+ $ criteria = $ this ->mergeCriteria (isset ($ c ['condition ' ]) ? $ c ['condition ' ] : array (), $ criteria );
862
+ $ fields = $ this ->mergeCriteria (isset ($ c ['project ' ]) ? $ c ['project ' ] : array (), $ fields );
863
+ $ options = array_merge ($ this ->getDbConnection ()->getDefaultWriteConcern (), $ options );
864
+
865
+ if (YII_DEBUG ) {
866
+ Yii::trace ('Executing findAndModify: { $query: ' . json_encode ($ criteria ) . ', $document: ' . json_encode ($ updateDoc ) . ', $fields: ' . json_encode ($ fields ) . ', $options: ' . json_encode ($ options ) . '} ' , 'extensions.MongoYii.EMongoDocument ' );
867
+ }
868
+
869
+ if ($ this ->getDbConnection ()->enableProfiling ) {
870
+ $ token = 'extensions.MongoYii.EMongoDocument.query. ' . $ this ->collectionName () . '.findAndModify({ $query: ' . json_encode ($ criteria ) . ', $document: ' . json_encode ($ updateDoc ) . ', $fields: ' . json_encode ($ fields ) . ', $options: ' . json_encode ($ options ) . '}) ' ;
871
+ Yii::beginProfile ($ token , 'extensions.MongoYii.EMongoDocument.findAndModify ' );
872
+ }
873
+
874
+ $ result = $ this ->getCollection ()->findAndModify ($ criteria , $ updateDoc , $ fields , $ options );
875
+
876
+ if ($ this ->getDbConnection ()->enableProfiling ) {
877
+ Yii::enableProfile ($ token , 'extensions.MongoYii.EMongoDocument.findAndModify ' );
878
+ }
879
+
880
+ if ($ result === null ) {
881
+ return null ;
882
+ }
883
+ return $ this ->populateRecord ($ result , true , $ fields === array () ? false : true );
884
+ }
885
+
848
886
/**
849
887
* Alias of find
850
888
* @param array $criteria
0 commit comments