Bakeメモ このエントリーをはてなブックマークに追加

提供:Asterisk Works Wiki
移動: 案内, 検索

CakePHPでアプリケーションのひな形を生成するBakeの使い方についてのメモ

目次

用意するもの

bakeではデータベースからそれを操作するためのモデルクラスを生成するため、

  • CakePHP
  • データベース

bakeの基本的な使い方

bakeはcakeコマンドのサブコマンド。cakeコマンドはCakePHP/cake/console/cakeにある。起動するには次のコマンドを実行する。

> ./cake/console/cake -app /<アプリケーションのパス>/app bake

-app オプションはbakeしたいcakeのappディレクトリの位置を示すためのオプション。

実行すると次の様に表示される

Welcome to CakePHP v1.2.3.8166 Console
---------------------------------------------------------------
App : app
Path: /<アプリケーションのパス>/app
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q) 
>

基本的な流れは次の通り

  1. モデルを生成する
  2. コントローラーを生成する
  3. ビューを生成する

これでとりあえずDBを操作できるアプリケーションが生成される。

モデルの生成

Mを選択すると次の様に表示される。データベースのコンフィグのいずれかを選択すると、DBのテーブル定義からbakeできるモデルの一覧が表示される。

---------------------------------------------------------------
Bake Model
Path: /<アプリケーションのパス>/app/models/
---------------------------------------------------------------
Use Database Config: (default/test) 
[default] > 
Possible Models based on your current database:
1. Content
2. Schedule
Enter a number from the list above, type in the name of another model, or 'q' to exit  

モデルを選択すると次の様に問われる。以下各フィールドに対するバリデーションの設定がフィールドの数だけ続く。

> Would you like to supply validation criteria for the fields in your model? (y/n)
[y] > y

Field: id
Type: integer
---------------------------------------------------------------
Please select one of the following validation options:
---------------------------------------------------------------
1 - alphaNumeric
2 - between
3 - blank
4 - boolean
5 - cc
6 - comparison
7 - custom
8 - date
9 - decimal
10 - email
11 - equalTo
12 - extension
13 - file
14 - inList
15 - ip
16 - maxLength
17 - minLength
18 - money
19 - multiple
20 - notEmpty
21 - numeric
22 - phone
23 - postal
24 - range
25 - ssn
26 - time
27 - url
28 - userDefined
29 - Do not do any validation on this field.
... or enter in a valid regex validation string.

[29] > 

一通りのバリデーションを設定すれば次のアソシエーションの設定となる。

Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n) 
[y] > 
One moment while the associations are detected.
---------------------------------------------------------------
Please confirm the following associations:
---------------------------------------------------------------
Would you like to define some additional model associations? (y/n) 
[n] > y
What is the association type?
1. belongsTo
2. hasOne
3. hasMany
4. hasAndBelongsToMany
[Enter a number] > 3
For the following options be very careful to match your setup exactly. Any spelling mistakes will cause errors.
---------------------------------------------------------------
What is the alias for this association?  
> schedules
What className will schedules use?  
[schedules] > Schedule
A helpful List of possible keys
1. id
2. starttime
3. subno
4. indate
5. mdate
6. enable
What is the foreignKey?  
[Enter a number] > 3
Define another association? (y/n) 
[y] > n

---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name:       Content
Validation: Array
(
    [id] => numeric
    [filename] => alphanumeric
    [frames] => numeric
    [title] => date
    [DESC] => numeric
)

Associations:
			Content hasMany	schedules
---------------------------------------------------------------
Look okay? (y/n) 
[y] > y

Baking model class for Content...

Creating file /<アプリケーションのパス>/app/models/content.php
File exists, overwrite? /<アプリケーションのパス>/app/models/content.php (y/n/q) 
[n] > y
Wrote /<アプリケーションのパス>/app/models/content.php

ここまででモデルクラスが生成される。すでに同名のクラスのファイルが存在すれば上書きするかも問われる。
続いてテストクラスの生成へと続く

