@@ -26,48 +26,49 @@ class Model implements \JsonSerializable
26
26
// Flags
27
27
const COUNT_ONLY = 1 ;
28
28
29
- public static $ dbconnection = ' default ' ; // Override database connection associated with this class
30
- public static $ tablename ; // Override table associated with this class
31
- protected static $ instance ; // An internal store of already created objects so that objects for each row only get created once
29
+ public static $ dbconnection; // Override database connection associated with this class
30
+ public static $ tablename ; // Override table associated with this class
31
+ protected static $ instance ; // An internal store of already created objects so that objects for each row only get created once
32
32
33
33
/* PUBLIC CONSTRUCTION METHODS */
34
34
public static function get ($ id , $ force_refresh = false )
35
35
{
36
36
return static ::factoryObjectCache ($ id , null , null , $ force_refresh );
37
37
}
38
38
39
+ public static function getConnection ()
40
+ {
41
+ if (static ::$ dbconnection ) return static ::$ dbconnection ;
42
+
43
+ $ class = get_called_class ();
44
+ $ namespace = substr ($ class , 0 , strrpos ($ class , '\\' ));
45
+ if (key_exists ($ namespace , Schema::$ namespaces )) return Schema::$ namespaces [$ namespace ];
46
+
47
+ return 'default ' ;
48
+ }
49
+
39
50
// Find a single(!) object via an arbitary $where clause
40
51
public static function find ($ where , $ cacheable = false )
41
52
{
53
+ if (!$ cacheable ) return static ::factory ($ where , null , null , ['limit ' => 1 ], true );
54
+
42
55
// If result is cacheable, look in the cache
43
- if ($ cacheable )
44
- {
45
- // Get table/class data
46
- if (!$ database ) $ database = static ::$ dbconnection ;
47
- $ schema = Schema::get ($ database );
48
- list ($ class , $ table ) = $ schema ->guessContext ($ class_or_table ?: get_called_class ());
49
-
50
- // Hash where clause
51
- $ key = md5 (serialize ($ where ));
52
-
53
- // Look in cache
54
- if (isset (Model::$ instance [$ database ][$ table ][$ key ])) {
55
- $ result = Model::$ instance [$ database ][$ table ][$ key ];
56
- }
57
- }
56
+ $ dbconnection = static ::getConnection ();
57
+ $ schema = Schema::get ($ dbconnection );
58
+ list ($ class , $ table ) = $ schema ->guessContext ($ class_or_table ?: get_called_class ());
58
59
59
- // No cache or cache miss
60
- if (!$ result )
61
- {
62
- $ result = static ::factory ($ where , null , null , ['limit ' => 1 ], true );
63
-
64
- // If caching, store in cache
65
- if ($ cacheable )
66
- {
67
- Model::$ instance [$ database ][$ table ][$ key ] = $ result ;
68
- }
60
+ // Hash where clause
61
+ $ key = md5 (serialize ($ where ));
62
+
63
+ // Look in cache, and return a result if we find one
64
+ if (isset (Model::$ instance [$ dbconnection ][$ table ][$ key ])) {
65
+ if ($ result = Model::$ instance [$ dbconnection ][$ table ][$ key ])
66
+ return $ result ;
69
67
}
70
68
69
+ // Generate and save result
70
+ $ result = static ::factory ($ where , null , null , ['limit ' => 1 ], true );
71
+ Model::$ instance [$ dbconnection ][$ table ][$ key ] = $ result ;
71
72
return $ result ;
72
73
}
73
74
@@ -79,21 +80,21 @@ public static function findAll($where = [], $options = [])
79
80
80
81
/* FACTORY METHODS */
81
82
// Build an appropriate Model object based on id and class/table name
82
- final public static function factory ($ where , $ class_or_table_name = null , $ database = null , array $ options = null , $ single_result = false )
83
+ final public static function factory ($ where , $ class_or_table_name = null , $ dbconnection = null , array $ options = null , $ single_result = false )
83
84
{
84
85
// Some defaults
85
- if (!$ database ) $ database = static ::$ dbconnection ;
86
+ if (!$ dbconnection ) $ dbconnection = static ::getConnection () ;
86
87
87
88
// Figure out the base class and table we need based on current context
88
- $ schema = Schema::get ($ database );
89
+ $ schema = Schema::get ($ dbconnection );
89
90
list ($ class , $ table ) = $ schema ->guessContext ($ class_or_table_name ?: get_called_class ());
90
91
91
92
// Get data from database
92
- $ data = Model::factoryData ($ where , $ table , $ database , $ options );
93
-
93
+ $ data = Model::factoryData ($ where , $ table , $ dbconnection , $ options );
94
+
94
95
// If we're in one object mode, and have no data, return null rather than an empty Model_Collection!
95
96
if ($ single_result and !$ data ) return null ;
96
-
97
+
97
98
// New container for the results
98
99
$ collection = new Collection ();
99
100
@@ -105,7 +106,7 @@ final public static function factory($where, $class_or_table_name = null, $datab
105
106
$ obj = new $ class ($ data_obj );
106
107
107
108
// Store it in the object cache.
108
- Model::$ instance [$ database ][$ table ][$ row ['id ' ]] = $ obj ;
109
+ Model::$ instance [$ dbconnection ][$ table ][$ row ['id ' ]] = $ obj ;
109
110
110
111
// Call Model objects _init() function - this is to avoid recursion issues with object's natural constructor and the cache above
111
112
$ obj ->_init ();
@@ -126,23 +127,23 @@ protected static function _subclass(Data $data)
126
127
return get_called_class ();
127
128
}
128
129
129
- final public static function factoryObjectCache ($ ids , $ class_or_table = null , $ database = null , $ force_refresh = false )
130
+ final public static function factoryObjectCache ($ ids , $ class_or_table = null , $ dbconnection = null , $ force_refresh = false )
130
131
{
131
- if (!$ database ) $ database = static ::$ dbconnection ;
132
- $ schema = Schema::get ($ database );
132
+ if (!$ dbconnection ) $ dbconnection = static ::getConnection () ;
133
+ $ schema = Schema::get ($ dbconnection );
133
134
list ($ class , $ table ) = $ schema ->guessContext ($ class_or_table ?: get_called_class ());
134
135
135
136
// If we have a single id
136
137
if (is_numeric ($ ids )) {
137
138
if (!$ force_refresh ) {
138
139
// Check Model object cache
139
- if (isset (Model::$ instance [$ database ][$ table ][$ ids ])) {
140
- return Model::$ instance [$ database ][$ table ][$ ids ];
140
+ if (isset (Model::$ instance [$ dbconnection ][$ table ][$ ids ])) {
141
+ return Model::$ instance [$ dbconnection ][$ table ][$ ids ];
141
142
}
142
143
}
143
144
144
145
/* Cache miss, so create new object */
145
- return static ::factory (['id ' => $ ids ], $ class_or_table , $ database , ['limit ' => 1 ], true );
146
+ return static ::factory (['id ' => $ ids ], $ class_or_table , $ dbconnection , ['limit ' => 1 ], true );
146
147
147
148
// Else if we have an array of ids
148
149
} elseif (is_array ($ ids )) {
@@ -159,8 +160,8 @@ final public static function factoryObjectCache($ids, $class_or_table = null, $d
159
160
// If we succeed, remove it from the list of ids to search for in the database
160
161
if (!$ force_refresh ) {
161
162
// Check Model object cache
162
- if (isset (Model::$ instance [$ database ][$ table ][$ id ])) {
163
- $ collection [] = Model::$ instance [$ database ][$ table ][$ id ];
163
+ if (isset (Model::$ instance [$ dbconnection ][$ table ][$ id ])) {
164
+ $ collection [] = Model::$ instance [$ dbconnection ][$ table ][$ id ];
164
165
unset($ ids [$ key ]);
165
166
}
166
167
}
@@ -169,7 +170,7 @@ final public static function factoryObjectCache($ids, $class_or_table = null, $d
169
170
// For any ids we failed to pull out the cache, pull them from the database instead
170
171
if (count ($ ids ) > 0 )
171
172
{
172
- $ newresults = static ::factory (['id ' => $ ids ], $ class_or_table , $ database );
173
+ $ newresults = static ::factory (['id ' => $ ids ], $ class_or_table , $ dbconnection );
173
174
$ collection = $ collection ->merge ($ newresults );
174
175
}
175
176
@@ -183,7 +184,7 @@ final public static function factoryObjectCache($ids, $class_or_table = null, $d
183
184
}
184
185
185
186
// Get data from database from which we can construct Model objects
186
- final public static function factoryData ($ where , $ table , $ database , array $ options = null )
187
+ final public static function factoryData ($ where , $ table , $ dbconnection , array $ options = null )
187
188
{
188
189
// Select * from $table where $where
189
190
$ build = QueryBuilder::select ($ table )->where ($ where );
@@ -211,14 +212,14 @@ final public static function factoryData($where, $table, $database, array $optio
211
212
}
212
213
}
213
214
214
- $ query = new Query ($ database );
215
+ $ query = new Query ($ dbconnection );
215
216
list ($ data ) = $ query ->sql ($ build )->execute ();
216
217
217
218
return $ data ;
218
219
}
219
220
220
221
// Get data from database from which we can construct Model objects
221
- final public static function factoryDataCount ($ where , $ table , $ database , array $ options = null )
222
+ final public static function factoryDataCount ($ where , $ table , $ dbconnection , array $ options = null )
222
223
{
223
224
// Select * from $table where $where
224
225
$ build = QueryBuilder::count ($ table )->where ($ where );
@@ -232,7 +233,7 @@ final public static function factoryDataCount($where, $table, $database, array $
232
233
}
233
234
}
234
235
235
- $ query = new Query ($ database );
236
+ $ query = new Query ($ dbconnection );
236
237
list ($ data ) = $ query ->sql ($ build )->execute ();
237
238
238
239
return $ data ;
@@ -242,8 +243,9 @@ final public static function factoryDataCount($where, $table, $database, array $
242
243
// For 'foreign' tables, a parent object must be supplied.
243
244
public static function newData (Model $ parent_object = null )
244
245
{
246
+ $ dbconnection = static ::getConnection ();
245
247
// Get the schema for the current class/table
246
- $ schema = Schema::get (static :: $ dbconnection );
248
+ $ schema = Schema::get ($ dbconnection );
247
249
list ($ class , $ table ) = $ schema ->guessContext (get_called_class ());
248
250
249
251
// Make a new blank data object
@@ -252,25 +254,25 @@ public static function newData(Model $parent_object = null)
252
254
$ table_schema = $ schema ->getTable ($ table );
253
255
// "Foreign" tables use a "parent" table for their primary key. We need that parent object for it's id.
254
256
if ($ table_schema ['type ' ] == 'foreign ' ) {
255
- if (!$ parent_object ) throw new Exception \Model ('NO_PARENT_OBJECT ' , [$ database , $ class , $ table , static ::$ tablename ]);
257
+ if (!$ parent_object ) throw new Exception \Model ('NO_PARENT_OBJECT ' , [$ dbconnection , $ class , $ table , static ::$ tablename ]);
256
258
$ model_data ->id = $ parent_object ->id ;
257
259
}
258
260
259
261
return $ model_data ;
260
262
}
261
263
262
- public static function clearInstanceCache ($ database = null , $ table = null , $ id = null )
264
+ public static function clearInstanceCache ($ dbconnection = null , $ table = null , $ id = null )
263
265
{
264
266
if (isset ($ id )) {
265
- unset(static ::$ instance [$ database ][$ table ][$ id ]);
267
+ unset(static ::$ instance [$ dbconnection ][$ table ][$ id ]);
266
268
return ;
267
269
}
268
270
if (isset ($ table )) {
269
- unset(static ::$ instance [$ database ][$ table ]);
271
+ unset(static ::$ instance [$ dbconnection ][$ table ]);
270
272
return ;
271
273
}
272
- if (isset ($ database )) {
273
- unset(static ::$ instance [$ database ]);
274
+ if (isset ($ dbconnection )) {
275
+ unset(static ::$ instance [$ dbconnection ]);
274
276
return ;
275
277
}
276
278
unset(static ::$ instance );
0 commit comments