-
Notifications
You must be signed in to change notification settings - Fork 0
/
Store.php
1056 lines (958 loc) · 32.6 KB
/
Store.php
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php namespace Mreschke\Repository;
use Event;
use stdClass;
use Exception;
use Illuminate\Support\Collection;
/**
* Repository Store
* @copyright 2015 Matthew Reschke
* @license http://mreschke.com/license/mit
* @author Matthew Reschke <mail@mreschke.com>
*/
abstract class Store
{
/**
* The repository manager
* @var object
*/
public $manager;
/**
* The repository store key
* @var object
*/
protected $storeKey;
/**
* The repository attributes
* @var array
*/
protected $attributes;
/**
* The selected columns for this instance
* @var array
*/
protected $select;
/**
* The distinct statement
* @var boolean
*/
protected $distinct;
/**
* The join statements
* @var array
*/
protected $join;
/**
* The where statement
* @var array
*/
protected $where;
/**
* The search query filter
* @var string
*/
protected $filter;
/*
* The orderBy column and direction
* @var array
*/
protected $orderBy;
/*
* The groupBy column
* @var array
*/
protected $groupBy;
/*
* The withCount statement
* @var array
*/
protected $withCount;
/**
* The limit and offset
* @var array
*/
protected $limit;
/**
* The final get function override
* @var string
*/
protected $with;
/**
* Add a join to the query
* @param string $table
* @param string $one
* @param string $operator
* @param string $two
* @return $this
*/
public function join($table, $one, $operator, $two)
{
$one = $this->map($one);
$two = $this->map($two);
// Translate table
if ($table != $this->attributes('table')) {
$manager = app($this->realNamespace());
$table = $manager->$table->store->attributes('table');
}
$this->join[] = [
'table' => $table,
'one' => $one,
'operator' => $operator,
'two' => $two
];
return $this;
}
/**
* Set a new select statement
* @param array
* @return $this
*/
public function select($columns)
{
$columns = is_array($columns) ? $columns : func_get_args();
$this->select = $this->map($columns);
return $this;
}
/**
* Set distinct statement
* @param boolean $distinct = true
* @return $this
*/
public function distinct($distinct = true)
{
$this->distinct = $distinct;
return $this;
}
/**
* Set withCount statement
* @param boolean $withCount = true
* @return $this
*/
public function withCount($withCount = true)
{
$this->withCount = $withCount;
return $this;
}
/**
* Add additional select(s) to the query
* @param array
* @return $this
*/
public function addSelect($columns)
{
$columns = is_array($columns) ? $columns : func_get_args();
$this->select[] = $this->map($columns);
return $this;
}
/**
* Add a where clause to the query
* @param string $column
* @param string $operator
* @param mixed $value
* @return $this
*/
public function where($column, $operator = null, $value = null)
{
// Only allow operators that ALL stores can utilize (no like, between...)
$operators = [
'=', '!=', '<>',
'>', '<', '>=', '<=',
'like', '!like', 'not like',
'in', '!in', 'not in',
'null'
];
if (func_num_args() >= 2) {
if (func_num_args() == 2) {
list($value, $operator) = array($operator, '=');
}
if (!in_array(strtolower($operator), $operators)) {
$operator = "=";
}
$this->where[] = [
'column' => $this->map($column),
'operator' => $operator,
'value' => $value,
];
}
return $this;
}
/**
* Set the search query filter
* @param string
* @return $this
*/
public function filter($query)
{
$this->filter = $query;
return $this;
}
/**
* Set the orderBy column and direction for the query
* @param string $column
* @param string $direction = 'asc'
* @return $this
*/
public function orderBy($column, $direction = 'asc')
{
// $this->orderBy = [
// 'column' => $this->map($column),
// 'direction' => $direction
// ];
$this->orderBy[] = [
'column' => $this->map($column),
'direction' => $direction
];
return $this;
}
/**
* Set the groupBy column
* @param string $column
* @return $this
*/
public function groupBy($column)
{
$this->groupBy[] = $this->map($column);
return $this;
}
/**
* Set the limit and offset for the query
* @param integer $offset
* @param integer $limit
* @return $this
*/
public function limit($offset, $limit)
{
$this->limit = ['skip' => $offset, 'take' => $limit];
return $this;
}
/**
* Set the offset for the query
* @param integer $offset
* @return $this
*/
public function skip($offset)
{
$this->limit['skip'] = $offset;
return $this;
}
/**
* Set the limit for the query
* @param integer $limit
* @return $this
*/
public function take($limit)
{
$this->limit['take'] = $limit;
return $this;
}
/**
* Add a with relationship for the query
* @param string $properties
* @return $this
*/
public function with($properties)
{
if (!is_array($properties)) {
$properties = func_get_args();
}
foreach ($properties as $property) {
if (is_null($this->with) || !in_array($property, $this->with)) {
$this->with[] = $property;
}
}
return $this;
}
/**
* Check if with has been set
* @param string $with
* @return boolean
*/
public function hasWith($with)
{
return in_array($with, $this->with);
}
/**
* Find a record by id or key
* @param mixed $id
* @return object
*/
public function find($id)
{
if (isset($id)) {
// Determine ID Columns
$idColumn = $this->map($this->attributes('primary'), true);
if (!is_numeric($id)) {
$idColumn = 'key';
} //fixme
return $this->where($idColumn, $id)->first();
}
}
/**
* Get first single record in query
* @return object
*/
public function first()
{
$results = $this->get($first = true);
return $results;
}
/**
* Get a key/value list
* @param string $column
* @param string $key = null
* @return \Illuminate\Support\Collection
*/
public function pluck($column, $key = null)
{
$results = $this->select($column, $key)->get();
if (isset($results)) return $results->pluck($column, $key);
}
/**
* Insert one or multiple records by array or collection
* @param object $entity
* @param \Illuminate\Support\Collection|array $data
* @return array|object|boolean
*/
public function insert($entity, $data)
{
// Empty data
if (!isset($data) || count($data) == 0) {
return;
}
// Single Record assoc array
if (is_array($data) && array_keys($data) !== range(0, count($data) - 1)) {
// Convert to an entity object then save
foreach ($data as $key => $value) {
$entity->$key = $value;
}
return $this->save($entity);
}
// Convert collection to array
if ($data instanceof Collection) {
$data = $data->toArray();
}
// Single object (one entity), just save it
if (is_object($data) && get_class($data) == get_class($entity)) {
return $this->save($data);
}
// Is a non-entity object convert to standard object than save
if (is_object($data)) {
// Convert to an entity object just in case it isn't one then save
foreach ($data as $key => $value) {
$entity->$key = $value;
}
return $this->save($entity);
}
// Array of objects or array of arrays
if (is_object(head($data)) && get_class(head($data)) == get_class($entity)) {
// Data is an array of entity objects
return $this->save($data);
}
// Convert array of arrays or array of objects into array of entity objects
array_walk($data, function (&$item) use ($entity) {
$entity = $entity->newInstance();
foreach ($item as $key => $value) {
$entity->$key = $value;
}
$item = $entity;
});
return $this->save($data);
}
/**
* Update a record by array
* @param object $entity
* @param array|object $data
* @return array|object|boolean
*/
public function update($entity, $data)
{
// Single entity object
if (is_object($data) && get_class($data) == get_class($entity)) {
// Data is already a single entity object
return $this->save($data);
}
// Array of non-objects, amend this entity
if (is_array($data)) {
if (isset($this->where)) {
return $this->amend($data);
} else {
throw new Exception("Must specify a WHERE for bulk entity updates.");
}
}
}
/**
* Delete one or multiple records by array or collection
* @param object $entity
* @param \Illuminate\Support\Collection|array|null $data
* @return array|object|boolean
*/
public function delete($entity, $data = null)
{
// Deleting from a query builder (most efficient)
if (!isset($data)) {
if (isset($this->where)) {
return $this->destroy($data);
} else {
throw new Exception("Must specify a WHERE for deletes or use ->truncate() to delete the entire table.");
}
}
// Convert collection to array
if ($data instanceof Collection) {
$data = $data->toArray();
}
// Single object (one entity), just delete it
if (is_object($data)) {
return $this->destroy($data);
}
// Array of objects or array of arrays
if (is_object(head($data)) && get_class(head($data)) == get_class($entity)) {
// Data is an array of entity objects
return $this->destroy($data);
}
// Convert array of arrays or array of objects into array of entity objects
array_walk($data, function (&$item) use ($entity) {
$entity = $entity->newInstance();
foreach ($item as $key => $value) {
$entity->$key = $value;
}
$item = $entity;
});
return $this->destroy($data);
}
/**
* Add a where attributes clause to the query
* @param string $column
* @param mixed $value
* @return \Illuminate\Support\Collection
*/
public function whereAttribute($column, $value = null)
{
// Attributes "entity_key" is primary OR key_by override if defined, must use this->getKeyBy()
$keyBy = $this->getKeyBy();
$entity = $this->attributes('entity');
// Query attribute index
if (isset($value)) {
// Query by actual attribute value
$index = $this->manager()->attributeIndex->select('entityKey')->where('entity', $entity)->where('index', $column)->where('value', $value);
} else {
// Query if attribute exists at all
$index = $this->manager()->attributeIndex->select('entityKey')->where('entity', $entity)->where('index', $column);
}
// Get array of matching entityKeys
$index = $index->get();
if (isset($index)) {
// Add where to entity query matching these found entityKeys
$this->where($keyBy, 'in', $index->pluck('entityKey'));
} else {
// Want to return nothing, no attribute match, no entity return
$this->where($keyBy, null);
}
return $this;
}
/**
* Merge entity attributes (not store attributes) subentity with client
* @param \Illuminate\Support\Collection $entities
*/
protected function mergeAttributes($entities)
{
// Attributes "entity_key" is primary OR key_by override if defined, must use this->getKeyBy()
$keyBy = $this->getKeyBy();
$entity = $this->attributes('entity');
// Attributes not available/enabled for this entity if store relationship not defined
if (is_null($this->properties('attributes'))) return;
$attributes = $this->manager()->attribute->where('entity', $entity)->where('entityKey', 'in', $entities->pluck($keyBy)->all())->get();
if (isset($attributes)) {
foreach ($attributes as $attribute) {
$entities[$attribute->entityKey]->attributes = json_decode($attribute->value, true);
}
}
}
/**
* Translate entity property names to store column names (or visa versa)
* @param array|string $items
* @param boolean $reverse = false reverse the translation
* @param array $map = null use alternate store attributes map
* @return array
*/
public function map($items, $reverse = false, $map = null)
{
$map = isset($map) ? $map : $this->attributes('map');
$translated = [];
$isArray = is_array($items);
if (!$isArray) {
$items = [$items];
}
if (!$reverse) {
// Translate entity.property into store table.column (ex: addressID to address_id)
foreach ($items as $item) {
$thisMap = $map;
$table = $this->attributes('table');
// Get entity attributes from selection name (id, clients.*...)
if (str_contains($item, '.')) {
// Map is potentially from another entity
list($table, $item) = explode('.', $item);
if ($table != $this->attributes('table')) {
// Get map and table name form another entity
$entity = $table;
if (isset($map[$entity])) {
$options = $map[$entity];
if (isset($options['entity'])) {
$table = $options['table'];
$entity = $options['entity'];
}
}
$manager = $this->manager($entity);
$thisMap = $manager->$entity->store->attributes('map');
$table = $manager->$entity->store->attributes('table');
}
}
// Translate property=>column
if ($item == '*') {
$translated[] = "$table.$item";
} else {
foreach ($thisMap as $property => $options) {
$column = isset($options['column']) ? $options['column'] : null;
if ($item == $property) {
if (isset($column)) {
if (str_contains($column, '.')) {
// Map has . in it...use that override instead of default table
$translated[] = $column;
} else {
$translated[] = "$table.$column";
}
}
break;
}
}
}
}
} else {
// Translate store table.column into entity.property (ex: address_id into addressID)
foreach ($items as $item) {
$thisMap = $map;
$table = null;
if (str_contains($item, '.')) {
list($table, $item) = explode('.', $item);
if ($table != $this->attributes('table')) {
// From another entity
$entity = $table;
foreach ($map as $property => $options) {
if (isset($options['table'])) {
if ($options['table'] == $table) {
$table = $property;
$entity = $options['entity'];
}
}
}
$manager = $this->manager($entity);
$thisMap = $manager->$entity->store->attributes('map');
} else {
// From same entity, reset table
$table = null;
}
}
// Translate table.column=>entity.property
if ($item == '*') {
$translated[] = "$table.$item";
} else {
foreach ($thisMap as $property => $options) {
if (isset($options['column']) && $options['column'] == $item) {
if (!isset($table)) {
$translated[] = $property;
} else {
$translated[] = "$table.$property";
}
break;
}
}
}
}
}
if ($isArray) {
return $translated;
} else {
// FIXME, why if empty return items[0]?? can't remember
// test later once you get proper unit tests
// at this point, if you entered ->where('actual_column') instead of
// ->where('entityColumn') it actually STILL works. This means you can use
// either property or column in your wheres, which I DON'T want as this lets
// you bypass the entire point of an entity mappper.
return empty($translated) ? $items[0] : $translated[0];
}
}
/**
* Perform a query transaction
* @param function $callback
* @param $collect = true run through $this->collect()
* @return mixed
*/
protected function transaction($callback, $collect = true)
{
// Perform transaction
$results = call_user_func($callback);
// Collect Results
if ($collect && isset($results)) {
$results = $this->collect($results);
}
// Reset transaction defaults
$this->resetQuery();
return $results;
}
/**
* Collect and transform store results
* @param array|object $results
* @return null|entity|\Illuminate\Support\Collection
*/
protected function collect($results)
{
if (is_array($results) || $results instanceof Collection) {
// Results is multi row
$entities = [];
foreach ($results as $result) {
#if (is_array($result)) $result = (object) $result;
$entities[] = $this->transformStore($result);
}
return $this->keyByPrimary($entities);
} else {
// Results is single row find() one
return $this->transformStore($results);
}
}
/**
* Transform one store record into one entity object
* @param array $store
* @return object
*/
protected function transformStore($store)
{
if (isset($store)) {
// If scalar from min, max, avg aggregates, just return that value
if (is_string($store) || is_numeric($store)) return $store;
if (isset($this->select)) {
// Using custom select, cannot return entity, return stdClass
$entity = new stdClass();
// Predefine empty $entity object to force order of class properties
foreach ($store as $key => $value) {
$mappedKey = $this->map($key, true);
$entity->$mappedKey = null;
}
} else {
// Using default select (*), transform into entity object
$entity = $this->newEntity();
}
$map = $this->attributes('map');
$unmappedKeys = get_object_vars($store);
foreach ($map as $property => $options) {
$value = null;
$type = isset($options['type']) ? $options['type'] : null;
$column = isset($options['column']) ? $options['column'] : null;
$filter = isset($options['filter']) ? $options['filter'] : null;
if (isset($column) && property_exists($store, $column)) {
// This is a database column which has been selected
// Its value is from the actual column or from a filter
if (isset($filter)) {
$value = call_user_func($filter, $store);
} else {
$value = $store->$column;
}
} elseif (!isset($column) && isset($filter)) {
// This is a virtual column which has been selected
// Its only value is from a filter
$value = call_user_func($filter, $store);
}
if (property_exists($entity, $property)) {
unset($unmappedKeys[$column]);
// Convert decimal PDO values to floats
if ($type == 'decimal') {
// PHP PDO returns decimals as double quoted "strings". We don't want that, convert to float
// Check if null, can have nullable decimals and don't want to (float) those
if (isset($value)) {
$value = (float) $value;
}
}
$entity->$property = $value;
}
}
// If using custom select or join there will be other unmapped keys
if (isset($this->select)) {
foreach ($unmappedKeys as $key => $value) {
$entity->$key = $value;
}
}
// Call a postTransformStore method if exists
if (method_exists($this, 'postTransformStore')) {
$this->postTransformStore($entity);
}
return $entity;
}
}
/**
* Transform one entity object into one store model
* @param object $entity
* @return array
*/
protected function transformEntity($entity)
{
if (isset($entity)) {
$map = $this->attributes('map');
$store = [];
foreach ($entity as $key => $value) {
foreach ($map as $property => $options) {
if (isset($options['column']) && $property == $key) {
if (!isset($options['save']) || $options['save'] == true) {
$store[$options['column']] = $entity->$key;
break;
}
}
}
}
return $store;
}
}
/**
* Select columns from a collection (post query)
* @param \Illuminate\Support\Collection $items
* @param array $select must be entity columns, not store columns so reverse map
* @return \Illuminate\Support\Collection
*/
public function selectCollection($items, $select)
{
if (isset($select)) {
// Custom select, return modify collection
$items = $items->map(function ($item) use ($select) {
$newItem = new stdClass();
foreach ($select as $column) {
if (str_contains($column, '.*')) {
// Just remove .* and pickup entire subentity
$column = head(explode('.', $column));
}
if (str_contains($column, '.')) {
// Sub entity
list($subentity, $subproperty) = explode('.', $column);
// Careful. Potential lazy load here. Ex ->select('address.state')->with(address) the state will lazy load from address
if (isset($item->$subentity)) {
if (!isset($newItem->$subentity)) {
$newItem->$subentity = new stdClass();
}
$newItem->$subentity->$subproperty = $item->$subentity->$subproperty; //subclass
}
} else {
$newItem->$column = $item->$column;
}
}
return $newItem;
});
}
return $items;
}
/**
* Unflatten collection (from address.city to address->city)
* @param \Illuminate\Support\Collection $items
* @return \Illuminate\Support\Collection
*/
protected function expandCollection($items)
{
if (isset($items)) {
$results = [];
foreach ($items as $item) {
$result = new stdClass();
foreach ($item as $key => $value) {
if (str_contains($key, '.')) {
list($key, $subkey) = explode('.', $key);
if (!isset($result->$key)) {
$result->$key = new stdClass();
}
$result->$key->$subkey = $value;
} else {
$result->$key = $value;
}
}
$results[] = $result;
}
return collect($results);
}
}
/**
* Key entities collection by primary or override key_by attribute if set, else no keyby
* @param array $entities
* @return \Illuminate\Support\Collection|null
*/
protected function keyByPrimary($entities)
{
if (count($entities) > 0) {
$keyBy = $this->getKeyBy();
if (property_exists($entities[0], $keyBy)) {
// Re-key assoc array by primary
return collect($entities)->keyBy($keyBy);
} else {
return collect($entities);
}
}
}
/**
* Resets the query properties
* @return void
*/
protected function resetQuery()
{
$this->select = null;
$this->join = null;
$this->where = null;
$this->filter = null;
$this->orderBy = null;
$this->groupBy = null;
$this->withCount = null;
$this->limit = null;
//do NOT reset ->with here, we do it in mergeWiths properly
}
/**
* Get one or all store attributes
* @param string $key = null
* @return mixed
*/
public function attributes($key = null)
{
// Special defaults
if ($key == 'order_by_dir' && !isset($this->attributes[$key])) {
return 'asc';
}
if (is_null($key)) {
// Return all attributes
return $this->attributes;
} elseif (isset($key) && isset($this->attributes[$key])) {
// Return individual attribute
return $this->attributes[$key];
}
}
/**
* Get keyBy property (not column name)
* Keyby is primary unless key_by override exists
* @return
*/
public function getKeyBy()
{
// Keyby is primary unless key_by override exists
// key_by value is the entity property, NOT the column name like 'primary' is, so do NOT ->map it
return $this->attributes('key_by') ?: $this->map($this->attributes('primary'), true);
}
/**
* Get store notes
* @return mixed
*/
public function notes()
{
return $this->notes;
}
/**
* Get one or all store map properties and options
* @param string $property = null
* @param string $option = null
* @return mixed
*/
public function properties($property = null, $option = null)
{
if (isset($property)) {
$map = $this->attributes('map');
if (isset($map[$property])) {
if (isset($option) && isset($map[$property][$option])) {
return $map[$property][$option];
}
return $map[$property];
}
} else {
return $this->attributes('map');
}
}
/**
* Get a query builder for this table
* @param string $table = null
* @return \Illuminate\Database\Query\Builder
*/
protected function table($table = null)
{
if (is_null($table)) {
$table = $this->attributes('table');
}
return $this->connection->table("$table");
}
/**
* Get new entity instance maintaining the last used store driver
* @return object
*/
protected function newEntity()
{
$entity = $this->attributes('entity');
return $this->manager->store($this->storeKey)->$entity->newInstance();
}
/**
* Get manager for entity
* @param string $entity attempt to detect inherited properly based on entity location
* @param boolean $inherited = true, get the inherited namespace if this entity inherits from another
* @return object
*/
protected function manager($entity = null, $inherited = true)
{
if (!isset($entity)) {
$entity = $this->attributes('entity');
}
if ($inherited) {
// Get inherited (if applies) manager
$manager = app($this->realNamespace());
if (is_null($manager->$entity)) {
// Entity not found in realNamespace, try inherited namespace
$manager = app($this->manager->namespace);
if (is_null($manager->$entity)) {
// Entity not found
return null;
}
}
return $manager;