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」を入力します。
これで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」を選択します。
would you like to choose one? (y/n)
自動設定をしないようにしますか?と聞かれているので「n」を選択して自動設定をするようにします。
※質問文が「not」になっているので少々注意が必要です。
これで自動設定されるようになります。
for the fields in your model? (y/n)
モデルに対してバリデーションを設定するか?という質問です。
バリデーションは設定したいのでここでは「y」を入力します。
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」と答えます。
すべての項目に設定が終わると下記のメッセージが表示されます。
(hasMany, hasOne, belongsTo, etc.)? (y/n)
モデルに対してアソシエーションを設定するか?を聞かれています。
設定するので「y」を選択します。
—————————————————————
Please confirm the following associations:
—————————————————————
User belongsTo Address? (y/n)
[y] >
アソシエーションを設定する候補をBakeが自動で表示してくるのでアソシエーションを設定したいものに「y」と答えます。
他にもアソシエーションを設定するかどうか質問されます。
今回は他にはアソシエーションを設定しないので「n」と答えます。
The following Model will be created:
—————————————————————
Name: Address
DB Table:
Address
Validation: Array
(
[user_id] => Array
(
[numeric] => numeric
)
)
Associations:
User belongsTo Address
—————————————————————
アソシエーションの内容とバリデーションの内容が表示されるので正しければ
に「y」と答えて終了です。
これでモデルの設定が終了です。
人気記事一覧
- MySQL 最大の値・最新の日付データを1つ抽出する方法(MAX)
- [CakePHP] XformHelperの使い方
- Linux: スワップファイル(swp)の削除方法
- HTMLのForm methodとactionについて
- PHPでカンマ区切りデータを配列に入れる方法 (explode)
- [エラー対処] failed to open stream: No such file or directory
- [CSS]ブロック要素(div)の高さを、内部のfloat要素に合わせて自動調整する
- [vsftp] FileZillaから接続できない場合の対処法
- PHP5.1→PHP5.3へバージョンアップする方法(CentOS)
- PHPでテキストを1行ずつ読み込む方法(file_get_contents,explode)