@@ -93,6 +93,16 @@ public function init()
93
93
$ this ->trigger (self ::EVENT_INIT );
94
94
}
95
95
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
+
96
106
/**
97
107
* {@inheritdoc}
98
108
*/
@@ -133,7 +143,7 @@ public function prepare()
133
143
*/
134
144
public function all ($ db = null )
135
145
{
136
- return parent ::all ($ db === null ? $ this ->modelClass :: GetDb () : null );
146
+ return parent ::all ($ this ->finalConObj ( $ db ) );
137
147
}
138
148
139
149
/**
@@ -146,7 +156,7 @@ public function all($db = null)
146
156
*/
147
157
public function one ($ db = null )
148
158
{
149
- $ row = parent ::one ($ db === null ? $ this ->modelClass :: GetDb () : null );
159
+ $ row = parent ::one ($ this ->finalConObj ( $ db ) );
150
160
if ($ row !== false ) {
151
161
$ models = $ this ->populate ([$ row ]);
152
162
return reset ($ models ) ?: null ;
@@ -167,7 +177,7 @@ public function one($db = null)
167
177
*/
168
178
public function modify ($ update , $ options = [], $ db = null )
169
179
{
170
- $ row = parent ::modify ($ update , $ options , $ db === null ? $ this ->modelClass :: GetDb () : null );
180
+ $ row = parent ::modify ($ update , $ options , $ this ->finalConObj ( $ db ) );
171
181
if ($ row !== null ) {
172
182
$ models = $ this ->populate ([$ row ]);
173
183
return reset ($ models ) ?: null ;
@@ -182,15 +192,12 @@ public function modify($update, $options = [], $db = null)
182
192
*/
183
193
public function getCollection ($ db = null )
184
194
{
185
- /* @var $modelClass ActiveRecord */
186
- if ($ db === null ) {
187
- $ db = $ this ->modelClass ::getDb ();
188
- }
189
195
if ($ this ->from === null ) {
190
- $ this ->from = $ this ->modelClass ::collectionName ();
196
+ $ modelClass = $ this ->modelClass ;
197
+ $ this ->from = $ modelClass ::collectionName ();
191
198
}
192
199
193
- return $ db ->getCollection ($ this ->from );
200
+ return $ this -> finalConObj ( $ db ) ->getCollection ($ this ->from );
194
201
}
195
202
196
203
/**
0 commit comments