protected function checkJunctionTable($table)
{
if (count($table->foreignKeys) < 2) {
return false;
}
$uniqueKeys = [$table->primaryKey];
try {
$uniqueKeys = array_merge($uniqueKeys, $this->getDbConnection()->getSchema()->findUniqueIndexes($table));
} catch (NotSupportedException $e) {
// ignore
}
$result = [];
// find all foreign key pairs that have all columns in an unique constraint
$foreignKeys = array_values($table->foreignKeys);
for ($i = 0; $i < count($foreignKeys); $i++) {
$firstColumns = $foreignKeys[$i];
unset($firstColumns[0]);
for ($j = $i + 1; $j < count($foreignKeys); $j++) {
$secondColumns = $foreignKeys[$j];
unset($secondColumns[0]);
$fks = array_merge(array_keys($firstColumns), array_keys($secondColumns));
foreach ($uniqueKeys as $uniqueKey) {
if (count(array_diff(array_merge($uniqueKey, $fks), array_intersect($uniqueKey, $fks))) === 0) {
// save the foreign key pair
$result[] = [$foreignKeys[$i], $foreignKeys[$j]];
break;
}
}
}
}
return empty($result) ? false : $result;
}