Prado\Data\Common\Mssql\TMssqlMetaData::getForeignConstraints PHP Method

getForeignConstraints() protected method

Gets foreign relationship constraint keys and table name
protected getForeignConstraints ( $col ) : array
return array foreign relationship table name and keys.
    protected function getForeignConstraints($col)
    {
        //From http://msdn2.microsoft.com/en-us/library/aa175805(SQL.80).aspx
        $sql = <<<EOD
\t\tSELECT
\t\t\t\tKCU1.CONSTRAINT_NAME AS 'FK_CONSTRAINT_NAME'
\t\t\t, KCU1.TABLE_NAME AS 'FK_TABLE_NAME'
\t\t\t, KCU1.COLUMN_NAME AS 'FK_COLUMN_NAME'
\t\t\t, KCU1.ORDINAL_POSITION AS 'FK_ORDINAL_POSITION'
\t\t\t, KCU2.CONSTRAINT_NAME AS 'UQ_CONSTRAINT_NAME'
\t\t\t, KCU2.TABLE_NAME AS 'UQ_TABLE_NAME'
\t\t\t, KCU2.COLUMN_NAME AS 'UQ_COLUMN_NAME'
\t\t\t, KCU2.ORDINAL_POSITION AS 'UQ_ORDINAL_POSITION'
\t\tFROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
\t\tJOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU1
\t\tON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG
\t\t\tAND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
\t\t\tAND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
\t\tJOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU2
\t\tON KCU2.CONSTRAINT_CATALOG =
\t\tRC.UNIQUE_CONSTRAINT_CATALOG
\t\t\tAND KCU2.CONSTRAINT_SCHEMA =
\t\tRC.UNIQUE_CONSTRAINT_SCHEMA
\t\t\tAND KCU2.CONSTRAINT_NAME =
\t\tRC.UNIQUE_CONSTRAINT_NAME
\t\t\tAND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION
\t\tWHERE KCU1.TABLE_NAME = :table
EOD;
        $command = $this->getDbConnection()->createCommand($sql);
        $command->bindValue(':table', $col['TABLE_NAME']);
        $fkeys = array();
        $catalogSchema = "[{$col['TABLE_CATALOG']}].[{$col['TABLE_SCHEMA']}]";
        foreach ($command->query() as $info) {
            $fkeys[$info['FK_CONSTRAINT_NAME']]['keys'][$info['FK_COLUMN_NAME']] = $info['UQ_COLUMN_NAME'];
            $fkeys[$info['FK_CONSTRAINT_NAME']]['table'] = $info['UQ_TABLE_NAME'];
        }
        return count($fkeys) > 0 ? array_values($fkeys) : $fkeys;
    }