Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit f3acd7c

Browse files
author
Nik Barham
committed
Improving binding of namespace to dbconnection
1 parent 5f0dde3 commit f3acd7c

File tree

2 files changed

+91
-84
lines changed

2 files changed

+91
-84
lines changed

src/Automatorm/Orm/Model.php

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,49 @@ class Model implements \JsonSerializable
2626
// Flags
2727
const COUNT_ONLY = 1;
2828

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
3232

3333
/* PUBLIC CONSTRUCTION METHODS */
3434
public static function get($id, $force_refresh = false)
3535
{
3636
return static::factoryObjectCache($id, null, null, $force_refresh);
3737
}
3838

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+
3950
// Find a single(!) object via an arbitary $where clause
4051
public static function find($where, $cacheable = false)
4152
{
53+
if (!$cacheable) return static::factory($where, null, null, ['limit' => 1], true);
54+
4255
// 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());
5859

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;
6967
}
7068

69+
// Generate and save result
70+
$result = static::factory($where, null, null, ['limit' => 1], true);
71+
Model::$instance[$dbconnection][$table][$key] = $result;
7172
return $result;
7273
}
7374

@@ -79,21 +80,21 @@ public static function findAll($where = [], $options = [])
7980

8081
/* FACTORY METHODS */
8182
// 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)
8384
{
8485
// Some defaults
85-
if (!$database) $database = static::$dbconnection;
86+
if (!$dbconnection) $dbconnection = static::getConnection();
8687

8788
// Figure out the base class and table we need based on current context
88-
$schema = Schema::get($database);
89+
$schema = Schema::get($dbconnection);
8990
list($class, $table) = $schema->guessContext($class_or_table_name ?: get_called_class());
9091

9192
// Get data from database
92-
$data = Model::factoryData($where, $table, $database, $options);
93-
93+
$data = Model::factoryData($where, $table, $dbconnection, $options);
94+
9495
// If we're in one object mode, and have no data, return null rather than an empty Model_Collection!
9596
if ($single_result and !$data) return null;
96-
97+
9798
// New container for the results
9899
$collection = new Collection();
99100

@@ -105,7 +106,7 @@ final public static function factory($where, $class_or_table_name = null, $datab
105106
$obj = new $class($data_obj);
106107

107108
// Store it in the object cache.
108-
Model::$instance[$database][$table][$row['id']] = $obj;
109+
Model::$instance[$dbconnection][$table][$row['id']] = $obj;
109110

110111
// Call Model objects _init() function - this is to avoid recursion issues with object's natural constructor and the cache above
111112
$obj->_init();
@@ -126,23 +127,23 @@ protected static function _subclass(Data $data)
126127
return get_called_class();
127128
}
128129

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)
130131
{
131-
if (!$database) $database = static::$dbconnection;
132-
$schema = Schema::get($database);
132+
if (!$dbconnection) $dbconnection = static::getConnection();
133+
$schema = Schema::get($dbconnection);
133134
list($class, $table) = $schema->guessContext($class_or_table ?: get_called_class());
134135

135136
// If we have a single id
136137
if (is_numeric($ids)) {
137138
if (!$force_refresh) {
138139
// 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];
141142
}
142143
}
143144

144145
/* 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);
146147

147148
// Else if we have an array of ids
148149
} elseif (is_array($ids)) {
@@ -159,8 +160,8 @@ final public static function factoryObjectCache($ids, $class_or_table = null, $d
159160
// If we succeed, remove it from the list of ids to search for in the database
160161
if (!$force_refresh) {
161162
// 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];
164165
unset($ids[$key]);
165166
}
166167
}
@@ -169,7 +170,7 @@ final public static function factoryObjectCache($ids, $class_or_table = null, $d
169170
// For any ids we failed to pull out the cache, pull them from the database instead
170171
if (count($ids) > 0)
171172
{
172-
$newresults = static::factory(['id' => $ids], $class_or_table, $database);
173+
$newresults = static::factory(['id' => $ids], $class_or_table, $dbconnection);
173174
$collection = $collection->merge($newresults);
174175
}
175176

@@ -183,7 +184,7 @@ final public static function factoryObjectCache($ids, $class_or_table = null, $d
183184
}
184185

185186
// 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)
187188
{
188189
// Select * from $table where $where
189190
$build = QueryBuilder::select($table)->where($where);
@@ -211,14 +212,14 @@ final public static function factoryData($where, $table, $database, array $optio
211212
}
212213
}
213214

214-
$query = new Query($database);
215+
$query = new Query($dbconnection);
215216
list($data) = $query->sql($build)->execute();
216217

217218
return $data;
218219
}
219220

220221
// 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)
222223
{
223224
// Select * from $table where $where
224225
$build = QueryBuilder::count($table)->where($where);
@@ -232,7 +233,7 @@ final public static function factoryDataCount($where, $table, $database, array $
232233
}
233234
}
234235

235-
$query = new Query($database);
236+
$query = new Query($dbconnection);
236237
list($data) = $query->sql($build)->execute();
237238

238239
return $data;
@@ -242,8 +243,9 @@ final public static function factoryDataCount($where, $table, $database, array $
242243
// For 'foreign' tables, a parent object must be supplied.
243244
public static function newData(Model $parent_object = null)
244245
{
246+
$dbconnection = static::getConnection();
245247
// Get the schema for the current class/table
246-
$schema = Schema::get(static::$dbconnection);
248+
$schema = Schema::get($dbconnection);
247249
list($class, $table) = $schema->guessContext(get_called_class());
248250

249251
// Make a new blank data object
@@ -252,25 +254,25 @@ public static function newData(Model $parent_object = null)
252254
$table_schema = $schema->getTable($table);
253255
// "Foreign" tables use a "parent" table for their primary key. We need that parent object for it's id.
254256
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]);
256258
$model_data->id = $parent_object->id;
257259
}
258260

259261
return $model_data;
260262
}
261263

262-
public static function clearInstanceCache($database = null, $table = null, $id = null)
264+
public static function clearInstanceCache($dbconnection = null, $table = null, $id = null)
263265
{
264266
if (isset($id)) {
265-
unset(static::$instance[$database][$table][$id]);
267+
unset(static::$instance[$dbconnection][$table][$id]);
266268
return;
267269
}
268270
if (isset($table)) {
269-
unset(static::$instance[$database][$table]);
271+
unset(static::$instance[$dbconnection][$table]);
270272
return;
271273
}
272-
if (isset($database)) {
273-
unset(static::$instance[$database]);
274+
if (isset($dbconnection)) {
275+
unset(static::$instance[$dbconnection]);
274276
return;
275277
}
276278
unset(static::$instance);

src/Automatorm/Orm/Schema.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@
1010

1111
class Schema
1212
{
13-
const CURRENT_VERSION = 3;
14-
15-
public static $object_list = array();
13+
const CURRENT_VERSION = 4;
14+
15+
// Cache Bridge
16+
protected static $cache = '\\Automatorm\\Cache\\HodgePodgeCache';
17+
public static function registerCache($cache)
18+
{
19+
static::$cache = $cache;
20+
}
1621

22+
// Singleton
23+
public static $object_list = [];
24+
public static $namespaces = [];
1725
public static function get($dbconnection)
1826
{
1927
if (!static::$object_list[$dbconnection]) {
@@ -22,52 +30,49 @@ public static function get($dbconnection)
2230

2331
return static::$object_list[$dbconnection];
2432
}
25-
26-
protected $model;
27-
protected $database;
28-
protected $namespace;
29-
protected $version;
30-
31-
protected function __construct($model, $database, $namespace) {
32-
$this->model = $model;
33-
$this->database = $database;
34-
$this->namespace = $namespace;
35-
$this->version = static::CURRENT_VERSION;
36-
}
37-
38-
protected static $cache = '\\Automatorm\\Cache\\HodgePodgeCache';
39-
public static function registerCache($cache)
40-
{
41-
static::$cache = $cache;
42-
}
4333

4434
public static function generate($dbconnection = 'default', $namespace = 'models', $cachebust = false)
4535
{
46-
$db = Connection::get($dbconnection);
47-
$key = 'schema_' . md5($dbconnection . $namespace . $db->database . static::CURRENT_VERSION);
48-
49-
if (is_object(static::$cache)) {
50-
$cache = static::$cache;
51-
} else {
52-
$cache = new static::$cache;
53-
}
36+
// Register namespace with connection
37+
static::$namespaces[$namespace] = $dbconnection;
5438

39+
// Get schema from cache
40+
$cache = is_object(static::$cache) ? static::$cache : new static::$cache();
41+
$key = 'schema_' . md5($dbconnection . $namespace . static::CURRENT_VERSION);
5542
$obj = $cache->get($key);
5643

44+
// Expire old versions of Schema
5745
if ($obj && $obj->version != static::CURRENT_VERSION)
5846
{
5947
unset($obj);
6048
}
6149

50+
// If no cache, generate the schema
6251
if ($cachebust or !$obj) {
6352
$model = static::generateSchema($dbconnection);
6453
$obj = new static($model, $dbconnection, $namespace);
6554
$cache->put($key, $obj, 60 * 60 * 24 * 7);
6655
}
6756

57+
// Return schema object
6858
return static::$object_list[$dbconnection] = $obj;
6959
}
7060

61+
///////////////////////////////////////////////////////////////////////////
62+
63+
protected $model;
64+
protected $database;
65+
protected $namespace;
66+
protected $version;
67+
68+
protected function __construct($model, $database, $namespace) {
69+
$this->model = $model;
70+
$this->database = $database;
71+
$this->namespace = $namespace;
72+
$this->version = static::CURRENT_VERSION;
73+
}
74+
75+
// Generate Schema
7176
public static function generateSchema($dbconnection)
7277
{
7378
$model = [];

0 commit comments

Comments
 (0)