|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Models\Sql; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Reactor\Models\Sql\DataObjects\Result\Row; |
|
19
|
|
|
use O2System\Reactor\Models\Sql\Traits\FinderTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Model |
|
23
|
|
|
* |
|
24
|
|
|
* @package O2System\Reactor\Models\Sql |
|
25
|
|
|
*/ |
|
26
|
|
|
class Model |
|
27
|
|
|
{ |
|
28
|
|
|
use FinderTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* AbstractModel::$db |
|
32
|
|
|
* |
|
33
|
|
|
* Database connection instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @var \O2System\Database\Sql\Abstracts\AbstractConnection |
|
36
|
|
|
*/ |
|
37
|
|
|
public $db = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* AbstractModel::$qb |
|
41
|
|
|
* |
|
42
|
|
|
* Database query builder instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @var \O2System\Database\Sql\Abstracts\AbstractQueryBuilder |
|
45
|
|
|
*/ |
|
46
|
|
|
public $qb = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Model Table |
|
50
|
|
|
* |
|
51
|
|
|
* @access public |
|
52
|
|
|
* @type string |
|
53
|
|
|
*/ |
|
54
|
|
|
public $table = null; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Model Table Columns |
|
58
|
|
|
* |
|
59
|
|
|
* @access public |
|
60
|
|
|
* @type array |
|
61
|
|
|
*/ |
|
62
|
|
|
public $fields = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Model Table Primary Key |
|
66
|
|
|
* |
|
67
|
|
|
* @access public |
|
68
|
|
|
* @type string |
|
69
|
|
|
*/ |
|
70
|
|
|
public $primaryKey = 'id'; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Model Table Primary Keys |
|
74
|
|
|
* |
|
75
|
|
|
* @access public |
|
76
|
|
|
* @type array |
|
77
|
|
|
*/ |
|
78
|
|
|
public $primaryKeys = []; |
|
79
|
|
|
/** |
|
80
|
|
|
* Model Result |
|
81
|
|
|
* |
|
82
|
|
|
* @var \O2System\Reactor\Models\Sql\DataObjects\Result |
|
83
|
|
|
*/ |
|
84
|
|
|
public $result; |
|
85
|
|
|
/** |
|
86
|
|
|
* Model Result Row |
|
87
|
|
|
* |
|
88
|
|
|
* @var \O2System\Reactor\Models\Sql\DataObjects\Result\Row |
|
89
|
|
|
*/ |
|
90
|
|
|
public $row; |
|
91
|
|
|
/** |
|
92
|
|
|
* List of library valid sub models |
|
93
|
|
|
* |
|
94
|
|
|
* @access protected |
|
95
|
|
|
* |
|
96
|
|
|
* @type array driver classes list |
|
97
|
|
|
*/ |
|
98
|
|
|
protected $validSubModels = []; |
|
99
|
|
|
|
|
100
|
|
|
// ------------------------------------------------------------------------ |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* AbstractModel::__construct |
|
104
|
|
|
*/ |
|
105
|
|
|
public function __construct() |
|
106
|
|
|
{ |
|
107
|
|
|
// Set database connection |
|
108
|
|
|
if (method_exists(database(), 'loadConnection')) { |
|
109
|
|
|
if ($this->db = database()->loadConnection('default')) { |
|
110
|
|
|
$this->qb = $this->db->getQueryBuilder(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// Set database table |
|
115
|
|
|
if (empty($this->table)) { |
|
116
|
|
|
$modelClassName = get_called_class(); |
|
117
|
|
|
$modelClassName = get_class_name($modelClassName); |
|
118
|
|
|
$this->table = underscore($modelClassName); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// Fetch sub-models |
|
122
|
|
|
$this->fetchSubModels(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* AbstractModel::fetchSubModels |
|
127
|
|
|
* |
|
128
|
|
|
* @access protected |
|
129
|
|
|
* @final this method cannot be overwritten. |
|
130
|
|
|
* |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
final protected function fetchSubModels() |
|
134
|
|
|
{ |
|
135
|
|
|
$reflection = new \ReflectionClass(get_called_class()); |
|
136
|
|
|
|
|
137
|
|
|
// Define called model class filepath |
|
138
|
|
|
$filePath = $reflection->getFileName(); |
|
139
|
|
|
|
|
140
|
|
|
// Define filename for used as subdirectory name |
|
141
|
|
|
$filename = pathinfo($filePath, PATHINFO_FILENAME); |
|
142
|
|
|
|
|
143
|
|
|
// Get model class directory name |
|
144
|
|
|
$dirName = dirname($filePath) . DIRECTORY_SEPARATOR; |
|
145
|
|
|
|
|
146
|
|
|
if ($filename === 'Model') { |
|
147
|
|
|
$subModelsDirName = dirname($dirName) . DIRECTORY_SEPARATOR . 'Models' . DIRECTORY_SEPARATOR; |
|
148
|
|
|
|
|
149
|
|
|
if (is_dir($subModelsDirName)) { |
|
150
|
|
|
$subModelPath = $subModelsDirName; |
|
151
|
|
|
} |
|
152
|
|
|
} elseif (is_dir($subModelsDirName = $dirName . $filename . DIRECTORY_SEPARATOR)) { |
|
153
|
|
|
$subModelPath = $subModelsDirName; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if (isset($subModelPath)) { |
|
157
|
|
|
loader()->addNamespace($reflection->name, $subModelPath); |
|
158
|
|
|
|
|
159
|
|
|
foreach (glob($subModelPath . '*.php') as $filepath) { |
|
160
|
|
|
$this->validSubModels[ camelcase(pathinfo($filepath, PATHINFO_FILENAME)) ] = $filepath; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// ------------------------------------------------------------------------ |
|
166
|
|
|
|
|
167
|
|
|
final public static function __callStatic($method, array $arguments = []) |
|
168
|
|
|
{ |
|
169
|
|
|
static $modelInstance; |
|
170
|
|
|
|
|
171
|
|
|
if (empty($modelInstance)) { |
|
172
|
|
|
$modelClassName = get_called_class(); |
|
173
|
|
|
$modelInstance = new $modelClassName(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (method_exists($modelInstance, $method)) { |
|
177
|
|
|
return call_user_func_array([&$modelInstance, $method], $arguments); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
final public function __call($method, array $arguments = []) |
|
184
|
|
|
{ |
|
185
|
|
|
if (method_exists($this, $method)) { |
|
186
|
|
|
return call_user_func_array([&$this, $method], $arguments); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// ------------------------------------------------------------------------ |
|
193
|
|
|
|
|
194
|
|
|
public function __get($property) |
|
195
|
|
|
{ |
|
196
|
|
|
if ($this->row instanceof Row) { |
|
|
|
|
|
|
197
|
|
|
if ($this->row->offsetExists($property)) { |
|
198
|
|
|
return $this->row->offsetGet($property); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
if (empty($get[ $property ])) { |
|
|
|
|
|
|
203
|
|
|
if (o2system()->hasService($property)) { |
|
|
|
|
|
|
204
|
|
|
return o2system()->getService($property); |
|
|
|
|
|
|
205
|
|
|
} elseif (array_key_exists($property, $this->validSubModels)) { |
|
206
|
|
|
return $this->loadSubModel($property); |
|
207
|
|
|
} elseif (o2system()->__isset($property)) { |
|
208
|
|
|
return o2system()->__get($property); |
|
209
|
|
|
} elseif (models()->__isset($property)) { |
|
|
|
|
|
|
210
|
|
|
return models()->get($property); |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
// ------------------------------------------------------------------------ |
|
216
|
|
|
|
|
217
|
|
|
final protected function loadSubModel($model) |
|
218
|
|
|
{ |
|
219
|
|
|
if (is_file($this->validSubModels[ $model ])) { |
|
220
|
|
|
$className = '\\' . get_called_class() . '\\' . ucfirst($model); |
|
221
|
|
|
$className = str_replace('\Base\\Model', '\Models', $className); |
|
222
|
|
|
|
|
223
|
|
|
if (class_exists($className)) { |
|
224
|
|
|
$this->{$model} = new $className(); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $this->{$model}; |
|
229
|
|
|
} |
|
230
|
|
|
} |