edo1z blog

プログラミングなどに関するブログです

cakePHP2.3 Schema

参考:CakePHP2.0のコンソールを使ってラクラク自動生成しよう【1/2】

データベースの状態をschemaに保存したり、schemaの内容をもとにテーブルをつくったりできる。

コンソールでapp/Console/cake schemaとやって使う。

schema generate

現在のデータベースの状態をapp/Config/Schema/schema.phpに作成する。 schema.phpが既に存在する場合は、overwirteかsnapshotかやめるかを聞かれる。snapshotはschema_1.phpといった感じで、上書きせずに新規作成する。データベースをいじったら作っておくと便利かもしれん。 --modelとか-mでモデルも指定できる。-m Userとかやると、Userモデルのテーブルだけつくられる。

schema create

schema.phpを基に、データベースにテーブルを作成する。作成するテーブルを選んだりすることはできないようだ。 オプションは下記のようになっているので、もとになるschemaファイルの場所とか名前とかスナップショットナンバーとかを指定できるようだ。

--help, -h        Display this help.
--verbose, -v     Enable verbose output.
--quiet, -q       Enable quiet output.
--plugin, -p      The plugin to use.
--path            Path to read and write schema.php (default:
                  /Applications/XAMPP/xamppfiles/htdocs/labo/app/Config/Schema)
--file            File name to read and write. (default:
                  schema.php)
--name            Classname to use. If its Plugin.class, both name and
                  plugin options will be set.
--connection, -c  Set the db config to use. (default:
                  default)
--dry             Perform a dry run on create and update commands.
                  Queries will be output instead of run.
--snapshot, -s    Snapshot number to use/make.

schema dump

schemaファイルをもとにsqlを作成する。 schema dump --write schema.sqlといった感じで使う。--writeオプションがないと、そのままコンソールにsqlが表示される。 上記オプションと似ていて、もとにするschemaファイルを指定できる。