2013/06/22

[CakePHP2.x][PostgreSQL] ConnectionManagerで DBのテーブルとフィールド定義を取得してみる

Class Postgres | CakePHP http://api.cakephp.org/2.3/class-Postgres.html

// getDataSource
$db = ConnectionManager::getDataSource(string Model::$useDbConfig);
  →Model::$useDbConfig databaseの設定名 'default'とか
  →ModelObject->useDbConfigで取れる e.g. $this->Model->useDbConfig

// an array of tables in the database.
$tables = $db->listSources();

// Fields in table. Keys are name and type
$data = array();
foreach ($tables as $table) {
  $fields = $db->describe(strtolower($table));
  $data[$table] = $fields;
}

// $dataに テーブルとフィールドの定義が入る
Array
(
    [table_name] => Array
        (
            [id] => Array
                (
                    [type] => integer
                    [null] =>
                    [default] =>
                    [length] =>
                )
            [name] => Array
                (
                    [type] => string
                    [null] =>
                    [default] =>
                    [length] => 512
                )
            [update_id] => Array
                (
                    [type] => text
                    [null] =>
                    [default] =>
                    [length] =>
                )

            [regist_date] => Array
                (
                    [type] => datetime
                    [null] =>
                    [default] =>
                    [length] =>
                )
        )
)

0 件のコメント: