-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftMapper.php
More file actions
1041 lines (915 loc) · 34.6 KB
/
SoftMapper.php
File metadata and controls
1041 lines (915 loc) · 34.6 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
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
/**
* Created by PhpStorm.
* User: Mohammed Elamin
* Date: 12/12/2018
* Time: 06:33
*/
require_once 'env.php';
class SoftMapper
{
/**
* PDO instance for database operations
* @var PDO
*/
private $pdo;
/**
* Initial query for complex queries
* @var string
*/
private $built_query = 'SELECT * FROM ';
/**
* Selected columns specified by select function
* @var string
*/
private $selected_columns = '';
/**
* Values for columns used by prepared statement
* @var array
*/
private $query_columns_place_holder_array;
/**
* Table name for the model
* @var string
*/
public $table_name;
/**
* Entity columns
* @var array
*/
public $columns;
/**
* Primary key column name
* @var string
*/
protected $primary_key = 'id';
/**
* Enable automatic timestamp management
* @var bool
*/
protected $timestamps = true;
/**
* Enable soft delete functionality
* @var bool
*/
protected $soft_deletes = false;
/**
* Custom query scopes
* @var array
*/
protected $scopes = [];
/**
* Defined relationships
* @var array
*/
public $relationships = [];
/**
* Loaded relationships data
* @var array
*/
public $loaded_relations = [];
/**
* @var bool
*/
private $update_switch = false;
/**
* @var bool
*/
private $with_trashed = false;
/**
* SoftMapper constructor.
* Initializes database connection using environment configuration
*/
public function __construct()
{
$dsn = 'mysql:host=' . host . ';dbname=' . dbname;
$this->pdo = new PDO($dsn, user, password);
}
/**
* Get all rows from table
* @return $this
*/
public function all()
{
$this->built_query .= $this->table_name;
return $this;
}
/**
* Apply soft delete filter if needed
* @return void
*/
private function applySoftDeleteFilter()
{
if ($this->soft_deletes && !$this->with_trashed) {
// Add soft delete filter to existing WHERE clause
if (strpos($this->built_query, 'where') !== false || strpos($this->built_query, 'WHERE') !== false) {
$this->built_query .= "\t AND \t deleted_at IS NULL";
} else {
$this->built_query .= "\t WHERE \t deleted_at IS NULL";
}
}
}
/**
* Select rows from table with optional aggregate functions
* @param array $columns Columns to select
* @param string $aggregate_function Aggregate function (e.g., COUNT, SUM, AVG)
* @param string $aggregate_parameter Parameter for aggregate function
* @return $this
*/
public function select($columns = array(), $aggregate_function = '', $aggregate_parameter = null)
{
// Reset old query for selection
$this->built_query = '';
if (!empty($columns)) {
$this->selected_columns .= implode(',', $columns);
if (!empty($aggregate_function))
$this->built_query .= 'SELECT' . "\t" . $this->selected_columns . ",\t" . $aggregate_function . '(' . $aggregate_parameter . ')' . "\t FROM \t" . $this->table_name;
else
$this->built_query .= 'SELECT' . "\t" . $this->selected_columns . "\t FROM \t" . $this->table_name;
} else {
if (!empty($aggregate_function))
$this->built_query .= 'SELECT' . "\t" . $aggregate_function . '(' . $aggregate_parameter . ')' . "\t FROM \t" . $this->table_name;
}
return $this;
}
/**
* Execute query and fetch all results
* This method follows most methods included here to execute generated query
* @return array
*/
public function getAll()
{
$this->applySoftDeleteFilter();
$stmt = $this->pdo->prepare($this->built_query);
$stmt->execute($this->query_columns_place_holder_array);
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
// Load eager relationships if specified
$result = $this->loadEagerRelations($result);
return $result;
}
/**
* Execute query and fetch single result
* @return mixed
*/
public function get()
{
$this->applySoftDeleteFilter();
$stmt = $this->pdo->prepare($this->built_query);
$stmt->execute($this->query_columns_place_holder_array);
$result = $stmt->fetch(PDO::FETCH_OBJ);
return $result;
}
/**
* Execute the built query
* @return bool
*/
public function execute()
{
$stmt = $this->pdo->prepare($this->built_query);
$result = $stmt->execute($this->query_columns_place_holder_array);
return $result;
}
/**
* Add conditions to the built query
* @param array $conditions Array of conditions [[column, operator, value, logical_operator], ...]
* @return $this
*/
public function where($conditions = array())
{
$this->query_columns_place_holder_array = array();
$where_query = "\t" . 'where' . "\t";
for ($i = 0; $i < sizeof($conditions); $i++) {
$values = $conditions[$i];
$index = $values[0];
$this->query_columns_place_holder_array[$index] = $values[2];
if (isset($values[3]))
$where_query .= "\t" . $values[0] . $values[1] . ' :' . $values[0] . "\t" . $values[3];
else
$where_query .= "\t" . $values[0] . $values[1] . ' :' . $values[0];
}
$this->built_query .= $where_query;
if ($this->update_switch) {
$keys = array_keys($this->columns);
$values = array_values($this->columns);
// Add update data to be added to prepared statement
for ($i = 0; $i < sizeof($this->columns); $i++)
$this->query_columns_place_holder_array['UP_' . $keys[$i]] = $values[$i];
}
return $this;
}
/**
* Add ORDER BY clause to query
* @param string $column_name Column to order by
* @param string $ordering_type Order type (ASC or DESC)
* @return $this
*/
public function orderBy($column_name, $ordering_type)
{
$this->built_query .= "\t" . 'order by' . "\t" . $column_name . "\t" . $ordering_type;
return $this;
}
/**
* Limit number of selected rows
* @param int $limitation_number Number of rows to limit
* @return $this
*/
public function limit($limitation_number)
{
$this->built_query .= "\t" . 'LIMIT' . "\t" . $limitation_number;
return $this;
}
/**
* Offset for pagination (use with limit)
* @param int $offset_number Number of rows to skip
* @return $this
*/
public function offset($offset_number)
{
$this->built_query .= "\t" . 'OFFSET' . "\t" . $offset_number;
return $this;
}
/**
* Group results by column (used with aggregate functions)
* @param string $grouper Column to group by
* @return $this
*/
public function groupBy($grouper)
{
$this->built_query .= "\t group by \t" . $grouper;
return $this;
}
/**
* Add HAVING clause (used as condition for aggregate functions)
* @param array $conditions Array of conditions [[column, operator, value, logical_operator], ...]
* @return $this
*/
public function having($conditions = array())
{
$this->query_columns_place_holder_array = array();
$having_query = "\t" . 'HAVING' . "\t";
for ($i = 0; $i < sizeof($conditions); $i++) {
$values = $conditions[$i];
$index = $values[0];
$this->query_columns_place_holder_array[$index] = $values[2];
if (isset($values[3]))
$having_query .= "\t" . $values[0] . $values[1] . ' :' . $values[0] . "\t" . $values[3];
else
$having_query .= "\t" . $values[0] . $values[1] . ' :' . $values[0];
}
$this->built_query .= $having_query;
return $this;
}
/**
* Insert data into table
* @return bool
*/
public function insert()
{
// Add timestamps if enabled
if ($this->timestamps) {
if (!isset($this->columns['created_at'])) {
$this->columns['created_at'] = date('Y-m-d H:i:s');
}
if (!isset($this->columns['updated_at'])) {
$this->columns['updated_at'] = date('Y-m-d H:i:s');
}
}
$columns = array_keys($this->columns);
$values_indicator = array();
for ($i = 0; $i < sizeof($columns); $i++) {
array_push($values_indicator, ':' . $columns[$i]);
}
$values_pointers = implode(',', $values_indicator);
$columns_name = implode(',', $columns);
$query = 'INSERT INTO ' . $this->table_name . "\t" . '(' . $columns_name . ') ' . "values (" . $values_pointers . ')';
$stmt_insert = $this->pdo->prepare($query);
$insert_a_row = $stmt_insert->execute($this->columns);
return $insert_a_row;
}
/**
* Find a record by its primary key (id)
* @param mixed $key Primary key value
* @return mixed
*/
public function find($key)
{
return $result = $this->all()->where([[$this->primary_key, '=', $key]])->get();
}
/**
* Delete records from table
* @param bool $force Force delete (ignore soft deletes)
* @return $this
*/
public function delete($force = false)
{
// Use soft delete if enabled and not forced
if ($this->soft_deletes && !$force) {
$this->columns['deleted_at'] = date('Y-m-d H:i:s');
if ($this->timestamps) {
$this->columns['updated_at'] = date('Y-m-d H:i:s');
}
return $this->update();
}
$this->built_query = "DELETE FROM\t" . $this->table_name;
return $this;
}
/**
* Update records in table
* @return $this
*/
public function update()
{
// Add updated_at timestamp if enabled
if ($this->timestamps && !isset($this->columns['updated_at'])) {
$this->columns['updated_at'] = date('Y-m-d H:i:s');
}
$keys = array_keys($this->columns);
$values = array_values($this->columns);
$keys_for_prepare = array();
for ($i = 0; $i < sizeof($keys); $i++) {
array_push($keys_for_prepare, $keys[$i] . "=:" . 'UP_' . $keys[$i] . "\t");
}
$query = 'UPDATE ' . $this->table_name . "\t" . 'SET ' . implode(',', $keys_for_prepare);
$this->built_query = $query;
$this->update_switch = true;
return $this;
}
/**
* Include soft deleted records in query results
* @return $this
*/
public function withTrashed()
{
$this->with_trashed = true;
return $this;
}
/**
* Get only soft deleted records
* @return $this
*/
public function onlyTrashed()
{
if ($this->soft_deletes) {
$this->with_trashed = true; // Don't filter in applySoftDeleteFilter
if (strpos($this->built_query, $this->table_name) === false) {
$this->built_query .= $this->table_name;
}
$this->built_query .= "\t WHERE \t deleted_at IS NOT NULL";
}
return $this;
}
/**
* Restore soft deleted records
* @return $this
*/
public function restore()
{
if ($this->soft_deletes) {
$this->columns['deleted_at'] = NULL;
if ($this->timestamps) {
$this->columns['updated_at'] = date('Y-m-d H:i:s');
}
return $this->update();
}
return $this;
}
/**
* Insert multiple records at once
* @param array $records Array of associative arrays with column => value
* @return bool
*/
public function insertMany($records)
{
if (empty($records)) {
return false;
}
$pdo = $this->pdo;
$pdo->beginTransaction();
try {
foreach ($records as $record) {
$this->columns = $record;
$this->insert();
}
$pdo->commit();
return true;
} catch (Exception $e) {
$pdo->rollBack();
return false;
}
}
/**
* WHERE IN clause
* @param string $column Column name
* @param array $values Array of values
* @return $this
*/
public function whereIn($column, $values)
{
if (empty($values)) {
return $this;
}
$placeholders = [];
$this->query_columns_place_holder_array = $this->query_columns_place_holder_array ?: [];
foreach ($values as $index => $value) {
$placeholder = $column . '_' . $index;
$placeholders[] = ':' . $placeholder;
$this->query_columns_place_holder_array[$placeholder] = $value;
}
$this->built_query .= "\t WHERE \t" . $column . ' IN (' . implode(',', $placeholders) . ')';
return $this;
}
/**
* WHERE NOT IN clause
* @param string $column Column name
* @param array $values Array of values
* @return $this
*/
public function whereNotIn($column, $values)
{
if (empty($values)) {
return $this;
}
$placeholders = [];
$this->query_columns_place_holder_array = $this->query_columns_place_holder_array ?: [];
foreach ($values as $index => $value) {
$placeholder = $column . '_' . $index;
$placeholders[] = ':' . $placeholder;
$this->query_columns_place_holder_array[$placeholder] = $value;
}
$this->built_query .= "\t WHERE \t" . $column . ' NOT IN (' . implode(',', $placeholders) . ')';
return $this;
}
/**
* WHERE BETWEEN clause
* @param string $column Column name
* @param mixed $start Start value
* @param mixed $end End value
* @return $this
*/
public function whereBetween($column, $start, $end)
{
$this->query_columns_place_holder_array = $this->query_columns_place_holder_array ?: [];
$this->query_columns_place_holder_array[$column . '_start'] = $start;
$this->query_columns_place_holder_array[$column . '_end'] = $end;
$this->built_query .= "\t WHERE \t" . $column . ' BETWEEN :' . $column . '_start AND :' . $column . '_end';
return $this;
}
/**
* WHERE NULL clause
* @param string $column Column name
* @return $this
*/
public function whereNull($column)
{
$this->built_query .= "\t WHERE \t" . $column . ' IS NULL';
return $this;
}
/**
* WHERE NOT NULL clause
* @param string $column Column name
* @return $this
*/
public function whereNotNull($column)
{
$this->built_query .= "\t WHERE \t" . $column . ' IS NOT NULL';
return $this;
}
/**
* Get the first record
* @return mixed
*/
public function first()
{
return $this->limit(1)->get();
}
/**
* Count records
* @return int
*/
public function count()
{
$result = $this->select([], 'COUNT', '*')->get();
return $result ? (int)$result->{'COUNT(*)'} : 0;
}
/**
* Check if records exist
* @return bool
*/
public function exists()
{
return $this->count() > 0;
}
/**
* Pluck a single column's values
* @param string $column Column name
* @return array
*/
public function pluck($column)
{
$results = $this->select([$column])->getAll();
return array_map(function($item) use ($column) {
return $item->$column;
}, $results);
}
/**
* Define a query scope
* @param string $name Scope name
* @param callable $callback Callback function that modifies the query
* @return void
*/
public function scope($name, $callback)
{
$this->scopes[$name] = $callback;
}
/**
* Apply a query scope
* @param string $name Scope name
* @param mixed ...$args Additional arguments for the scope
* @return $this
*/
public function applyScope($name, ...$args)
{
if (isset($this->scopes[$name])) {
call_user_func_array($this->scopes[$name], array_merge([$this], $args));
}
return $this;
}
/**
* Start a database transaction
* @return bool
*/
public function beginTransaction()
{
return $this->pdo->beginTransaction();
}
/**
* Commit a database transaction
* @return bool
*/
public function commit()
{
return $this->pdo->commit();
}
/**
* Rollback a database transaction
* @return bool
*/
public function rollback()
{
return $this->pdo->rollBack();
}
/**
* Execute a raw SQL query
* @param string $query SQL query
* @param array $params Parameters for prepared statement
* @return mixed
*/
public function raw($query, $params = [])
{
$stmt = $this->pdo->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
/**
* Get the last inserted ID
* @return string
*/
public function lastInsertId()
{
return $this->pdo->lastInsertId();
}
/**
* Chunk results for memory-efficient processing
* @param int $size Chunk size
* @param callable $callback Callback to process each chunk
* @return void
*/
public function chunk($size, $callback)
{
$page = 0;
do {
$results = $this->limit($size)->offset($page * $size)->getAll();
if (empty($results)) {
break;
}
call_user_func($callback, $results);
$page++;
} while (count($results) === $size);
}
/**
* Simple JOIN support
* @param string $table Table to join
* @param string $first First column
* @param string $operator Operator
* @param string $second Second column
* @param string $type Join type (INNER, LEFT, RIGHT)
* @return $this
*/
public function join($table, $first, $operator, $second, $type = 'INNER')
{
$this->built_query .= "\t" . $type . ' JOIN ' . $table . ' ON ' . $first . $operator . $second;
return $this;
}
/**
* LEFT JOIN
* @param string $table Table to join
* @param string $first First column
* @param string $operator Operator
* @param string $second Second column
* @return $this
*/
public function leftJoin($table, $first, $operator, $second)
{
return $this->join($table, $first, $operator, $second, 'LEFT');
}
/**
* RIGHT JOIN
* @param string $table Table to join
* @param string $first First column
* @param string $operator Operator
* @param string $second Second column
* @return $this
*/
public function rightJoin($table, $first, $operator, $second)
{
return $this->join($table, $first, $operator, $second, 'RIGHT');
}
/**
* Get distinct records
* @return $this
*/
public function distinct()
{
$this->built_query = str_replace('SELECT', 'SELECT DISTINCT', $this->built_query);
return $this;
}
/**
* Create or update a record
* @param array $attributes Attributes to search for
* @param array $values Values to update or insert
* @return bool
*/
public function updateOrCreate($attributes, $values = [])
{
$conditions = [];
foreach ($attributes as $key => $value) {
$conditions[] = [$key, '=', $value];
}
$existing = $this->all()->where($conditions)->get();
if ($existing) {
$this->columns = array_merge($attributes, $values);
return $this->update()->where($conditions)->execute();
} else {
$this->columns = array_merge($attributes, $values);
return $this->insert();
}
}
// ==================== Relationship Methods ====================
/**
* Define a one-to-one relationship
* @param string $related_class Related model class name
* @param string $foreign_key Foreign key on related table (default: {this_table}_id)
* @param string $local_key Local key on this table (default: primary key)
* @return mixed
*/
public function hasOne($related_class, $foreign_key = null, $local_key = null)
{
$foreign_key = $foreign_key ?: $this->table_name . '_id';
$local_key = $local_key ?: $this->primary_key;
$this->relationships['hasOne'][] = [
'class' => $related_class,
'foreign_key' => $foreign_key,
'local_key' => $local_key
];
return $this;
}
/**
* Define a one-to-many relationship
* @param string $related_class Related model class name
* @param string $foreign_key Foreign key on related table (default: {this_table}_id)
* @param string $local_key Local key on this table (default: primary key)
* @return mixed
*/
public function hasMany($related_class, $foreign_key = null, $local_key = null)
{
$foreign_key = $foreign_key ?: $this->table_name . '_id';
$local_key = $local_key ?: $this->primary_key;
$this->relationships['hasMany'][] = [
'class' => $related_class,
'foreign_key' => $foreign_key,
'local_key' => $local_key
];
return $this;
}
/**
* Define an inverse one-to-many relationship (belongs to)
* @param string $related_class Related model class name
* @param string $foreign_key Foreign key on this table (default: {related_table}_id)
* @param string $owner_key Primary key on related table (default: id)
* @return mixed
*/
public function belongsTo($related_class, $foreign_key = null, $owner_key = null)
{
$related = new $related_class();
$foreign_key = $foreign_key ?: $related->table_name . '_id';
$owner_key = $owner_key ?: $related->primary_key;
$this->relationships['belongsTo'][] = [
'class' => $related_class,
'foreign_key' => $foreign_key,
'owner_key' => $owner_key
];
return $this;
}
/**
* Define a many-to-many relationship
* @param string $related_class Related model class name
* @param string $pivot_table Pivot/junction table name (default: alphabetically ordered tables)
* @param string $foreign_pivot_key Foreign key for this model in pivot table
* @param string $related_pivot_key Foreign key for related model in pivot table
* @param string $parent_key Parent key on this table (default: primary key)
* @param string $related_key Related key on related table (default: primary key)
* @return mixed
*/
public function belongsToMany($related_class, $pivot_table = null, $foreign_pivot_key = null, $related_pivot_key = null, $parent_key = null, $related_key = null)
{
$related = new $related_class();
// Auto-generate pivot table name if not provided (alphabetically ordered)
if (!$pivot_table) {
$tables = [$this->table_name, $related->table_name];
sort($tables);
$pivot_table = implode('_', $tables);
}
$foreign_pivot_key = $foreign_pivot_key ?: $this->table_name . '_id';
$related_pivot_key = $related_pivot_key ?: $related->table_name . '_id';
$parent_key = $parent_key ?: $this->primary_key;
$related_key = $related_key ?: $related->primary_key;
$this->relationships['belongsToMany'][] = [
'class' => $related_class,
'pivot_table' => $pivot_table,
'foreign_pivot_key' => $foreign_pivot_key,
'related_pivot_key' => $related_pivot_key,
'parent_key' => $parent_key,
'related_key' => $related_key
];
return $this;
}
/**
* Load a relationship for a single model instance
* @param string $relation_name Name of the relationship method
* @param mixed $record The record to load relationships for
* @return mixed
*/
public function loadRelation($relation_name, $record)
{
if (!method_exists($this, $relation_name)) {
return $record;
}
// Get a fresh instance and call the relationship method to register it
$temp_instance = new static();
$temp_instance->$relation_name();
// Find the relationship definition
foreach ($temp_instance->relationships as $type => $relations) {
foreach ($relations as $relation) {
// Load based on relationship type
if ($type === 'hasOne') {
$related = new $relation['class']();
$result = $related->all()
->where([[$relation['foreign_key'], '=', $record->{$relation['local_key']}]])
->first();
$record->$relation_name = $result;
} elseif ($type === 'hasMany') {
$related = new $relation['class']();
$results = $related->all()
->where([[$relation['foreign_key'], '=', $record->{$relation['local_key']}]])
->getAll();
$record->$relation_name = $results;
} elseif ($type === 'belongsTo') {
$related = new $relation['class']();
$result = $related->find($record->{$relation['foreign_key']});
$record->$relation_name = $result;
} elseif ($type === 'belongsToMany') {
$related = new $relation['class']();
$pivot_table = $relation['pivot_table'];
$foreign_pivot_key = $relation['foreign_pivot_key'];
$related_pivot_key = $relation['related_pivot_key'];
$parent_key = $relation['parent_key'];
$related_key = $relation['related_key'];
// Query through pivot table
$query = "SELECT " . $related->table_name . ".* FROM " . $related->table_name .
" INNER JOIN " . $pivot_table .
" ON " . $related->table_name . "." . $related_key . " = " . $pivot_table . "." . $related_pivot_key .
" WHERE " . $pivot_table . "." . $foreign_pivot_key . " = :parent_id";
$results = $this->raw($query, ['parent_id' => $record->{$parent_key}]);
$record->$relation_name = $results;
}
}
}
return $record;
}
/**
* Eager load relationships for multiple records
* @param array $relations Array of relationship names to load
* @return $this
*/
public function with($relations = [])
{
if (!is_array($relations)) {
$relations = [$relations];
}
$this->loaded_relations = $relations;
return $this;
}
/**
* Override getAll to support eager loading
* @return array
*/
private function loadEagerRelations($results)
{
if (empty($this->loaded_relations) || empty($results)) {
return $results;
}
foreach ($results as $record) {
foreach ($this->loaded_relations as $relation_name) {
$this->loadRelation($relation_name, $record);
}
}
return $results;
}
/**
* Attach a related model in a many-to-many relationship
* @param mixed $id The ID of the parent record
* @param mixed $related_id The ID of the related record to attach
* @param string $relation_name Name of the belongsToMany relationship
* @param array $pivot_data Additional pivot table data
* @return bool
*/
public function attach($id, $related_id, $relation_name, $pivot_data = [])
{
// Get relationship definition
$temp_instance = new static();
$temp_instance->$relation_name();
if (!isset($temp_instance->relationships['belongsToMany'])) {
return false;
}
$relation = $temp_instance->relationships['belongsToMany'][0];
$pivot_table = $relation['pivot_table'];
$foreign_pivot_key = $relation['foreign_pivot_key'];
$related_pivot_key = $relation['related_pivot_key'];
// Insert into pivot table
$columns = array_merge([
$foreign_pivot_key => $id,
$related_pivot_key => $related_id
], $pivot_data);
$column_names = array_keys($columns);
$placeholders = array_map(function($col) { return ':' . $col; }, $column_names);
$query = "INSERT INTO " . $pivot_table . " (" . implode(',', $column_names) . ") VALUES (" . implode(',', $placeholders) . ")";
$stmt = $this->pdo->prepare($query);
return $stmt->execute($columns);
}
/**
* Detach a related model in a many-to-many relationship
* @param mixed $id The ID of the parent record
* @param string $relation_name Name of the belongsToMany relationship
* @param mixed $related_id The ID of the related record to detach (null to detach all)
* @return bool
*/
public function detach($id, $relation_name, $related_id = null)
{
// Get relationship definition
$temp_instance = new static();
$temp_instance->$relation_name();
if (!isset($temp_instance->relationships['belongsToMany'])) {