@@ -93,6 +93,16 @@ public function init()
9393 $ this ->trigger (self ::EVENT_INIT );
9494 }
9595
96+ /**
97+ * returns mongodb connection object from `modelClass` if `$db` is null otherwise returns `$db`
98+ * @param null|Connection your custom connection object
99+ * @return Connection returns a connection object base on input
100+ */
101+ protected function finalConObj ($ db ){
102+ $ modelClass = $ this ->modelClass ;
103+ return $ db === null ? $ modelClass ::getDb () : $ db ;
104+ }
105+
96106 /**
97107 * {@inheritdoc}
98108 */
@@ -133,7 +143,7 @@ public function prepare()
133143 */
134144 public function all ($ db = null )
135145 {
136- return parent ::all ($ db === null ? $ this ->modelClass :: GetDb () : null );
146+ return parent ::all ($ this ->finalConObj ( $ db ) );
137147 }
138148
139149 /**
@@ -146,7 +156,7 @@ public function all($db = null)
146156 */
147157 public function one ($ db = null )
148158 {
149- $ row = parent ::one ($ db === null ? $ this ->modelClass :: GetDb () : null );
159+ $ row = parent ::one ($ this ->finalConObj ( $ db ) );
150160 if ($ row !== false ) {
151161 $ models = $ this ->populate ([$ row ]);
152162 return reset ($ models ) ?: null ;
@@ -167,7 +177,7 @@ public function one($db = null)
167177 */
168178 public function modify ($ update , $ options = [], $ db = null )
169179 {
170- $ row = parent ::modify ($ update , $ options , $ db === null ? $ this ->modelClass :: GetDb () : null );
180+ $ row = parent ::modify ($ update , $ options , $ this ->finalConObj ( $ db ) );
171181 if ($ row !== null ) {
172182 $ models = $ this ->populate ([$ row ]);
173183 return reset ($ models ) ?: null ;
@@ -182,15 +192,12 @@ public function modify($update, $options = [], $db = null)
182192 */
183193 public function getCollection ($ db = null )
184194 {
185- /* @var $modelClass ActiveRecord */
186- if ($ db === null ) {
187- $ db = $ this ->modelClass ::getDb ();
188- }
189195 if ($ this ->from === null ) {
190- $ this ->from = $ this ->modelClass ::collectionName ();
196+ $ modelClass = $ this ->modelClass ;
197+ $ this ->from = $ modelClass ::collectionName ();
191198 }
192199
193- return $ db ->getCollection ($ this ->from );
200+ return $ this -> finalConObj ( $ db ) ->getCollection ($ this ->from );
194201 }
195202
196203 /**
0 commit comments