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\Traits\ActiveRecord\ActiveRecordsTrait;
|
10
|
|
|
use Nip\Utility\Traits\NameWorksTrait;
|
11
|
|
|
|
12
|
|
|
/**
|
13
|
|
|
* Class Table
|
14
|
|
|
* @package Nip\Records\_Abstract
|
15
|
|
|
*
|
16
|
|
|
* @method \Nip_Helper_Url Url()
|
17
|
|
|
*/
|
18
|
|
|
abstract class RecordManager
|
19
|
|
|
{
|
20
|
|
|
use NameWorksTrait;
|
21
|
|
|
use ActiveRecordsTrait;
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* Collection class for current record manager
|
25
|
|
|
*
|
26
|
|
|
* @var string
|
27
|
|
|
*/
|
28
|
|
|
protected $collectionClass = null;
|
29
|
|
|
|
30
|
|
|
protected $helpers = [];
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* @var null|string
|
34
|
|
|
*/
|
35
|
|
|
protected $urlPK = null;
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* Model class name
|
39
|
|
|
* @var null|string
|
40
|
|
|
*/
|
41
|
|
|
protected $model = null;
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* @var null|string
|
45
|
|
|
*/
|
46
|
|
|
protected $controller = null;
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* @var null|string
|
50
|
|
|
*/
|
51
|
|
|
protected $modelNamespacePath = null;
|
52
|
|
|
|
53
|
|
|
protected $registry = null;
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* Overloads findByRecord, findByField, deleteByRecord, deleteByField, countByRecord, countByField
|
57
|
|
|
*
|
58
|
|
|
* @example findByCategory(Category $item)
|
59
|
|
|
* @example deleteByProduct(Product $item)
|
60
|
|
|
* @example findByIdUser(2)
|
61
|
|
|
* @example deleteByTitle(array('Lorem ipsum', 'like'))
|
62
|
|
|
* @example countByIdCategory(1)
|
63
|
|
|
*
|
64
|
|
|
* @param string $name
|
65
|
|
|
* @param array $arguments
|
66
|
|
|
*
|
67
|
|
|
* @return mixed
|
68
|
|
|
*/
|
69
|
|
|
public function __call($name, $arguments)
|
70
|
|
|
{
|
71
|
|
|
$return = $this->isCallDatabaseOperation($name, $arguments);
|
72
|
|
|
if ($return !== null) {
|
73
|
|
|
return $return;
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/** @noinspection PhpAssignmentInConditionInspection */
|
77
|
|
|
if ($return = $this->isCallUrl($name, $arguments)) {
|
78
|
|
|
return $return;
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
if ($name === ucfirst($name)) {
|
82
|
|
|
return $this->getHelper($name);
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
trigger_error("Call to undefined method $name", E_USER_ERROR);
|
86
|
|
|
|
87
|
|
|
return $this;
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* @param $name
|
92
|
|
|
* @param $arguments
|
93
|
|
|
* @return bool
|
94
|
|
|
*/
|
95
|
|
|
protected function isCallUrl($name, $arguments)
|
96
|
|
|
{
|
97
|
|
|
if (substr($name, 0, 3) == "get" && substr($name, -3) == "URL") {
|
98
|
|
|
$action = substr($name, 3, -3);
|
99
|
|
|
$params = isset($arguments[0]) ? $arguments[0] : [];
|
100
|
|
|
$module = isset($arguments[1]) ? $arguments[1] : null;
|
101
|
|
|
|
102
|
|
|
return $this->compileURL($action, $params, $module);
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
return false;
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* @param $action
|
110
|
|
|
* @param array $params
|
111
|
|
|
* @param null $module
|
112
|
|
|
* @return mixed
|
113
|
|
|
*/
|
114
|
|
|
public function compileURL($action, $params = [], $module = null)
|
115
|
|
|
{
|
116
|
|
|
$controller = $this->getController();
|
117
|
|
|
|
118
|
|
|
if (substr($action, 0, 5) == 'Async') {
|
119
|
|
|
$controller = 'async-' . $controller;
|
120
|
|
|
$action = substr($action, 5);
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
if (substr($action, 0, 5) == 'Modal') {
|
124
|
|
|
$controller = 'modal-' . $controller;
|
125
|
|
|
$action = substr($action, 5);
|
126
|
|
|
}
|
127
|
|
|
|
128
|
|
|
$params['action'] = (!empty($action)) ? $action : 'index';
|
129
|
|
|
$params['controller'] = $controller;
|
130
|
|
|
|
131
|
|
|
$params['action'] = inflector()->unclassify($params['action']);
|
132
|
|
|
$params['action'] = ($params['action'] == 'index') ? false : $params['action'];
|
133
|
|
|
|
134
|
|
|
$params['controller'] = $controller ? $controller : $this->getController();
|
135
|
|
|
$params['module'] = $module ? $module : request()->getModuleName();
|
136
|
|
|
|
137
|
|
|
$routeName = $params['module'] . '.' . $params['controller'] . '.' . $params['action'];
|
138
|
|
|
if ($this->Url()->getRouter()->hasRoute($routeName)) {
|
139
|
|
|
unset($params['module'], $params['controller'], $params['action']);
|
140
|
|
|
} else {
|
141
|
|
|
$routeName = $params['module'] . '.default';
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
return $this->Url()->assemble($routeName, $params);
|
|
|
|
|
145
|
|
|
}
|
146
|
|
|
|
147
|
|
|
/**
|
148
|
|
|
* @return string
|
149
|
|
|
*/
|
150
|
9 |
|
public function getController()
|
151
|
|
|
{
|
152
|
9 |
|
if ($this->controller === null) {
|
153
|
9 |
|
$this->initController();
|
154
|
|
|
}
|
155
|
|
|
|
156
|
9 |
|
return $this->controller;
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* @param null|string $controller
|
161
|
|
|
*/
|
162
|
9 |
|
public function setController($controller)
|
163
|
|
|
{
|
164
|
9 |
|
$this->controller = $controller;
|
165
|
9 |
|
}
|
166
|
|
|
|
167
|
9 |
|
protected function initController()
|
168
|
|
|
{
|
169
|
9 |
|
if ($this->isNamespaced()) {
|
170
|
4 |
|
$controller = $this->generateControllerNamespaced();
|
171
|
|
|
} else {
|
172
|
5 |
|
$controller = $this->generateControllerGeneric();
|
173
|
|
|
}
|
174
|
9 |
|
$this->setController($controller);
|
175
|
9 |
|
}
|
176
|
|
|
|
177
|
|
|
/**
|
178
|
|
|
* @return string
|
179
|
|
|
*/
|
180
|
4 |
|
protected function generateControllerNamespaced()
|
181
|
|
|
{
|
182
|
4 |
|
$class = $this->getModelNamespacePath();
|
183
|
4 |
|
$class = trim($class, '\\');
|
184
|
|
|
|
185
|
4 |
|
return inflector()->unclassify($class);
|
186
|
|
|
}
|
187
|
|
|
|
188
|
|
|
/**
|
189
|
|
|
* @return string
|
190
|
|
|
*/
|
191
|
4 |
|
public function getModelNamespacePath()
|
192
|
|
|
{
|
193
|
4 |
|
if ($this->modelNamespacePath == null) {
|
|
|
|
|
194
|
4 |
|
$this->initModelNamespacePath();
|
195
|
|
|
}
|
196
|
|
|
|
197
|
4 |
|
return $this->modelNamespacePath;
|
198
|
|
|
}
|
199
|
|
|
|
200
|
4 |
|
public function initModelNamespacePath()
|
201
|
|
|
{
|
202
|
4 |
|
if ($this->isNamespaced()) {
|
203
|
4 |
|
$path = $this->generateModelNamespacePathFromClassName() . '\\';
|
204
|
|
|
} else {
|
205
|
|
|
$controller = $this->generateControllerGeneric();
|
206
|
|
|
$path = inflector()->classify($controller) . '\\';
|
207
|
|
|
}
|
208
|
4 |
|
$this->modelNamespacePath = $path;
|
209
|
4 |
|
}
|
210
|
|
|
|
211
|
|
|
/**
|
212
|
|
|
* @return string
|
213
|
|
|
*/
|
214
|
4 |
|
protected function generateModelNamespacePathFromClassName()
|
215
|
|
|
{
|
216
|
4 |
|
$className = $this->getClassName();
|
217
|
4 |
|
$rootNamespace = $this->getRootNamespace();
|
218
|
4 |
|
$path = str_replace($rootNamespace, '', $className);
|
219
|
|
|
|
220
|
4 |
|
$nsParts = explode('\\', $path);
|
221
|
4 |
|
array_pop($nsParts);
|
222
|
|
|
|
223
|
4 |
|
return implode($nsParts, '\\');
|
224
|
|
|
}
|
225
|
|
|
|
226
|
|
|
/**
|
227
|
|
|
* @return string
|
228
|
|
|
*/
|
229
|
4 |
|
public function getRootNamespace()
|
230
|
|
|
{
|
231
|
4 |
|
return app('app')->getRootNamespace() . 'Models\\';
|
232
|
|
|
}
|
233
|
|
|
|
234
|
|
|
/**
|
235
|
|
|
* @return string
|
236
|
|
|
*/
|
237
|
5 |
|
protected function generateControllerGeneric()
|
238
|
|
|
{
|
239
|
5 |
|
$class = $this->getClassName();
|
240
|
|
|
|
241
|
5 |
|
return inflector()->unclassify($class);
|
242
|
|
|
}
|
243
|
|
|
|
244
|
|
|
/**
|
245
|
|
|
* @param $name
|
246
|
|
|
* @return mixed
|
247
|
|
|
*/
|
248
|
|
|
public function getHelper($name)
|
249
|
|
|
{
|
250
|
|
|
return HelperBroker::get($name);
|
251
|
|
|
}
|
252
|
|
|
|
253
|
|
|
/**
|
254
|
|
|
* @return string
|
255
|
|
|
*/
|
256
|
|
|
public function getModelNamespace()
|
257
|
|
|
{
|
258
|
|
|
return $this->getRootNamespace() . $this->getModelNamespacePath();
|
259
|
|
|
}
|
260
|
|
|
|
261
|
|
|
/**
|
262
|
|
|
* When searching by primary key, look for items in current registry before
|
263
|
|
|
* fetching them from the database
|
264
|
|
|
*
|
265
|
|
|
* @param array $pk_list
|
266
|
|
|
* @return RecordCollection
|
267
|
|
|
*/
|
268
|
|
|
public function findByPrimary($pk_list = [])
|
269
|
|
|
{
|
270
|
|
|
$pk = $this->getPrimaryKey();
|
271
|
|
|
$return = $this->newCollection();
|
272
|
|
|
|
273
|
|
|
if ($pk_list) {
|
|
|
|
|
274
|
|
|
$pk_list = array_unique($pk_list);
|
275
|
|
|
foreach ($pk_list as $key => $value) {
|
276
|
|
|
$item = $this->getRegistry()->get($value);
|
277
|
|
|
if ($item) {
|
278
|
|
|
unset($pk_list[$key]);
|
279
|
|
|
$return[$item->{$pk}] = $item;
|
280
|
|
|
}
|
281
|
|
|
}
|
282
|
|
|
if ($pk_list) {
|
|
|
|
|
283
|
|
|
$query = $this->paramsToQuery();
|
284
|
|
|
$query->where("$pk IN ?", $pk_list);
|
285
|
|
|
$items = $this->findByQuery($query);
|
286
|
|
|
|
287
|
|
|
if (count($items)) {
|
288
|
|
|
foreach ($items as $item) {
|
289
|
|
|
$this->getRegistry()->set($item->{$pk}, $item);
|
290
|
|
|
$return[$item->{$pk}] = $item;
|
291
|
|
|
}
|
292
|
|
|
}
|
293
|
|
|
}
|
294
|
|
|
}
|
295
|
|
|
|
296
|
|
|
return $return;
|
297
|
|
|
}
|
298
|
|
|
|
299
|
|
|
/**
|
300
|
|
|
* @return RecordCollection
|
301
|
|
|
*/
|
302
|
1 |
|
public function newCollection()
|
303
|
|
|
{
|
304
|
1 |
|
$class = $this->getCollectionClass();
|
305
|
|
|
/** @var RecordCollection $collection */
|
306
|
1 |
|
$collection = new $class();
|
307
|
1 |
|
$collection->setManager($this);
|
308
|
|
|
|
309
|
1 |
|
return $collection;
|
310
|
|
|
}
|
311
|
|
|
|
312
|
|
|
/**
|
313
|
|
|
* @return string
|
314
|
|
|
*/
|
315
|
2 |
|
public function getCollectionClass()
|
316
|
|
|
{
|
317
|
2 |
|
if ($this->collectionClass === null) {
|
318
|
2 |
|
$this->initCollectionClass();
|
319
|
|
|
}
|
320
|
|
|
|
321
|
2 |
|
return $this->collectionClass;
|
322
|
|
|
}
|
323
|
|
|
|
324
|
|
|
/**
|
325
|
|
|
* @param string $collectionClass
|
326
|
|
|
*/
|
327
|
2 |
|
public function setCollectionClass($collectionClass)
|
328
|
|
|
{
|
329
|
2 |
|
$this->collectionClass = $collectionClass;
|
330
|
2 |
|
}
|
331
|
|
|
|
332
|
2 |
|
protected function initCollectionClass()
|
333
|
|
|
{
|
334
|
2 |
|
$this->setCollectionClass($this->generateCollectionClass());
|
335
|
2 |
|
}
|
336
|
|
|
|
337
|
|
|
/**
|
338
|
|
|
* @return string
|
339
|
|
|
*/
|
340
|
2 |
|
protected function generateCollectionClass()
|
341
|
|
|
{
|
342
|
2 |
|
return RecordCollection::class;
|
343
|
|
|
}
|
344
|
|
|
|
345
|
|
|
/**
|
346
|
|
|
* @return \Nip\Collections\Registry
|
347
|
|
|
*/
|
348
|
|
|
public function getRegistry()
|
349
|
|
|
{
|
350
|
|
|
if (!$this->registry) {
|
351
|
|
|
$this->registry = new Registry();
|
352
|
|
|
}
|
353
|
|
|
|
354
|
|
|
return $this->registry;
|
355
|
|
|
}
|
356
|
|
|
|
357
|
|
|
/**
|
358
|
|
|
* Factory
|
359
|
|
|
*
|
360
|
|
|
* @return Record
|
361
|
|
|
* @param array $data [optional]
|
362
|
|
|
*/
|
363
|
|
|
public function getNew($data = [])
|
364
|
|
|
{
|
365
|
|
|
$pk = $this->getPrimaryKey();
|
366
|
|
|
if (is_string($pk) && isset($data[$pk]) && $this->getRegistry()->has($data[$pk])) {
|
367
|
|
|
$return = $this->getRegistry()->get($data[$pk]);
|
368
|
|
|
$return->writeData($data);
|
369
|
|
|
$return->writeDBData($data);
|
370
|
|
|
|
371
|
|
|
return $return;
|
372
|
|
|
}
|
373
|
|
|
|
374
|
|
|
$record = $this->getNewRecordFromDB($data);
|
375
|
|
|
|
376
|
|
|
return $record;
|
377
|
|
|
}
|
378
|
|
|
|
379
|
|
|
/**
|
380
|
|
|
* @param array $data
|
381
|
|
|
* @return Record
|
382
|
|
|
*/
|
383
|
|
|
public function getNewRecordFromDB($data = [])
|
384
|
|
|
{
|
385
|
|
|
$record = $this->getNewRecord($data);
|
386
|
|
|
$record->writeDBData($data);
|
387
|
|
|
|
388
|
|
|
return $record;
|
389
|
|
|
}
|
390
|
|
|
|
391
|
|
|
/**
|
392
|
|
|
* @param array $data
|
393
|
|
|
* @return Record
|
394
|
|
|
*/
|
395
|
|
|
public function getNewRecord($data = [])
|
396
|
|
|
{
|
397
|
|
|
$model = $this->getModel();
|
398
|
|
|
/** @var Record $record */
|
399
|
|
|
$record = new $model();
|
400
|
|
|
$record->setManager($this);
|
401
|
|
|
$record->writeData($data);
|
402
|
|
|
|
403
|
|
|
return $record;
|
404
|
|
|
}
|
405
|
|
|
|
406
|
|
|
/**
|
407
|
|
|
* @return string
|
408
|
|
|
*/
|
409
|
1 |
|
public function getModel()
|
410
|
|
|
{
|
411
|
1 |
|
if ($this->model == null) {
|
|
|
|
|
412
|
|
|
$this->inflectModel();
|
413
|
|
|
}
|
414
|
|
|
|
415
|
1 |
|
return $this->model;
|
416
|
|
|
}
|
417
|
|
|
|
418
|
|
|
/**
|
419
|
|
|
* @param null $model
|
420
|
|
|
*/
|
421
|
1 |
|
public function setModel($model)
|
422
|
|
|
{
|
423
|
1 |
|
$this->model = $model;
|
424
|
1 |
|
}
|
425
|
|
|
|
426
|
|
|
protected function inflectModel()
|
427
|
|
|
{
|
428
|
|
|
$class = $this->getClassName();
|
429
|
|
|
if ($this->model == null) {
|
|
|
|
|
430
|
|
|
$this->model = $this->generateModelClass($class);
|
431
|
|
|
}
|
432
|
|
|
}
|
433
|
|
|
|
434
|
|
|
/**
|
435
|
|
|
* @param null $class
|
436
|
|
|
* @return string
|
437
|
|
|
*/
|
438
|
1 |
|
public function generateModelClass($class = null)
|
439
|
|
|
{
|
440
|
1 |
|
$class = $class ? $class : get_class($this);
|
441
|
|
|
|
442
|
1 |
|
if (strpos($class, '\\')) {
|
443
|
1 |
|
$nsParts = explode('\\', $class);
|
444
|
1 |
|
$class = array_pop($nsParts);
|
445
|
|
|
|
446
|
1 |
|
if ($class == 'Table') {
|
447
|
1 |
|
$class = 'Row';
|
448
|
|
|
} else {
|
449
|
|
|
$class = ucfirst(inflector()->singularize($class));
|
450
|
|
|
}
|
451
|
|
|
|
452
|
1 |
|
return implode($nsParts, '\\').'\\'.$class;
|
453
|
|
|
}
|
454
|
|
|
|
455
|
1 |
|
return ucfirst(inflector()->singularize($class));
|
456
|
|
|
}
|
457
|
|
|
|
458
|
|
|
/**
|
459
|
|
|
* @return \Nip\Request
|
460
|
|
|
*/
|
461
|
|
|
public function getRequest()
|
462
|
|
|
{
|
463
|
|
|
return request();
|
|
|
|
|
464
|
|
|
}
|
465
|
|
|
|
466
|
|
|
public function __wakeup()
|
467
|
|
|
{
|
468
|
|
|
$this->initDB();
|
469
|
|
|
}
|
470
|
|
|
|
471
|
|
|
/**
|
472
|
|
|
* @param Record $item
|
473
|
|
|
* @return bool|false|Record
|
474
|
|
|
*/
|
475
|
|
|
public function exists(Record $item)
|
476
|
|
|
{
|
477
|
|
|
$params = [];
|
478
|
|
|
$params['where'] = [];
|
479
|
|
|
|
480
|
|
|
$fields = $this->getUniqueFields();
|
|
|
|
|
481
|
|
|
|
482
|
|
|
if (!$fields) {
|
483
|
|
|
return false;
|
484
|
|
|
}
|
485
|
|
|
|
486
|
|
|
foreach ($fields as $field) {
|
487
|
|
|
$params['where'][$field.'-UNQ'] = ["$field = ?", $item->{$field}];
|
488
|
|
|
}
|
489
|
|
|
|
490
|
|
|
$pk = $this->getPrimaryKey();
|
491
|
|
|
if ($item->getPrimaryKey()) {
|
492
|
|
|
$params['where'][] = ["$pk != ?", $item->getPrimaryKey()];
|
493
|
|
|
}
|
494
|
|
|
|
495
|
|
|
return $this->findOneByParams($params);
|
496
|
|
|
}
|
497
|
|
|
|
498
|
|
|
/**
|
499
|
|
|
* @return null
|
500
|
|
|
*/
|
501
|
|
|
public function getUniqueFields()
|
502
|
|
|
{
|
503
|
|
|
if ($this->uniqueFields === null) {
|
504
|
|
|
$this->initUniqueFields();
|
505
|
|
|
}
|
506
|
|
|
|
507
|
|
|
return $this->uniqueFields;
|
508
|
|
|
}
|
509
|
|
|
|
510
|
|
|
/**
|
511
|
|
|
* @return array|null
|
512
|
|
|
*/
|
513
|
|
|
public function initUniqueFields()
|
514
|
|
|
{
|
515
|
|
|
$this->uniqueFields = [];
|
516
|
|
|
$structure = $this->getTableStructure();
|
517
|
|
|
foreach ($structure['indexes'] as $name => $index) {
|
518
|
|
|
if ($index['unique']) {
|
519
|
|
|
foreach ($index['fields'] as $field) {
|
520
|
|
|
if ($field != $this->getPrimaryKey()) {
|
521
|
|
|
$this->uniqueFields[] = $field;
|
522
|
|
|
}
|
523
|
|
|
}
|
524
|
|
|
}
|
525
|
|
|
}
|
526
|
|
|
|
527
|
|
|
return $this->uniqueFields;
|
528
|
|
|
}
|
529
|
|
|
|
530
|
|
|
/**
|
531
|
|
|
* Finds one Record using params array
|
532
|
|
|
*
|
533
|
|
|
* @param array $params
|
534
|
|
|
* @return Record|false
|
535
|
|
|
*/
|
536
|
|
|
public function findOneByParams(array $params = [])
|
537
|
|
|
{
|
538
|
|
|
$params['limit'] = 1;
|
539
|
|
|
$records = $this->findByParams($params);
|
540
|
|
|
if (count($records) > 0) {
|
541
|
|
|
return $records->rewind();
|
542
|
|
|
}
|
543
|
|
|
|
544
|
|
|
return false;
|
545
|
|
|
}
|
546
|
|
|
|
547
|
|
|
/**
|
548
|
|
|
* Finds Records using params array
|
549
|
|
|
*
|
550
|
|
|
* @param array $params
|
551
|
|
|
* @return mixed
|
552
|
|
|
*/
|
553
|
|
|
public function findByParams($params = [])
|
554
|
|
|
{
|
555
|
|
|
$query = $this->paramsToQuery($params);
|
556
|
|
|
|
557
|
|
|
return $this->findByQuery($query, $params);
|
558
|
|
|
}
|
559
|
|
|
|
560
|
|
|
/**
|
561
|
|
|
* @return RecordCollection
|
562
|
|
|
*/
|
563
|
|
|
public function getAll()
|
564
|
|
|
{
|
565
|
|
|
if (!$this->getRegistry()->has("all")) {
|
566
|
|
|
$this->getRegistry()->set("all", $this->findAll());
|
567
|
|
|
}
|
568
|
|
|
|
569
|
|
|
return $this->getRegistry()->get("all");
|
570
|
|
|
}
|
571
|
|
|
|
572
|
|
|
/**
|
573
|
|
|
* @return RecordCollection
|
574
|
|
|
*/
|
575
|
|
|
public function findAll()
|
576
|
|
|
{
|
577
|
|
|
return $this->findByParams();
|
578
|
|
|
}
|
579
|
|
|
|
580
|
|
|
/**
|
581
|
|
|
* @param int $count
|
582
|
|
|
* @return mixed
|
583
|
|
|
*/
|
584
|
|
|
public function findLast($count = 9)
|
585
|
|
|
{
|
586
|
|
|
return $this->findByParams([
|
587
|
|
|
'limit' => $count,
|
588
|
|
|
]);
|
589
|
|
|
}
|
590
|
|
|
|
591
|
|
|
/**
|
592
|
|
|
* Inserts a Record into the database
|
593
|
|
|
* @param Record $model
|
594
|
|
|
* @param array|bool $onDuplicate
|
595
|
|
|
* @return mixed
|
596
|
|
|
*/
|
597
|
|
|
public function insert($model, $onDuplicate = false)
|
598
|
|
|
{
|
599
|
|
|
$query = $this->insertQuery($model, $onDuplicate);
|
600
|
|
|
$query->execute();
|
601
|
|
|
|
602
|
|
|
return $this->getDB()->lastInsertID();
|
603
|
|
|
}
|
604
|
|
|
|
605
|
|
|
/**
|
606
|
|
|
* @param $model
|
607
|
|
|
* @param $onDuplicate
|
608
|
|
|
* @return InsertQuery
|
609
|
|
|
*/
|
610
|
|
|
public function insertQuery($model, $onDuplicate)
|
611
|
|
|
{
|
612
|
|
|
$inserts = $this->getQueryModelData($model);
|
613
|
|
|
|
614
|
|
|
$query = $this->newInsertQuery();
|
615
|
|
|
$query->data($inserts);
|
|
|
|
|
616
|
|
|
|
617
|
|
|
if ($onDuplicate !== false) {
|
618
|
|
|
$query->onDuplicate($onDuplicate);
|
619
|
|
|
}
|
620
|
|
|
|
621
|
|
|
return $query;
|
622
|
|
|
}
|
623
|
|
|
|
624
|
|
|
/**
|
625
|
|
|
* @param Record $model
|
626
|
|
|
* @return array
|
627
|
|
|
*/
|
628
|
|
|
public function getQueryModelData($model)
|
629
|
|
|
{
|
630
|
|
|
$data = [];
|
631
|
|
|
|
632
|
|
|
$fields = $this->getFields();
|
|
|
|
|
633
|
|
|
foreach ($fields as $field) {
|
|
|
|
|
634
|
|
|
if (isset($model->{$field})) {
|
635
|
|
|
$data[$field] = $model->{$field};
|
636
|
|
|
}
|
637
|
|
|
}
|
638
|
|
|
|
639
|
|
|
return $data;
|
640
|
|
|
}
|
641
|
|
|
|
642
|
|
|
/**
|
643
|
|
|
* The name of the field used as a foreign key in other tables
|
644
|
|
|
* @return string
|
645
|
|
|
*/
|
646
|
7 |
|
public function getPrimaryFK()
|
647
|
|
|
{
|
648
|
7 |
|
if ($this->foreignKey == null) {
|
|
|
|
|
649
|
5 |
|
$this->initPrimaryFK();
|
650
|
|
|
}
|
651
|
|
|
|
652
|
7 |
|
return $this->foreignKey;
|
653
|
|
|
}
|
654
|
|
|
|
655
|
5 |
|
public function initPrimaryFK()
|
656
|
|
|
{
|
657
|
5 |
|
$this->setForeignKey($this->generatePrimaryFK());
|
658
|
5 |
|
}
|
659
|
|
|
|
660
|
|
|
/**
|
661
|
|
|
* @param null $foreignKey
|
662
|
|
|
*/
|
663
|
5 |
|
public function setForeignKey($foreignKey)
|
664
|
|
|
{
|
665
|
5 |
|
$this->foreignKey = $foreignKey;
|
666
|
5 |
|
}
|
667
|
|
|
|
668
|
|
|
/**
|
669
|
|
|
* @return string
|
670
|
|
|
*/
|
671
|
5 |
|
public function generatePrimaryFK()
|
672
|
|
|
{
|
673
|
5 |
|
$singularize = inflector()->singularize($this->getController());
|
674
|
|
|
|
675
|
5 |
|
return $this->getPrimaryKey()."_".inflector()->underscore($singularize);
|
676
|
|
|
}
|
677
|
|
|
|
678
|
|
|
/**
|
679
|
|
|
* @param $fk
|
680
|
|
|
*/
|
681
|
2 |
|
public function setPrimaryFK($fk)
|
682
|
|
|
{
|
683
|
2 |
|
$this->foreignKey = $fk;
|
684
|
2 |
|
}
|
685
|
|
|
|
686
|
|
|
/**
|
687
|
|
|
* The name of the field used as a foreign key in other tables
|
688
|
|
|
* @return string
|
689
|
|
|
*/
|
690
|
|
|
public function getUrlPK()
|
691
|
|
|
{
|
692
|
|
|
if ($this->urlPK == null) {
|
|
|
|
|
693
|
|
|
$this->urlPK = $this->getPrimaryKey();
|
694
|
|
|
}
|
695
|
|
|
|
696
|
|
|
return $this->urlPK;
|
697
|
|
|
}
|
698
|
|
|
|
699
|
|
|
/**
|
700
|
|
|
* @param $name
|
701
|
|
|
* @return bool
|
702
|
|
|
*/
|
703
|
|
|
public function hasField($name)
|
704
|
|
|
{
|
705
|
|
|
$fields = $this->getFields();
|
|
|
|
|
706
|
|
|
if (is_array($fields) && in_array($name, $fields)) {
|
707
|
|
|
return true;
|
708
|
|
|
}
|
709
|
|
|
|
710
|
|
|
return false;
|
711
|
|
|
}
|
712
|
|
|
|
713
|
|
|
/**
|
714
|
|
|
* @return array
|
715
|
|
|
*/
|
716
|
|
|
public function getFullTextFields()
|
717
|
|
|
{
|
718
|
|
|
$return = [];
|
719
|
|
|
$structure = $this->getTableStructure();
|
720
|
|
|
foreach ($structure['indexes'] as $name => $index) {
|
721
|
|
|
if ($index['fulltext']) {
|
722
|
|
|
$return[$name] = $index['fields'];
|
723
|
|
|
}
|
724
|
|
|
}
|
725
|
|
|
|
726
|
|
|
return $return;
|
727
|
|
|
}
|
728
|
|
|
|
729
|
|
|
/**
|
730
|
|
|
* Sets model and database table from the class name
|
731
|
|
|
*/
|
732
|
|
|
protected function inflect()
|
733
|
|
|
{
|
734
|
|
|
$this->initController();
|
735
|
|
|
}
|
736
|
|
|
}
|
737
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: