Bakeでテーブル単位のCRUD系ファイルを自動生成
とりあえずbakeする
bakeスクリプトを使うと、DBに対して一覧表示、新規レコード追加、変更、削除などが出来るMVCファイルを作ってくれる。
bakeスクリプトファイルは
/cake/console/cake
です。
DBの定義
hogepostsというテーブルを作ります。
bakeで作って行く場合は、cakephpの命名規則にのっとらないと、modelファイルは作れるけど、controllerファイル生成時に対応するテーブルが無いというエラーが出るので、今回はテーブル名を複数形にしました。
それと、Primary keyはidというカラム名でAutoincrementとし、日付のカラムとしてcreated, modifiedを作ります。これを作っておくと、レコードの新規作成時にcreatedカラムにその日時を、レコードの変更時にmodifiedカラムにその変更日時を勝手にセットしてくれます。便利〜!
mysql> show fields from hogeposts; +----------+------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | body | text | YES | | NULL | | | created | datetime | YES | | NULL | | | modified | datetime | YES | | NULL | | | title | text | YES | | NULL | | +----------+------------+------+-----+---------+----------------+
まず、Modelを作成します。
./cake bake Model Welcome to CakePHP v1.2.0.7125 RC1 Console --------------------------------------------------------------- App : app Path: /Users/username/Sites/php/cake12/app --------------------------------------------------------------- --------------------------------------------------------------- Bake Model Path: /Users/username/Sites/php/cake12/app/models/ --------------------------------------------------------------- Use Database Config: (default/test) [default] > (database.phpで設定したモードを選択します。defaultで問題ないでしょう) Possible Models based on your current database: 1. Hogepost 2. Post 3. Task Enter a number from the list above, type in the name of another model, or 'q' to exit [q] > 1 (テーブルを選択します。ここは1のHogepostです(テーブル名の単数なのでsがないです)) Would you like to supply validation criteria for the fields in your model? (y/n) [y] > n (バリデーションを付けるか? とりあえずパターンが多いので後で) Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n) [y] > n (リレーションの設定。今回は単一テーブルなのでなし) --------------------------------------------------------------- The following Model will be created: --------------------------------------------------------------- Name: Hogepost Associations: --------------------------------------------------------------- Look okay? (y/n) [y] > y (ファイル作っていいか?) Baking model class for Hogepost... Creating file /Users/username/Sites/php/cake12/app/models/hogepost.php Wrote /Users/username/Sites/php/cake12/app/models/hogepost.php Cake test suite not installed. Do you want to bake unit test files anyway? (y/n) [y] > n (テストファイル作るか?)
という感じでやると、/app/models/hogepost.phpというファイルが出来て、中身は下記のようになる
<?php class Hogepost extends AppModel { var $name = 'Hogepost'; } ?>
次にコントローラの作成。
./cake bake Controller Welcome to CakePHP v1.2.0.7125 RC1 Console --------------------------------------------------------------- App : app Path: /Users/username/Sites/php/cake12/app --------------------------------------------------------------- --------------------------------------------------------------- Bake Controller Path: /Users/username/Sites/php/cake12/app/controllers/ --------------------------------------------------------------- Possible Controllers based on your current database: 1. Hogeposts 2. Posts 3. Tasks Enter a number from the list above, type in the name of another controller, or 'q' to exit [q] > 1 (テーブルの選択) --------------------------------------------------------------- Baking HogepostsController --------------------------------------------------------------- Would you like to build your controller interactively? (y/n) [y] > y (インタラクティブにコントローラを設定するか?) Would you like to use scaffolding? (y/n) [n] > n (scaffoldingを使うか? 使わないのでn) Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n) [n] > y (あらかじめメソッドを作るか? yes) Would you like to create the methods for admin routing? (y/n) [n] > n (adminルーティングをするか? no) Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n) [n] > n (form helperを使うか? no) Would you like this controller to use any components? (y/n) [n] > n (コンポーネントを使うか? no) Would you like to use Sessions? (y/n) [y] > n (セッションを使うか? no) --------------------------------------------------------------- The following controller will be created: --------------------------------------------------------------- Controller Name: Hogeposts --------------------------------------------------------------- Look okay? (y/n) [y] > y Creating file /Users/username/Sites/php/cake12/app/controllers/hogeposts_controller.php Wrote /Users/username/Sites/php/cake12/app/controllers/hogeposts_controller.php Cake test suite not installed. Do you want to bake unit test files anyway? (y/n) [y] > n
これで、/app/controllers/hogeposts_controller.phpが作成される。
ここには、indexやaddなどのメソッドが既に作成されている。
最後にViewを作成
./cake bake view Welcome to CakePHP v1.2.0.7125 RC1 Console --------------------------------------------------------------- App : app Path: /Users/username/Sites/php/cake12/app --------------------------------------------------------------- --------------------------------------------------------------- Bake View Path: /Users/username/Sites/php/cake12/app/views/ --------------------------------------------------------------- Possible Controllers based on your current database: 1. Hogeposts 2. Posts 3. Tasks Enter a number from the list above, type in the name of another controller, or 'q' to exit [q] > 1 (DBテーブルの選択) Would you like to create some scaffolded views (index, add, view, edit) for this controller? NOTE: Before doing so, you'll need to create your controller and model classes (including associated models). (y/n) [n] > y (controllerで自動的に追加したメソッドに対応するViewを作るか? yes) Would you like to create the views for admin routing? (y/n) [y] > n (Adminルーティングのview設定をするか? no) Creating file /Users/username/Sites/php/cake12/app/views/hogeposts/index.ctp Wrote /Users/username/Sites/php/cake12/app/views/hogeposts/index.ctp Creating file /Users/username/Sites/php/cake12/app/views/hogeposts/view.ctp Wrote /Users/username/Sites/php/cake12/app/views/hogeposts/view.ctp Creating file /Users/username/Sites/php/cake12/app/views/hogeposts/add.ctp Wrote /Users/username/Sites/php/cake12/app/views/hogeposts/add.ctp Creating file /Users/username/Sites/php/cake12/app/views/hogeposts/edit.ctp Wrote /Users/username/Sites/php/cake12/app/views/hogeposts/edit.ctp --------------------------------------------------------------- View Scaffolding Complete.
これで、/app/views/hogeposts/以下に、
add.ctp, edit.ctp, index.ctp, view.ctp
ファイルが出来る。これらはaddなどのメソッドに対応したViewファイルとなる
ここまでで、hogepostsテーブルに対する入出力を行うWEBアプリが出来上がり!
設定は楽だけど、ブログに書くのに時間がかかるなぁ。。。
とりあえず、下記のようなURLにアクセスすれば画面が表示される。
http://localhost/cake/hogeposts/
全体のデザインを変えたければ、
/cake/libs/view/layouts/default.ctp
を下記の場所にコピー
/app/views/layouts/default.ctp
そしてそのファイルを変更すれば、ヘッダ、フッタ領域が変わる。
ヘッダ、フッタ以外は、views以下のadd.ctpなどのファイルを修正する。
あとは、バリデーションは、modelファイルに追記し、確認画面などの表示の遷移はcontrollerファイルに追記し、デザインなどのファイルは、viewとlayoutファイルを変更すればお好みのWEBアプリの出来上がり!