@@ -89,15 +89,21 @@ public function __construct(AbstractSchemaManager $schemaManager, Cache $cache =
89
89
* - it has exactly 2 foreign keys
90
90
* - it has only 2 columns (or 3 columns if the third one is an autoincremented primary key).
91
91
*
92
+ * If $ignoreReferencedTables is true, junctions table that are pointed to by a foreign key of another
93
+ * table are ignored.
94
+ *
95
+ * @param bool $ignoreReferencedTables
92
96
*
93
97
* @return Table[]
94
98
*/
95
- public function detectJunctionTables ()
99
+ public function detectJunctionTables ($ ignoreReferencedTables = false )
96
100
{
97
- $ junctionTablesKey = $ this ->cachePrefix .'_junctiontables ' ;
101
+ $ junctionTablesKey = $ this ->cachePrefix .'_junctiontables_ ' .( $ ignoreReferencedTables ? ' true ' : ' false ' ) ;
98
102
$ junctionTables = $ this ->cache ->fetch ($ junctionTablesKey );
99
103
if ($ junctionTables === false ) {
100
- $ junctionTables = array_filter ($ this ->getSchema ()->getTables (), [$ this , 'isJunctionTable ' ]);
104
+ $ junctionTables = array_filter ($ this ->getSchema ()->getTables (), function (Table $ table ) use ($ ignoreReferencedTables ) {
105
+ return $ this ->isJunctionTable ($ table , $ ignoreReferencedTables );
106
+ });
101
107
$ this ->cache ->save ($ junctionTablesKey , $ junctionTables );
102
108
}
103
109
@@ -111,14 +117,18 @@ public function detectJunctionTables()
111
117
* - it must have exactly 2 foreign keys
112
118
* - it must have only 2 columns (or 3 columns if the third one is an autoincremented primary key).
113
119
*
120
+ * If $ignoreReferencedTables is true, junctions table that are pointed to by a foreign key of another
121
+ * table are ignored.
122
+ *
114
123
* @param Table $table
124
+ * @param bool $ignoreReferencedTables
115
125
*
116
126
* @return bool
117
127
*/
118
- private function isJunctionTable (Table $ table )
128
+ private function isJunctionTable (Table $ table, $ ignoreReferencedTables = false )
119
129
{
120
130
$ foreignKeys = $ table ->getForeignKeys ();
121
- if (count ($ foreignKeys ) != 2 ) {
131
+ if (count ($ foreignKeys ) !== 2 ) {
122
132
return false ;
123
133
}
124
134
@@ -133,24 +143,24 @@ private function isJunctionTable(Table $table)
133
143
$ pkColumns = [];
134
144
}
135
145
136
- if (count ($ pkColumns ) == 1 && count ($ columns ) == 2 ) {
146
+ if (count ($ pkColumns ) === 1 && count ($ columns ) = == 2 ) {
137
147
return false ;
138
148
}
139
149
140
- if (count ($ pkColumns ) != 1 && count ($ columns ) == 3 ) {
150
+ if (count ($ pkColumns ) !== 1 && count ($ columns ) = == 3 ) {
141
151
return false ;
142
152
}
143
153
144
154
$ fkColumnNames = [];
145
155
foreach ($ foreignKeys as $ foreignKey ) {
146
156
$ fkColumns = $ foreignKey ->getColumns ();
147
- if (count ($ fkColumns ) != 1 ) {
157
+ if (count ($ fkColumns ) !== 1 ) {
148
158
return false ;
149
159
}
150
160
$ fkColumnNames [$ fkColumns [0 ]] = true ;
151
161
}
152
162
153
- if (count ($ columns ) == 3 ) {
163
+ if (count ($ columns ) === 3 ) {
154
164
// Let's check that the third column (the ID is NOT a foreign key)
155
165
if (isset ($ fkColumnNames [$ pkColumns [0 ]])) {
156
166
return false ;
@@ -162,9 +172,34 @@ private function isJunctionTable(Table $table)
162
172
}
163
173
}
164
174
175
+ if ($ ignoreReferencedTables && $ this ->isTableReferenced ($ table )) {
176
+ return false ;
177
+ }
178
+
165
179
return true ;
166
180
}
167
181
182
+ /**
183
+ * Returns true if the table $table is referenced by another table.
184
+ *
185
+ * @param Table $table
186
+ *
187
+ * @return bool
188
+ */
189
+ private function isTableReferenced (Table $ table )
190
+ {
191
+ $ tableName = $ table ->getName ();
192
+ foreach ($ this ->getSchema ()->getTables () as $ tableIter ) {
193
+ foreach ($ tableIter ->getForeignKeys () as $ fk ) {
194
+ if ($ fk ->getForeignTableName () === $ tableName ) {
195
+ return true ;
196
+ }
197
+ }
198
+ }
199
+
200
+ return false ;
201
+ }
202
+
168
203
/**
169
204
* Get the shortest path between 2 tables.
170
205
*
@@ -488,7 +523,7 @@ private function getParentRelationshipWithoutCache($tableName)
488
523
}
489
524
}
490
525
491
- return null ;
526
+ return ;
492
527
}
493
528
494
529
/**
0 commit comments