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
|
|
|
use O2System\Reactor\Models\Sql\Traits\ModifierTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Model |
24
|
|
|
* |
25
|
|
|
* @package O2System\Reactor\Models\Sql |
26
|
|
|
*/ |
27
|
|
|
class Model |
28
|
|
|
{ |
29
|
|
|
use FinderTrait; |
30
|
|
|
use ModifierTrait; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* AbstractModel::$db |
34
|
|
|
* |
35
|
|
|
* Database connection instance. |
36
|
|
|
* |
37
|
|
|
* @var \O2System\Database\Sql\Abstracts\AbstractConnection |
38
|
|
|
*/ |
39
|
|
|
public $db = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* AbstractModel::$qb |
43
|
|
|
* |
44
|
|
|
* Database query builder instance. |
45
|
|
|
* |
46
|
|
|
* @var \O2System\Database\Sql\Abstracts\AbstractQueryBuilder |
47
|
|
|
*/ |
48
|
|
|
public $qb = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Model Table |
52
|
|
|
* |
53
|
|
|
* @access public |
54
|
|
|
* @type string |
55
|
|
|
*/ |
56
|
|
|
public $table = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Model Table Columns |
60
|
|
|
* |
61
|
|
|
* @access public |
62
|
|
|
* @type array |
63
|
|
|
*/ |
64
|
|
|
public $fields = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Model Table Primary Key |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* @type string |
71
|
|
|
*/ |
72
|
|
|
public $primaryKey = 'id'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Model Table Primary Keys |
76
|
|
|
* |
77
|
|
|
* @access public |
78
|
|
|
* @type array |
79
|
|
|
*/ |
80
|
|
|
public $primaryKeys = []; |
81
|
|
|
/** |
82
|
|
|
* Model Result |
83
|
|
|
* |
84
|
|
|
* @var \O2System\Framework\Models\Sql\DataObjects\Result |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
public $result; |
87
|
|
|
/** |
88
|
|
|
* Model Result Row |
89
|
|
|
* |
90
|
|
|
* @var \O2System\Framework\Models\Sql\DataObjects\Result\Row |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public $row; |
93
|
|
|
/** |
94
|
|
|
* List of library valid sub models |
95
|
|
|
* |
96
|
|
|
* @access protected |
97
|
|
|
* |
98
|
|
|
* @type array driver classes list |
99
|
|
|
*/ |
100
|
|
|
protected $validSubModels = []; |
101
|
|
|
|
102
|
|
|
// ------------------------------------------------------------------------ |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* AbstractModel::__construct |
106
|
|
|
*/ |
107
|
|
|
public function __construct() |
108
|
|
|
{ |
109
|
|
|
// Set database connection |
110
|
|
|
if (method_exists(database(), 'loadConnection')) { |
111
|
|
|
if ($this->db = database()->loadConnection('default')) { |
112
|
|
|
$this->qb = $this->db->getQueryBuilder(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Set database table |
117
|
|
|
if (empty($this->table)) { |
118
|
|
|
$modelClassName = get_called_class(); |
119
|
|
|
$modelClassName = get_class_name($modelClassName); |
120
|
|
|
$this->table = underscore($modelClassName); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Fetch sub-models |
124
|
|
|
$this->fetchSubModels(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* AbstractModel::fetchSubModels |
129
|
|
|
* |
130
|
|
|
* @access protected |
131
|
|
|
* @final this method cannot be overwritten. |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
final protected function fetchSubModels() |
136
|
|
|
{ |
137
|
|
|
$reflection = new \ReflectionClass(get_called_class()); |
138
|
|
|
|
139
|
|
|
// Define called model class filepath |
140
|
|
|
$filePath = $reflection->getFileName(); |
141
|
|
|
|
142
|
|
|
// Define filename for used as subdirectory name |
143
|
|
|
$filename = pathinfo($filePath, PATHINFO_FILENAME); |
144
|
|
|
|
145
|
|
|
// Get model class directory name |
146
|
|
|
$dirName = dirname($filePath) . DIRECTORY_SEPARATOR; |
147
|
|
|
|
148
|
|
|
// Get sub models or siblings models |
149
|
|
|
if ($filename === 'Model' || $filename === modules()->current()->getDirName()) { |
|
|
|
|
150
|
|
|
$subModelsDirName = dirname($dirName) . DIRECTORY_SEPARATOR . 'Models' . DIRECTORY_SEPARATOR; |
151
|
|
|
|
152
|
|
|
if (is_dir($subModelsDirName)) { |
153
|
|
|
$subModelPath = $subModelsDirName; |
154
|
|
|
} |
155
|
|
|
} elseif (is_dir($subModelsDirName = $dirName . $filename . DIRECTORY_SEPARATOR)) { |
156
|
|
|
$subModelPath = $subModelsDirName; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (isset($subModelPath)) { |
160
|
|
|
loader()->addNamespace($reflection->name, $subModelPath); |
161
|
|
|
|
162
|
|
|
foreach (glob($subModelPath . '*.php') as $filepath) { |
163
|
|
|
if ($filepath === $filePath) { |
164
|
|
|
continue; |
165
|
|
|
} |
166
|
|
|
$this->validSubModels[ camelcase(pathinfo($filepath, PATHINFO_FILENAME)) ] = $filepath; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// ------------------------------------------------------------------------ |
172
|
|
|
|
173
|
|
|
final public static function __callStatic($method, array $arguments = []) |
174
|
|
|
{ |
175
|
|
|
$modelClassName = get_called_class(); |
176
|
|
|
|
177
|
|
|
if ( ! models()->has($modelClassName)) { |
178
|
|
|
models()->load($modelClassName, $modelClassName); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$modelInstance = models()->get($modelClassName); |
182
|
|
|
|
183
|
|
|
if (method_exists($modelInstance, $method)) { |
184
|
|
|
return $modelInstance->__call($method, $arguments); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
final public function __call($method, array $arguments = []) |
191
|
|
|
{ |
192
|
|
|
if (method_exists($this, $method)) { |
193
|
|
|
return call_user_func_array([&$this, $method], $arguments); |
194
|
|
|
} elseif (method_exists($this->db, $method)) { |
195
|
|
|
return call_user_func_array([&$this->db, $method], $arguments); |
196
|
|
|
} elseif (method_exists($this->qb, $method)) { |
197
|
|
|
return call_user_func_array([&$this->qb, $method], $arguments); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// ------------------------------------------------------------------------ |
204
|
|
|
|
205
|
|
|
public function __get($property) |
206
|
|
|
{ |
207
|
|
|
if ($this->row instanceof Row) { |
208
|
|
|
if ($this->row->offsetExists($property)) { |
209
|
|
|
return $this->row->offsetGet($property); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (empty($get[ $property ])) { |
|
|
|
|
214
|
|
|
if (services()->has($property)) { |
215
|
|
|
return services()->get($property); |
216
|
|
|
} elseif ($this->hasSubModel($property)) { |
217
|
|
|
return $this->loadSubModel($property); |
218
|
|
|
} elseif (o2system()->__isset($property)) { |
219
|
|
|
return o2system()->__get($property); |
220
|
|
|
} elseif (models()->__isset($property)) { |
221
|
|
|
return models()->get($property); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// ------------------------------------------------------------------------ |
227
|
|
|
|
228
|
|
|
final protected function loadSubModel($model) |
229
|
|
|
{ |
230
|
|
|
if ($this->hasSubModel($model)) { |
231
|
|
|
$classNames = [ |
232
|
|
|
'\\' . get_called_class() . '\\' . ucfirst($model), |
233
|
|
|
'\\' . get_namespace(get_called_class()) . ucfirst($model), |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
foreach ($classNames as $className) { |
237
|
|
|
if (class_exists($className)) { |
238
|
|
|
$this->{$model} = new $className(); |
239
|
|
|
break; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (property_exists($this, $model)) { |
245
|
|
|
return $this->{$model}; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return false; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
final protected function hasSubModel($model) |
252
|
|
|
{ |
253
|
|
|
if (array_key_exists($model, $this->validSubModels)) { |
254
|
|
|
return (bool)is_file($this->validSubModels[ $model ]); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return false; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
final protected function getSubModel($model) |
261
|
|
|
{ |
262
|
|
|
return $this->loadSubModel($model); |
263
|
|
|
} |
264
|
|
|
} |