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