Links: dibi | API reference

Forum: [česky] [english]

dibi forum

tiny ‘n’ smart
database layer

You are not logged in.

#1 2 years ago

pekelnik
Member
Registered: 2009-02-14
Posts: 372

implementace DibiDatabaseInfo::initForeignKeys()

ahoj,

tady je zkusmá implementace této metody pro postgres – myslím, že tato metoda bude muset být delegována na driver.

<?php
/**
 * Init Foreign Key Information
 * @return void
 */
protected function initForeignKeys()
{
        $this->foreignKeys = array();
        $r = dibi::getConnection()->getDriver()->getResource();

        $q = "
                SELECT
                        tc.constraint_name, tc.table_name, kcu.column_name,
                        ccu.table_name AS foreign_table_name,
                        ccu.column_name AS foreign_column_name
                FROM
                        information_schema.table_constraints AS tc
                        JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
                        JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
                WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = '" . $this->name . "'";

        $result = pg_query($r, $q);

        $rows = array();
        $rows = pg_fetch_all($result);
        while($row = pg_fetch_assoc($result)) {
                $rows[] = $row;
        }

        if ($rows) {
                foreach ($rows as $row) {
                        $references = array();
                        $references['local']['table'] = $row['table_name'];
                        $references['local']['column'] = $row['column_name'];
                        $references['foreign']['table'] = $row['foreign_table_name'];
                        $references['foreign']['column'] = $row['foreign_column_name'];

                        $info = new DibiForeignKeyInfo($row['constraint_name'], $references);
                        $this->foreignKeys[] = $info;
                }
        }
}
?>

_ _ _ _ _ _ _ _ _ __(_) |_| |____ _
/ _` | || | '_| || | | '_ \ | _| / / _` |
\__, |\_,_|_| \_,_| | .__/_|\__|_\_\__,_|

 

Board footer