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