Beispiel #1
0
    public function getForeignKey($table, $name)
    {
        $query = 'SELECT
				ku.COLUMN_NAME AS columnName,
				ku.REFERENCED_TABLE_NAME AS referencedTable,
				ku.REFERENCED_COLUMN_NAME AS referencedColumn,
				r.DELETE_RULE AS onDelete
				FROM information_schema.REFERENTIAL_CONSTRAINTS AS r JOIN information_schema.KEY_COLUMN_USAGE AS ku
				ON ku.CONSTRAINT_NAME = r.CONSTRAINT_NAME AND ku.TABLE_NAME = r.TABLE_NAME
				WHERE r.CONSTRAINT_SCHEMA = :id  AND ku.CONSTRAINT_SCHEMA = :id AND r.TABLE_NAME = :table AND ku.TABLE_NAME = :table AND ku.CONSTRAINT_NAME = :name AND r.CONSTRAINT_NAME = :name';
        $fields = $this->pdoDriver->executeQuery($query, array('id' => $this->pdoDriver->getID(), 'table' => $this->addPrefix($table), 'name' => $name), true);
        if (count($fields) === 0) {
            return null;
        }
        $referencedTable = $this->removePrefix($fields[0]['referencedTable']);
        $res = new ForeignKey($name, $table, $referencedTable, array(), $this->onDeleteActionMapping[$fields[0]['onDelete']]);
        foreach ($fields as $field) {
            $res->addReference($field['columnName'], $field['referencedColumn']);
        }
        return $res;
    }