public function getFK(TableSchema $table, $indent = 0)
    {
        if (empty($table->foreignKeys)) {
            return '';
        }

        $string = $this->db->createCommand("SHOW CREATE TABLE {$table->name}")->queryAll();
        $onParams = $this->FKOnParams($string[0]['Create Table']);

        $textIndent = $this->textIndent($indent);
        $definition = $textIndent;
        $definition .= '$tablePrefix = \Yii::$app->getDb()->tablePrefix;' . self::ENTER;
        $tableName = static::removePrefix($table->name, $this->db->tablePrefix);

        foreach ($table->foreignKeys as $fkName => $fk) {
            $refTable = '';
            $refColumns = '';
            $columns = '';
            $delete = isset($onParams[$fkName]['ON DELETE']) ? "'{$onParams[$fkName]['ON DELETE']}'" : 'null';
            $update = isset($onParams[$fkName]['ON UPDATE']) ? "'{$onParams[$fkName]['ON UPDATE']}'" : 'null';
            $fkName = static::removePrefix($fkName, $this->db->tablePrefix);

            foreach ($fk as $k => $v) {
                if (0 === $k) {
                    $refTable = $v;
                } else {
                    $columns = $k;
                    $refColumns = $v;
                }
            }

            $definition .= $textIndent;
            $definition .= "\$this->runSuccess[\$tablePrefix.'{$fkName}'] = ";
            $definition .= sprintf(
                    "\$this->addForeignKey(\$tablePrefix.'%s', '{{%%%s}}', '%s', '{{%%%s}}', '%s', %s, %s);" . self::ENTER,
                    $fkName, // 外健名称
                    $tableName, // 表名
                    $columns, // 列名
                    static::removePrefix($refTable, $this->db->tablePrefix), // 对应外健的表名
                    $refColumns, // 对应外健的列名
                    $delete, // ON DELETE
                    $update // ON UPDATE
                );

        }
        return $definition;
    }
public function getFK(TableSchema $table, $indent = 0)
    {
        if (empty($table->foreignKeys)) {
            return '';
        }

        $string = $this->db->createCommand("SHOW CREATE TABLE {$table->name}")->queryAll();
        $onParams = $this->FKOnParams($string[0]['Create Table']);

        $textIndent = $this->textIndent($indent);
        $definition = $textIndent;
        $definition .= '$tablePrefix = \Yii::$app->getDb()->tablePrefix;' . self::ENTER;
        $tableName = static::removePrefix($table->name, $this->db->tablePrefix);

        foreach ($table->foreignKeys as $fkName => $fk) {
            $refTable = '';
            $refColumns = '';
            $columns = '';
            $delete = isset($onParams[$fkName]['ON DELETE']) ? "'{$onParams[$fkName]['ON DELETE']}'" : 'null';
            $update = isset($onParams[$fkName]['ON UPDATE']) ? "'{$onParams[$fkName]['ON UPDATE']}'" : 'null';
            $fkName = static::removePrefix($fkName, $this->db->tablePrefix);

            foreach ($fk as $k => $v) {
                if (0 === $k) {
                    $refTable = $v;
                } else {
                    $columns = $k;
                    $refColumns = $v;
                }
            }

            $definition .= $textIndent;
            $definition .= "\$this->runSuccess[\$tablePrefix.'{$fkName}'] = ";
            $definition .= sprintf(
                    "\$this->addForeignKey(\$tablePrefix.'%s', '{{%%%s}}', '%s', '{{%%%s}}', '%s', %s, %s);" . self::ENTER,
                    $fkName, // 外健名称
                    $tableName, // 表名
                    $columns, // 列名
                    static::removePrefix($refTable, $this->db->tablePrefix), // 对应外健的表名
                    $refColumns, // 对应外健的列名
                    $delete, // ON DELETE
                    $update // ON UPDATE
                );

        }
        return $definition;
    }
public function getFK ( TableSchema $ table , $ indent = 0 ) { if ( empty ( $ table -> foreignKeys ) ) { return '' ; } $ string = $ this -> db -> createCommand ( "SHOW CREATE TABLE {$table->name}" ) -> queryAll ( ) ; $ onParams = $ this -> FKOnParams ( $ string [ 0 ] [ 'Create Table' ] ) ; $ textIndent = $ this -> textIndent ( $ indent ) ; $ definition = $ textIndent ; $ definition .= '$tablePrefix = \Yii::$app->getDb()->tablePrefix;' . self :: ENTER ; $ tableName = static :: removePrefix ( $ table -> name , $ this -> db -> tablePrefix ) ; foreach ( $ table -> foreignKeys as $ fkName => $ fk ) { $ refTable = '' ; $ refColumns = '' ; $ columns = '' ; $ delete = isset ( $ onParams [ $ fkName ] [ 'ON DELETE' ] ) ? "'{$onParams[$fkName]['ON DELETE']}'" : 'null' ; $ update = isset ( $ onParams [ $ fkName ] [ 'ON UPDATE' ] ) ? "'{$onParams[$fkName]['ON UPDATE']}'" : 'null' ; $ fkName = static :: removePrefix ( $ fkName , $ this -> db -> tablePrefix ) ; foreach ( $ fk as $ k => $ v ) { if ( 0 === $ k ) { $ refTable = $ v ; } else { $ columns = $ k ; $ refColumns = $ v ; } } $ definition .= $ textIndent ; $ definition .= "\$this->runSuccess[\$tablePrefix.'{$fkName}'] = " ; $ definition .= sprintf ( "\$this->addForeignKey(\$tablePrefix.'%s', '{{%%%s}}', '%s', '{{%%%s}}', '%s', %s, %s);" . self :: ENTER , $ fkName , // 外健名称 $ tableName , // 表名 $ columns , // 列名 static :: removePrefix ( $ refTable , $ this -> db -> tablePrefix ) , // 对应外健的表名 $ refColumns , // 对应外健的列名 $ delete , // ON DELETE $ update // ON UPDATE ) ; } return $ definition ; }
add forengin key
@param TableSchema $table
@param $indent int text-indent 文本缩进
@return string
add forengin key