その他

MySQL WorkbenchでDoctrineのスキーマ出力

こんにちは、熊谷です。
暑い日が続く今日この頃、皆さんはいかがお過ごしでしょうか。

以前、MySQL Workbenchを使ってみようということでOSX版のバージョン5.2.xについて触れましたが、この5.2.xはまだアルファ版ということもあり普段使用するにはためらうことがあるかもしれません。しかし、それから2ヶ月半ほど過ぎ、この5.2.xはまだアルファ版ではありますが、以前日本語処理に問題のあった5.1.xが晴れて安定版となり、問題なく動作するようになっています。

ちなみに、もし今現在5.2.xを使用していて5.1.xへダウングレードしたい場合、作成したmwbファイルは今のところ古いバージョンでは開くことができません。しかし、mwbファイル自体はzipで圧縮されているファイルなので解凍し出てきたxmlファイルをエディタ等で開き、2行目に書かれてあるversionを1.3.1に変更しzipで圧縮し直すことにより5.1.x(確認したのは今現在の最新版5.1.16)で開けるようになります。

そんなことで、最新版が好きな私としては5.2.xを使い続けたいのですが、周りの諸事情により5.1.xにダウングレードして使っています。

さて、そのMySQL Workbenchですがプラグインで機能を追加することが出来ます。今回はその中からDoctrineのスキーマを出力するDoctrine Pluginを使ってみたいと思います。

MySQL WorkbenchのDoctrine pluginは以下のサイトからダウンロードすることが出来ます。
http://code.google.com/p/mysql-workbench-doctrine-plugin/

インストールについてはOSX版ユーザは、


~/Library/Application Support/MySQL/Workbench/modules

に解凍します。

そしてMySQL Workbenchを起動すると「Plugins」のメニューに「Doctrine Export ほげほげ」という項目が追加されていると思います。

以上で完了。あとはER図を作成しこのメニューからDoctrineのスキーマを出力するだけです。

例えば、以下のようなER図を書いて

「Plugins」のメニューの「Doctrine Export 0.3.5: Write Generated Doctrine Schema to File…」をクリックすると出力先をきかれます。

保存したいPATHを入力しOKボタンをクリックすると完了です。

出力された内容は以下のようになります


---
detect_relations: true
options:
  collation: utf8_general_ci
  charset: utf8
  type: InnoDB
user:
  columns:
    id:
      type: integer
      primary: true
      unsigned: true
      notnull: true
      autoincrement: true
    name:
      type: string(255)
      notnull: true
    password:
      type: string(255)
      notnull: true
    deleted:
      type: integer(1)
      zerofill: true
      unsigned: true
      notnull: true
      default: 0
    created_at:
      type: timestamp
      notnull: true
    updated_at:
      type: timestamp
      notnull: true
  options:
    type: InnoDB
blog:
  columns:
    id:
      type: integer
      primary: true
      unsigned: true
      notnull: true
      autoincrement: true
    title:
      type: string(255)
    body:
      type: clob(65535)
    created_at:
      type: timestamp
      notnull: true
    updated_at:
      type: timestamp
      notnull: true
    user_id:
      type: integer
      unsigned: true
      notnull: true
  relations:
    user:
      local: user_id
      foreign: id
      foreignAlias: blogs
  options:
    type: InnoDB

このDoctrine PluginはLuaと呼ばれるスクリプト言語で書かれてあり内容的にもそれほど難しいものではないのでいろいろ自分好みに変更できると思います。是非試してみてはいかがでしょうか。

ちなみに、以下のように


--- DoctrineExport.grt.lua.orig 2009-07-29 12:22:12.000000000 +0900
+++ DoctrineExport.grt.lua      2009-07-29 14:55:09.000000000 +0900
@@ -437,22 +437,11 @@
 -- doctrine friendly tableNames
 function buildTableName(s)
     -- don't call ucfirst, leave table names as they are
-    --= ucfirst(s)
+    s = "Db" .. ucfirst(s)
     if ( isNestedTableModel(s) ) then
         s = string.sub(s, 1, #s - 3)
     end
-    --
-    -- converting User_has_Groups (default WB-Scheme) to UserGroups
-    -- as used in the doctrine manual
-    local patternStart, patternEnd = string.find(s, "_has_")
-    if ( patternStart ~= nil and patternEnd ~= nil ) then
-        local front = singularizeTableName(string.sub(s, 1, patternStart - 1))
-        local back = string.sub(s, patternEnd + 1)
-        s = ucfirst(front) .. ucfirst(back)
-    end
-
-    s = singularizeTableName(s)
     --
     -- make camel_case to CamelCase

Doctrine Pluginを変更してスキーマを出力すると、以下のように


---
detect_relations: true
options:
  collation: utf8_general_ci
  charset: utf8
  type: InnoDB
DbUser:
  tableName: user
  columns:
    id:
      type: integer
      primary: true
      unsigned: true
      notnull: true
      autoincrement: true
    name:
      type: string(255)
      notnull: true
    password:
      type: string(255)
      notnull: true
    deleted:
      type: integer(1)
      zerofill: true
      unsigned: true
      notnull: true
      default: 0
    created_at:
      type: timestamp
      notnull: true
    updated_at:
      type: timestamp
      notnull: true
  options:
    type: InnoDB
DbBlog:
  tableName: blog
  columns:
    id:
      type: integer
      primary: true
      unsigned: true
      notnull: true
      autoincrement: true
    title:
      type: string(255)
    body:
      type: clob(65535)
    created_at:
      type: timestamp
      notnull: true
    updated_at:
      type: timestamp
      notnull: true
    user_id:
      type: integer
      unsigned: true
      notnull: true
  relations:
    DbUser:
      local: user_id
      foreign: id
      foreignAlias: DbBlogs
  options:
    type: InnoDB

モデルに接頭辞として”Db”を付けてアッパーキャメルケースで出力されるようになります。

author img for asial

asial

前の記事へ

次の記事へ

一覧へ戻る
PAGE TOP