SimpleTest is not installed.  Do you want to bake unit test files anyway? (y/n) 
[y] > y

You can download SimpleTest from http://simpletest.org

Baking test fixture for Content...

Creating file /<アプリケーションのパス>/app/tests/fixtures/content_fixture.php
File exists, overwrite? /<アプリケーションのパス>/app/tests/fixtures/content_fixture.php (y/n/q) 
[n] > y
Wrote /<アプリケーションのパス>/app/tests/fixtures/content_fixture.php

Baking unit test for Content...

Creating file /<アプリケーションのパス>/app/tests/cases/models/content.test.php
File exists, overwrite? /<アプリケーションのパス>/app/tests/cases/models/content.test.php (y/n/q) 
[n] > y
Wrote /<アプリケーションのパス>/app/tests/cases/models/content.test.php

ここまでで一覧のモデルクラスの生成は終了。最初のメニューに戻る。

コントローラーの生成

---------------------------------------------------------------
Bake Controller
Path: /<アプリケーションのパス>/app/controllers/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Contents
2. Schedules
Enter a number from the list above, type in the name of another controller, or 'q' to exit  
[q] > 1
---------------------------------------------------------------
Baking ContentsController
---------------------------------------------------------------
Would you like to build your controller interactively?
Warning: Choosing no will overwrite the ContentsController. (y/n) 
[y] > y
Would you like to use scaffolding? (y/n) 
[n] > 
Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n) 
[n] > y
Would you like to create the methods for admin routing? (y/n) 
[n] > y
Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n) 
[n] > y
Please provide a comma separated list of the other helper names you'd like to use.
Example: 'Ajax, Javascript, Time'  
> Ajax, Javascript, Time
Would you like this controller to use any components? (y/n) 
[n] > y
Please provide a comma separated list of the component names you'd like to use.
Example: 'Acl, Security, RequestHandler'  
> Acl
Would you like to use Sessions? (y/n) 
[y] > y
You need to enable Configure::write('Routing.admin','admin') in /app/config/core.php to use admin routing.
What would you like the admin route to be?
Example: www.example.com/admin/controller
What would you like the admin route to be?  
[admin] > 

---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:  Contents
Helpers:      Ajax, Javascript, Time
Components:      Acl
---------------------------------------------------------------
Look okay? (y/n) 
[y] > y

Creating file /<アプリケーションのパス>/app/controllers/contents_controller.php
File exists, overwrite? /<アプリケーションのパス>/app/controllers/contents_controller.php (y/n/q) 
[n] > y
Wrote /<アプリケーションのパス>/app/controllers/contents_controller.php
SimpleTest is not installed.  Do you want to bake unit test files anyway? (y/n) 
[y] > y

You can download SimpleTest from http://simpletest.org

Baking unit test for Contents...

Creating file /<アプリケーションのパス>/app/tests/cases/controllers/contents_controller.test.php
File exists, overwrite? /<アプリケーションのパス>/app/tests/cases/controllers/contents_controller.test.php (y/n/q) 
[n] > y
Wrote /<アプリケーションのパス>/app/tests/cases/controllers/contents_controller.test.php





ビューの生成


---------------------------------------------------------------
Bake View
Path: /<アプリケーションのパス>/app/views/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Contents
2. Schedules
Enter a number from the list above, type in the name of another controller, or 'q' to exit  
[q] > 1
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] > 
Action Name? (use camelCased function name)  
> list

---------------------------------------------------------------
The following view will be created:
---------------------------------------------------------------
Controller Name: Contents
Action Name:	 list
Path:			 app/contents/list.ctp
---------------------------------------------------------------
Look okay? (y/n) 
[y] > 

Creating file /<アプリケーションのパス>/app/views/contents/list.ctp
Wrote /<アプリケーションのパス>/app/views/contents/list.ctp
個人用ツール
名前空間
変種
操作
案内
主なカテゴリ
ツールボックス