lib/gii/cascadeTypeModule/Generator.php
protected function findTableKeys($table)
    {
        $r = [
                'foreignKeys' => [],
                'indices' => [],
                'primaryKeys' => [],
            ];
        $row = $this->dbConnection->createCommand('SHOW CREATE TABLE ' . $this->dbConnection->getSchema()->quoteSimpleTableName($table->name))->queryOne();
        if (isset($row['Create Table'])) {
            $sql = $row['Create Table'];
        } else {
            $row = array_values($row);
            $sql = $row[1];
        }
        $regexp = '/(PRIMARY KEY)\s+\(([^\)]+)\)/mi';
        if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $pks = array_map('trim', explode(',', str_replace('`', '', $match[2])));
                $r['primaryKeys'][] = ['keys' => $pks];
            }
        }

        $regexp = '/(UNIQUE KEY|INDEX|KEY)\s+([^\s]+)\s+\(([^\)]+)\)/mi';
        if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $unique = $match[1] === 'UNIQUE KEY';
                $fname = trim(str_replace('`', '', $match[2]));
                $pks = array_map('trim', explode(',', str_replace('`', '', $match[3])));
                $r['indices'][$fname] = ['keys' => $pks, 'unique' => $unique];
            }
        }

        $regexp = '/CONSTRAINT\s+([^\s]+)\sFOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)\s+ON DELETE\s+((?:(?!\sON).)*)\s+ON UPDATE\s+((?:(?!,).)*)/mi';
        if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $fname = trim(str_replace('`', '', $match[1]));
                $fks = array_map('trim', explode(',', str_replace('`', '', $match[2])));
                $pks = array_map('trim', explode(',', str_replace('`', '', $match[4])));
                $constraint = ['name' => $fname, 'table' => str_replace('`', '', $match[3]), 'keys' => []];
                $constraint['onDelete'] = trim($match[5]);
                $constraint['onUpdate'] = trim($match[6]);
                foreach ($fks as $k => $name) {
                    $constraint['keys'][$name] = $pks[$k];
                }
                $r['foreignKeys'][] = $constraint;
            }
        }

        return $r;
    }
protected function findTableKeys($table)
    {
        $r = [
                'foreignKeys' => [],
                'indices' => [],
                'primaryKeys' => [],
            ];
        $row = $this->dbConnection->createCommand('SHOW CREATE TABLE ' . $this->dbConnection->getSchema()->quoteSimpleTableName($table->name))->queryOne();
        if (isset($row['Create Table'])) {
            $sql = $row['Create Table'];
        } else {
            $row = array_values($row);
            $sql = $row[1];
        }
        $regexp = '/(PRIMARY KEY)\s+\(([^\)]+)\)/mi';
        if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $pks = array_map('trim', explode(',', str_replace('`', '', $match[2])));
                $r['primaryKeys'][] = ['keys' => $pks];
            }
        }

        $regexp = '/(UNIQUE KEY|INDEX|KEY)\s+([^\s]+)\s+\(([^\)]+)\)/mi';
        if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $unique = $match[1] === 'UNIQUE KEY';
                $fname = trim(str_replace('`', '', $match[2]));
                $pks = array_map('trim', explode(',', str_replace('`', '', $match[3])));
                $r['indices'][$fname] = ['keys' => $pks, 'unique' => $unique];
            }
        }

        $regexp = '/CONSTRAINT\s+([^\s]+)\sFOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)\s+ON DELETE\s+((?:(?!\sON).)*)\s+ON UPDATE\s+((?:(?!,).)*)/mi';
        if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $fname = trim(str_replace('`', '', $match[1]));
                $fks = array_map('trim', explode(',', str_replace('`', '', $match[2])));
                $pks = array_map('trim', explode(',', str_replace('`', '', $match[4])));
                $constraint = ['name' => $fname, 'table' => str_replace('`', '', $match[3]), 'keys' => []];
                $constraint['onDelete'] = trim($match[5]);
                $constraint['onUpdate'] = trim($match[6]);
                foreach ($fks as $k => $name) {
                    $constraint['keys'][$name] = $pks[$k];
                }
                $r['foreignKeys'][] = $constraint;
            }
        }

        return $r;
    }
protected function findTableKeys ( $ table ) { $ r = [ 'foreignKeys' => [ ] , 'indices' => [ ] , 'primaryKeys' => [ ] , ] ; $ row = $ this -> dbConnection -> createCommand ( 'SHOW CREATE TABLE ' . $ this -> dbConnection -> getSchema ( ) -> quoteSimpleTableName ( $ table -> name ) ) -> queryOne ( ) ; if ( isset ( $ row [ 'Create Table' ] ) ) { $ sql = $ row [ 'Create Table' ] ; } else { $ row = array_values ( $ row ) ; $ sql = $ row [ 1 ] ; } $ regexp = '/(PRIMARY KEY)\s+\(([^\)]+)\)/mi' ; if ( preg_match_all ( $ regexp , $ sql , $ matches , PREG_SET_ORDER ) ) { foreach ( $ matches as $ match ) { $ pks = array_map ( 'trim' , explode ( ',' , str_replace ( '`' , '' , $ match [ 2 ] ) ) ) ; $ r [ 'primaryKeys' ] [ ] = [ 'keys' => $ pks ] ; } } $ regexp = '/(UNIQUE KEY|INDEX|KEY)\s+([^\s]+)\s+\(([^\)]+)\)/mi' ; if ( preg_match_all ( $ regexp , $ sql , $ matches , PREG_SET_ORDER ) ) { foreach ( $ matches as $ match ) { $ unique = $ match [ 1 ] === 'UNIQUE KEY' ; $ fname = trim ( str_replace ( '`' , '' , $ match [ 2 ] ) ) ; $ pks = array_map ( 'trim' , explode ( ',' , str_replace ( '`' , '' , $ match [ 3 ] ) ) ) ; $ r [ 'indices' ] [ $ fname ] = [ 'keys' => $ pks , 'unique' => $ unique ] ; } } $ regexp = '/CONSTRAINT\s+([^\s]+)\sFOREIGN KEY\s+\(([^\)]+)\)\s+REFERENCES\s+([^\(^\s]+)\s*\(([^\)]+)\)\s+ON DELETE\s+((?:(?!\sON).)*)\s+ON UPDATE\s+((?:(?!,).)*)/mi' ; if ( preg_match_all ( $ regexp , $ sql , $ matches , PREG_SET_ORDER ) ) { foreach ( $ matches as $ match ) { $ fname = trim ( str_replace ( '`' , '' , $ match [ 1 ] ) ) ; $ fks = array_map ( 'trim' , explode ( ',' , str_replace ( '`' , '' , $ match [ 2 ] ) ) ) ; $ pks = array_map ( 'trim' , explode ( ',' , str_replace ( '`' , '' , $ match [ 4 ] ) ) ) ; $ constraint = [ 'name' => $ fname , 'table' => str_replace ( '`' , '' , $ match [ 3 ] ) , 'keys' => [ ] ] ; $ constraint [ 'onDelete' ] = trim ( $ match [ 5 ] ) ; $ constraint [ 'onUpdate' ] = trim ( $ match [ 6 ] ) ; foreach ( $ fks as $ k => $ name ) { $ constraint [ 'keys' ] [ $ name ] = $ pks [ $ k ] ; } $ r [ 'foreignKeys' ] [ ] = $ constraint ; } } return $ r ; }
Collects the foreign key column details for the given table.

@param TableSchema $table the table metadata

@return [[@doctodo return_type:findTableKeys]] [[@doctodo return_description:findTableKeys]]
Collects the foreign key column details for the given table .