annotate src/Helpers/RelationshipNavigator.php @ 23:827efbf4d73c main-dev

Huge changes to the relationships for models and more complex
author Luka Sitas <sitas.luka.97@gmail.com>
date Fri, 11 Apr 2025 20:50:20 -0400
parents
children 31109c61ce02
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
1 <?php
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
2
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
3 namespace Wizard\MagicForger\Helpers;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
4
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
5 use Illuminate\Support\Facades\DB;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
6
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
7 class RelationshipNavigator
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
8 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
9 public static function handle()
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
10 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
11 $tables = DB::select('SHOW TABLES');
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
12 $tableNames = array_map(function ($table) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
13 return current((array) $table);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
14 }, $tables);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
15
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
16 foreach ($tableNames as $table) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
17 echo "Table: $table \n";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
18
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
19 $relations = self::getRelations($table);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
20 echo "Relationships: \n";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
21 foreach ($relations as $relation => $related_tables) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
22 echo $relation.": \n";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
23 foreach ($related_tables as $related_table) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
24 echo "\t";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
25 foreach ($related_table as $key => $value) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
26 echo "$key : ";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
27 if (is_array($value)) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
28 foreach ($related_table as $key => $value) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
29 echo "\n\t\t";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
30 echo "$key: $value";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
31 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
32 } else {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
33 echo $value.' ';
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
34 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
35 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
36 echo "\n";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
37 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
38 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
39 echo "\n --- \n";
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
40 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
41 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
42
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
43 public static function getRelations($table)
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
44 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
45 $relations = [
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
46 'belongsTo' => [],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
47 'hasMany' => [],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
48 'hasManyThrough' => [],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
49 ];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
50
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
51 $foreignKeys = DB::select("SHOW KEYS FROM $table WHERE Key_name != 'PRIMARY'");
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
52 $referencedTables = self::getAllReferencedTables($table);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
53
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
54 // BelongsTo Relationships
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
55 foreach ($foreignKeys as $fk) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
56 $column = $fk->Column_name;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
57
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
58 // skip created and updated by
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
59 if (in_array($column, ['created_by', 'updated_by'])) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
60 continue;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
61 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
62
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
63 $referencedTable = $referencedTables[$column] ?? null;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
64
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
65 if ($referencedTable) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
66 $relations['belongsTo'][] = ['column' => $column, 'table' => $referencedTable->REFERENCED_TABLE_NAME];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
67 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
68
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
69 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
70
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
71 // HasMany Relationships
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
72 if ($reverseRelation = self::findReverseRelation($table)) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
73 foreach ($reverseRelation as $relatedTable) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
74 $relations['hasMany'][] = $relatedTable;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
75 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
76 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
77
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
78 // HasManyThrough Relationships
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
79 if ($hasManyThroughRelations = self::findHasManyThroughRelations($table)) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
80 foreach ($hasManyThroughRelations as $relatedTable) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
81 $relations['hasManyThrough'][] = $relatedTable;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
82 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
83 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
84
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
85 return $relations;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
86 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
87
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
88 public static function getAllReferencedTables($table)
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
89 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
90 $results = DB::select("
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
91 SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
92 FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
93 WHERE TABLE_SCHEMA = DATABASE()
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
94 AND TABLE_NAME = '$table'
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
95 ");
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
96
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
97 $tables = self::re_key_array($results, 'COLUMN_NAME');
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
98
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
99 return (count($tables) > 0) ? $tables : null;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
100 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
101
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
102 public static function findReverseRelation($table)
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
103 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
104 $relations = DB::select("
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
105 SELECT TABLE_NAME, COLUMN_NAME
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
106 FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
107 WHERE TABLE_SCHEMA = DATABASE()
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
108 AND REFERENCED_TABLE_NAME = '$table'
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
109 AND REFERENCED_COLUMN_NAME = 'id'
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
110 AND COLUMN_NAME != 'created_by'
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
111 AND COLUMN_NAME != 'updated_by'
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
112 ");
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
113
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
114 $relatedTables = array_map(function ($rel) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
115 return ['table' => $rel->TABLE_NAME, 'column' => $rel->COLUMN_NAME];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
116 }, $relations);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
117
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
118 return $relatedTables ? $relatedTables : null;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
119 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
120
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
121 public static function findHasManyThroughRelations($table)
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
122 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
123 $relations = [];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
124
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
125 // Find potential intermediary tables
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
126 $intermediaryTables = self::findReverseRelation($table);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
127
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
128 if (! is_null($intermediaryTables)) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
129
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
130 foreach ($intermediaryTables as $intermediary) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
131
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
132 if ($is_pivot = self::isPivot($intermediary['table'])) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
133 $is_pivot = current($is_pivot);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
134
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
135 // reformat the table based on the current and external
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
136 $potential_tables = array_keys($is_pivot['tables']);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
137 $external_table = $potential_tables[0] == $table ? $is_pivot['tables'][$potential_tables[1]] : $is_pivot['tables'][$potential_tables[0]];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
138 $internal_table = $potential_tables[0] == $table ? $is_pivot['tables'][$potential_tables[0]] : $is_pivot['tables'][$potential_tables[1]];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
139
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
140 $hasManyThrough = [
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
141 'table' => $external_table['table_name'],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
142 'through' => [
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
143 'table' => $is_pivot['table'],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
144 'external_column' => $external_table['column'],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
145 'internal_column' => $internal_table['column']
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
146 ]
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
147 ];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
148 $relations[] = $hasManyThrough;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
149 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
150 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
151
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
152 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
153
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
154 return $relations;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
155 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
156
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
157 public static function isPivot($table)
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
158 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
159
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
160 $relations = [];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
161 // TODO: alsot get the columns that are relevant
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
162 $pivotTables = DB::select("
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
163 SELECT TABLE_NAME, TABLE_COMMENT
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
164 FROM INFORMATION_SCHEMA.TABLES
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
165 WHERE TABLE_SCHEMA = DATABASE()
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
166 AND TABLE_NAME = '{$table}'
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
167 AND TABLE_COMMENT != ''
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
168 ");
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
169
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
170 if(!is_null($pivotTables) && count($pivotTables) > 0) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
171 $ref = current($pivotTables);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
172
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
173 $pivots = json_decode(str_replace('PIVOT:', '', $ref->TABLE_COMMENT), true);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
174
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
175 $tables = [];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
176 if (count($pivots) > 0) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
177 //re-key array
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
178 $references = self::getAllReferencedTables($table);
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
179 $references = self::re_key_array($references, 'REFERENCED_TABLE_NAME');
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
180
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
181 foreach($pivots as $key => $value) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
182 if($ref_data = ($references[$value] ?? null)) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
183 $tables[$value] = [
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
184 'table_name' => $value,
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
185 'column' => $ref_data->COLUMN_NAME
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
186 ];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
187 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
188 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
189 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
190 $relations[] = ['table' => $ref->TABLE_NAME, 'tables' => $tables];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
191 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
192
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
193 return $relations != [] ? $relations : null;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
194 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
195
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
196 public static function re_key_array($old_array, $key) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
197 $new_array = [];
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
198 if (count($old_array) > 0) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
199 foreach ($old_array as $array) {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
200 $new_array[$array->$key] = $array;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
201 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
202 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
203 return $new_array;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
204 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
205 }