1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\AbstractModels; |
4
|
|
|
|
5
|
|
|
use Nip\Collections\Registry; |
6
|
|
|
use Nip\Database\Query\Insert as InsertQuery; |
7
|
|
|
use Nip\HelperBroker; |
8
|
|
|
use Nip\Records\Collections\Collection as RecordCollection; |
9
|
|
|
use Nip\Records\EventManager\HasEvents; |
10
|
|
|
use Nip\Records\Traits\ActiveRecord\ActiveRecordsTrait; |
11
|
|
|
use Nip\Records\Traits\CanBoot\CanBootRecordsTrait; |
12
|
|
|
use Nip\Records\Traits\CanBootTraits\CanBootTraitsRecordsTrait; |
13
|
|
|
use Nip\Records\Traits\HasController\HasControllerRecordsTrait; |
14
|
|
|
use Nip\Records\Traits\HasModelName\HasModelNameRecordsTrait; |
15
|
|
|
use Nip\Records\Traits\HasUrl\HasUrlRecordManagerTrait; |
16
|
|
|
use Nip\Utility\Traits\NameWorksTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Table |
20
|
|
|
* @package Nip\Records\_Abstract |
21
|
|
|
* |
22
|
|
|
* @method \Nip_Helper_Url Url() |
23
|
|
|
*/ |
24
|
|
|
abstract class RecordManager |
25
|
|
|
{ |
26
|
|
|
use NameWorksTrait; |
27
|
|
|
use ActiveRecordsTrait; |
28
|
|
|
use CanBootRecordsTrait; |
29
|
|
|
use CanBootTraitsRecordsTrait; |
30
|
|
|
use HasControllerRecordsTrait; |
31
|
|
|
use HasModelNameRecordsTrait; |
32
|
|
|
use HasEvents; |
33
|
|
|
use HasUrlRecordManagerTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Collection class for current record manager |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $collectionClass = null; |
41
|
|
|
|
42
|
|
|
protected $helpers = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var null|string |
46
|
|
|
*/ |
47
|
|
|
protected $urlPK = null; |
48
|
|
|
|
49
|
|
|
protected $registry = null; |
50
|
|
|
|
51
|
31 |
|
public function __construct() |
52
|
|
|
{ |
53
|
31 |
|
$this->bootIfNotBooted(); |
54
|
31 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Overloads findByRecord, findByField, deleteByRecord, deleteByField, countByRecord, countByField |
58
|
|
|
* |
59
|
|
|
* @example findByCategory(Category $item) |
60
|
|
|
* @example deleteByProduct(Product $item) |
61
|
|
|
* @example findByIdUser(2) |
62
|
|
|
* @example deleteByTitle(array('Lorem ipsum', 'like')) |
63
|
|
|
* @example countByIdCategory(1) |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* @param array $arguments |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
1 |
|
public function __call($name, $arguments) |
71
|
|
|
{ |
72
|
1 |
|
$return = $this->isCallDatabaseOperation($name, $arguments); |
73
|
1 |
|
if ($return !== false) { |
74
|
|
|
return $return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @noinspection PhpAssignmentInConditionInspection */ |
78
|
1 |
|
if ($return = $this->isCallUrl($name, $arguments)) { |
79
|
|
|
return $return; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
if ($name === ucfirst($name)) { |
83
|
1 |
|
return $this->getHelper($name); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
trigger_error("Call to undefined method $name", E_USER_ERROR); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* When a model is being unserialized, check if it needs to be booted. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function __wakeup() |
97
|
|
|
{ |
98
|
|
|
$this->initDB(); |
99
|
|
|
$this->bootIfNotBooted(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
10 |
|
public function getRootNamespace() |
106
|
|
|
{ |
107
|
10 |
|
if (function_exists('app') && app()->has('app')) { |
108
|
|
|
return app('app')->getRootNamespace() . 'Models\\'; |
109
|
|
|
} |
110
|
10 |
|
return 'App\\Models\\'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $name |
115
|
|
|
* @return \Nip\Helpers\AbstractHelper |
116
|
|
|
*/ |
117
|
1 |
|
public function getHelper($name) |
118
|
|
|
{ |
119
|
1 |
|
return HelperBroker::get($name); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getModelNamespace() |
126
|
|
|
{ |
127
|
|
|
return $this->getRootNamespace() . $this->getModelNamespacePath(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return RecordCollection |
132
|
|
|
*/ |
133
|
1 |
|
public function newCollection() |
134
|
|
|
{ |
135
|
1 |
|
$class = $this->getCollectionClass(); |
136
|
|
|
/** @var RecordCollection $collection */ |
137
|
1 |
|
$collection = new $class(); |
138
|
1 |
|
$collection->setManager($this); |
139
|
|
|
|
140
|
1 |
|
return $collection; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
2 |
|
public function getCollectionClass() |
147
|
|
|
{ |
148
|
2 |
|
if ($this->collectionClass === null) { |
149
|
2 |
|
$this->initCollectionClass(); |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
return $this->collectionClass; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $collectionClass |
157
|
|
|
*/ |
158
|
2 |
|
public function setCollectionClass($collectionClass) |
159
|
|
|
{ |
160
|
2 |
|
$this->collectionClass = $collectionClass; |
161
|
2 |
|
} |
162
|
|
|
|
163
|
2 |
|
protected function initCollectionClass() |
164
|
|
|
{ |
165
|
2 |
|
$this->setCollectionClass($this->generateCollectionClass()); |
166
|
2 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
2 |
|
protected function generateCollectionClass() |
172
|
|
|
{ |
173
|
2 |
|
return RecordCollection::class; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return \Nip\Collections\Registry |
178
|
|
|
*/ |
179
|
|
|
public function getRegistry() |
180
|
|
|
{ |
181
|
|
|
if (!$this->registry) { |
182
|
|
|
$this->registry = new Registry(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this->registry; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Factory |
190
|
|
|
* |
191
|
|
|
* @param array $data [optional] |
192
|
|
|
* @return Record |
193
|
|
|
*/ |
194
|
3 |
|
public function getNew($data = []) |
195
|
|
|
{ |
196
|
3 |
|
$pk = $this->getPrimaryKey(); |
197
|
3 |
|
if (is_string($pk) && isset($data[$pk]) && $this->getRegistry()->has($data[$pk])) { |
198
|
|
|
$return = $this->getRegistry()->get($data[$pk]); |
199
|
|
|
$return->writeData($data); |
200
|
|
|
$return->writeDBData($data); |
201
|
|
|
|
202
|
|
|
return $return; |
203
|
|
|
} |
204
|
|
|
|
205
|
3 |
|
$record = $this->getNewRecordFromDB($data); |
206
|
|
|
|
207
|
3 |
|
return $record; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array $data |
212
|
|
|
* @return Record |
213
|
|
|
*/ |
214
|
3 |
|
public function getNewRecordFromDB($data = []) |
215
|
|
|
{ |
216
|
3 |
|
$record = $this->getNewRecord($data); |
217
|
3 |
|
$record->writeDBData($data); |
218
|
|
|
|
219
|
3 |
|
return $record; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param array $data |
224
|
|
|
* @return Record |
225
|
|
|
*/ |
226
|
3 |
|
public function getNewRecord($data = []) |
227
|
|
|
{ |
228
|
3 |
|
$model = $this->getModel(); |
229
|
|
|
/** @var Record $record */ |
230
|
3 |
|
$record = new $model(); |
231
|
3 |
|
$record->setManager($this); |
232
|
3 |
|
$record->writeData($data); |
233
|
|
|
|
234
|
3 |
|
return $record; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return \Nip\Request |
240
|
|
|
*/ |
241
|
|
|
public function getRequest() |
242
|
|
|
{ |
243
|
|
|
return request(); |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return RecordCollection |
248
|
|
|
*/ |
249
|
|
|
public function getAll() |
250
|
|
|
{ |
251
|
|
|
if (!$this->getRegistry()->has("all")) { |
252
|
|
|
$this->getRegistry()->set("all", $this->findAll()); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this->getRegistry()->get("all"); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param Record $model |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
1 |
|
public function getQueryModelData($model) |
263
|
|
|
{ |
264
|
1 |
|
$data = []; |
265
|
|
|
|
266
|
1 |
|
$fields = $this->getFields(); |
|
|
|
|
267
|
1 |
|
foreach ($fields as $field) { |
|
|
|
|
268
|
1 |
|
if (isset($model->{$field})) { |
269
|
|
|
$data[$field] = $model->{$field}; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
1 |
|
return $data; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* The name of the field used as a foreign key in other tables |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
public function getUrlPK() |
281
|
|
|
{ |
282
|
|
|
if ($this->urlPK == null) { |
|
|
|
|
283
|
|
|
$this->urlPK = $this->getPrimaryKey(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $this->urlPK; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param $name |
291
|
|
|
* @return bool |
292
|
|
|
*/ |
293
|
1 |
|
public function hasField($name) |
294
|
|
|
{ |
295
|
1 |
|
$fields = $this->getFields(); |
|
|
|
|
296
|
1 |
|
if (is_array($fields) && in_array($name, $fields)) { |
|
|
|
|
297
|
1 |
|
return true; |
298
|
|
|
} |
299
|
|
|
|
300
|
1 |
|
return false; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return array |
305
|
|
|
*/ |
306
|
|
|
public function getFullTextFields() |
307
|
|
|
{ |
308
|
|
|
$return = []; |
309
|
|
|
$structure = $this->getTableStructure(); |
310
|
|
|
foreach ($structure['indexes'] as $name => $index) { |
311
|
|
|
if ($index['fulltext']) { |
312
|
|
|
$return[$name] = $index['fields']; |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return $return; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Sets model and database table from the class name |
321
|
|
|
*/ |
322
|
|
|
protected function inflect() |
323
|
|
|
{ |
324
|
|
|
$this->initController(); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|