src/generators/model/Generator.php
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);
        $foreignKeysCount = count($foreignKeys);

        for ($i = 0; $i < $foreignKeysCount; $i++) {
            $firstColumns = $foreignKeys[$i];
            unset($firstColumns[0]);

            for ($j = $i + 1; $j < $foreignKeysCount; $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;
    }
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);
        $foreignKeysCount = count($foreignKeys);

        for ($i = 0; $i < $foreignKeysCount; $i++) {
            $firstColumns = $foreignKeys[$i];
            unset($firstColumns[0]);

            for ($j = $i + 1; $j < $foreignKeysCount; $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;
    }
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 ) ; $ foreignKeysCount = count ( $ foreignKeys ) ; for ( $ i = 0 ; $ i < $ foreignKeysCount ; $ i ++ ) { $ firstColumns = $ foreignKeys [ $ i ] ; unset ( $ firstColumns [ 0 ] ) ; for ( $ j = $ i + 1 ; $ j < $ foreignKeysCount ; $ 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 ; }
Checks if the given table is a junction table, that is it has at least one pair of unique foreign keys.
@param \yii\db\TableSchema the table being checked
@return array|bool all unique foreign key pairs if the table is a junction table,
or false if the table is not a junction table.
Checks if the given table is a junction table that is it has at least one pair of unique foreign keys .