[CakePHP]Bakeでモデルを作る




Bakeを使ってモデルの設定をします。

Bakeの起動方法はこちらの記事を参照してください。
[CakePHP] Bakeの起動

Bakeが立ち上がると下記のメッセージが表示されます。

Welcome to CakePHP v1.3.14 Console
—————————————————————
App : app
Path: /home/me/app
—————————————————————
Interactive Bake Shell
—————————————————————
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)
>

今回はモデルを作成します。
Bakeでの作成順序は「モデル」→「コントローラー」→「ビュー」の順が良いでしょう。
「>」カーソルの後に「M」を入力します。

>M

これでOKです。

下記のような表示が出てきます。

—————————————————————
Bake Model
Path: /home/me/app/models/
—————————————————————
Possible Models based on your current database:
1. Address
2. Post
3. User
Enter a number from the list above,
type in the name of another model, or ‘q’ to exit

今回は「User」のモデルを作りますので、「3」を選択します。

>3

A displayField could not be automatically detected
would you like to choose one? (y/n)

自動設定をしないようにしますか?と聞かれているので「n」を選択して自動設定をするようにします。
※質問文が「not」になっているので少々注意が必要です。

>n

これで自動設定されるようになります。

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 – inlist
14 – ip
15 – maxlength
16 – minlength
17 – money
18 – multiple
19 – notempty
20 – numeric
21 – phone
22 – postal
23 – range
24 – ssn
25 – time
26 – url
27 – userdefined
28 – uuid
29 – Do not do any validation on this field.
… or enter in a valid regex validation string.

一つ一つのフィールドに対しどのバリデーションを設定するかを数字で選択していきます。

複数の設定をするかどうかを下記の文章で聞かれるので必要だったら「y」、複数設定がいらなのであれば「n」と答えます。

Would you like to add another validation rule? (y/n)

すべての項目に設定が終わると下記のメッセージが表示されます。

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:
—————————————————————
User belongsTo Address? (y/n)
[y] >

アソシエーションを設定する候補をBakeが自動で表示してくるのでアソシエーションを設定したいものに「y」と答えます。

Would you like to define some additional model associations? (y/n)

他にもアソシエーションを設定するかどうか質問されます。
今回は他にはアソシエーションを設定しないので「n」と答えます。

—————————————————————
The following Model will be created:
—————————————————————
Name: Address
DB Table: Address
Validation: Array
(
[user_id] => Array
(
[numeric] => numeric
)
)

Associations:
User belongsTo Address

—————————————————————

アソシエーションの内容とバリデーションの内容が表示されるので正しければ

Look okay? (y/n)

に「y」と答えて終了です。

これでモデルの設定が終了です。




人気記事一覧


関連記事一覧

This entry was posted in bake and tagged , , , . Bookmark the permalink.

コメントは受け付けていません。