-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathModelTrait.php
More file actions
334 lines (273 loc) · 9 KB
/
ModelTrait.php
File metadata and controls
334 lines (273 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace Tatter\Relations\Traits;
use RuntimeException;
use Tatter\Schemas\Structures\Schema;
trait ModelTrait
{
use BaseTrait;
/**
* Whether to reindex results by the primary key
*
* @var bool
*/
protected $reindex = true;
/**
* Add related tables to load along with the next finder.
*
* @param mixed $with Table name, array of table names, or false (to disable)
* @param bool $overwrite Whether to merge with existing table 'with' list
*
* @return $this
*/
public function with($with, bool $overwrite = false)
{
// Check for a request to disable
if ($with === false) {
$this->tmpWith = [];
return $this;
}
// Force a single table name into an array
if (! is_array($with)) {
$with = [$with];
}
// Option to override this model's pre-seeded value
$this->tmpWith = $overwrite ? $with : array_merge($this->getWith(), $with);
return $this;
}
/**
* Blocks specified tables from being loaded as relations with the next finder.
* Used mostly to prevent nesting loops.
*
* @param list<string>|string $tables Table name or array of table names
*
* @return $this
*/
public function without($tables)
{
// @phpstan-ignore-next-line
if (! is_string($tables) && ! is_array($tables)) {
throw new RuntimeException(lang('Relations.invalidWithout'));
}
if (is_string($tables)) {
$tables = [$tables];
}
$this->tmpWithout = array_merge($this->getWithout(), $tables);
return $this;
}
/**
* Return $with
*/
protected function getWith(): array
{
// Ensure $this->with is set at all
if (empty($this->with)) {
$this->with = [];
}
// Force a single table name into an array
if (! is_array($this->with)) {
$this->with = [$this->with];
}
return $this->with;
}
/**
* Return $withOut
*/
protected function getWithout(): array
{
// Ensure $this->without is set at all
if (empty($this->without)) {
$this->without = [];
}
// Force a single table name into an array
if (! is_array($this->without)) {
$this->without = [$this->without];
}
return $this->without;
}
/**
* Reset per-query variables.
*
* @return $this
*/
public function resetTmp()
{
unset($this->tmpWith, $this->tmpWithout, $this->tmpReindex);
return $this;
}
/**
* Enable/disable result reindexing.
*
* @return $this
*/
public function reindex(bool $bool = true)
{
$this->reindex = $bool;
return $this;
}
/**
* Intercept join requests to disable reindexing.
*
* @return $this
*/
public function join(...$params)
{
$this->tmpReindex = false;
// Pass through to the builder
$this->builder()->join(...$params);
return $this;
}
// --------------------------------------------------------------------
// FINDERS EXTENSIONS
// --------------------------------------------------------------------
/**
* Fetches the row of database from $this->table with a primary key
* matching $id.
*
* @param array|mixed|null $id One primary key or an array of primary keys
*
* @return array|object|null The resulting row of data, or null.
*/
public function find($id = null)
{
// Get data from the framework model as usual
$data = parent::find($id);
// For singletons, wrap them as a one-item array and then unwrap on return
if (is_numeric($id) || is_string($id)) {
$data = $this->addRelations([$data]);
return reset($data);
}
return $this->addRelations($data);
}
// --------------------------------------------------------------------
/**
* Works with the current Query Builder instance to return
* all results, while optionally limiting them.
*
* @return array|null
*/
public function findAll(?int $limit = null, int $offset = 0)
{
$data = parent::findAll($limit, $offset);
return $this->addRelations($data);
}
// --------------------------------------------------------------------
/**
* Returns the first row of the result set. Will take any previous
* Query Builder calls into account when determining the result set.
*
* @return array|object|null
*/
public function first()
{
$data = parent::first();
// For singletons, wrap them as a one-item array and then unwrap on return
$data = $this->addRelations([$data]);
return reset($data);
}
/**
* Intercepts data from a finder and injects related items
*
* @param array $rows Array of rows from the finder
*/
protected function addRelations($rows): ?array
{
// If there were no matches then reset per-query data and quit
if (empty($rows)) {
$this->resetTmp();
return $rows;
}
// Likewise for empty singletons
if (count($rows) === 1 && reset($rows) === null) {
$this->resetTmp();
return $rows;
}
// If no tmpWith was set then use this model's default
if (! isset($this->tmpWith)) {
$this->tmpWith = $this->getWith();
}
// If no tmpWithout was set then use this model's default
if (! isset($this->tmpWithout)) {
$this->tmpWithout = $this->getWithout();
}
// If no tmpReindex was set then use this model's default
if (! isset($this->tmpReindex)) {
$this->tmpReindex = $this->reindex;
}
// Remove any blocked tables from the request
$this->tmpWith = array_diff($this->tmpWith, $this->tmpWithout);
// If tmpWith ends up empty then reset and quit
if (empty($this->tmpWith)) {
$rows = $this->tmpReindex ? $this->simpleReindex($rows) : $rows;
$this->resetTmp();
return $rows;
}
// Harvest the IDs that want relations
$ids = array_column($rows, $this->primaryKey);
// Get the schema
$schema = $this->_schema();
// Find the relations for each table
$relations = $singletons = [];
foreach ($this->tmpWith as $tableName) {
// Check for singletons
$relation = $this->_getRelationship($tableName);
$singletons[$tableName] = $relation->singleton ? singular($tableName) : false;
$relations[$tableName] = $this->_getRelations($tableName, $ids);
}
unset($schema);
// Inject related items back into the rows
$return = [];
foreach ($rows as $item) {
$id = is_array($item) ? $item[$this->primaryKey] : $item->{$this->primaryKey};
// Inject related items
foreach ($relations as $tableName => $related) {
// Assign singletons to a property named for the singular table
if ($name = $singletons[$tableName]) {
if (is_array($item)) {
$item[$name] = $related[$id] ?? null;
} else {
$item->{$name} = $related[$id] ?? null;
}
} elseif (is_array($item)) {
$item[$tableName] = $related[$id] ?? [];
} else {
$item->{$tableName} = $related[$id] ?? [];
}
}
if ($this->tmpReindex) {
$return[$id] = $item;
} else {
$return[] = $item;
}
}
// Clear old data and reset per-query properties
unset($rows);
$this->resetTmp();
return $return;
}
/**
* Reindexes $rows from a finder by their primary key
* Mostly used for consistent return format when no relations are requested
* If multiple rows have the same primary (e.g. a join) it returns the originals
*
* @param array $rows Array of rows from the finder
*/
public function simpleReindex($rows): array
{
if (empty($rows)) {
return [];
}
// Reindex $rows by this model's primary key and inject related items
$return = [];
foreach ($rows as $item) {
// Handle array return types
$id = is_array($item) ? $item[$this->primaryKey] ?? null : $item->{$this->primaryKey} ?? null;
// If no primary key or an entry already existed then return it as is
// Probably the former is custom select() and the latter is a join()
if (empty($id) || isset($return[$id])) {
return $rows;
}
$return[$id] = $item;
}
return $return;
}
}