5月 29
06.bakeする(model)
続いてbakeしてモデル、コントローラ、ビューを作ります。
作るんですが、その前にマスターにデータを入れておきます。
テーブルの types と favorites は、値が入ってるだけのマスターテーブルです。
今回これの管理機能は提供しません。
ということで最初にテーブルにデータを入れておきます。
別に普通にSQLのinsert文で入れておくだけです。
INSERT INTO types (id, name) VALUES (1, '一般'); INSERT INTO types (id, name) VALUES (2, '専門'); INSERT INTO favorites (id, name) VALUES (1, 'apple'); INSERT INTO favorites (id, name) VALUES (2, 'orange'); INSERT INTO favorites (id, name) VALUES (3, 'grape');
で、最初にmembersテーブルのモデルから作ります。
cd Sites/cake/app cake bake
と打つと、
Welcome to CakePHP v1.2.3.8166 Console
—————————————————————
App : app
Path: /Users/bob/Sites/cake/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)
>
と表示されて入力待ちになるので、 M を入れます。
> m
—————————————————————
Bake Model
Path: /Users/bob/Sites/cake/app/models/
—————————————————————
Possible Models based on your current database:
1. Favorite
2. Member
3. MembersFavorite
4. Type
5. User
Enter a number from the list above, type in the name of another model, or ‘q’ to exit
[q] >
となるのでここで2番を選びます。
あとは対話しながら選択していくだけです。
Would you like to supply validation criteria for the fields in your model? (y/n)
[y] > n
これはvalidateを自動で作ってくれる機能ですが、全然使えないのでNOにします。
Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] >
関連づけをするかどうかの確認です。YESです。
One moment while the associations are detected.
—————————————————————
Please confirm the following associations:
—————————————————————
Member belongsTo Type? (y/n)
[y] >
Member hasAndBelongsToMany Favorite? (y/n)
[y] >
Type に対して belongsTo 、Favorite に対して hasAndBelongsToMany と出ました。信用してそのままYESです。
Would you like to define some additional model associations? (y/n)
[n] >—————————————————————
The following Model will be created:
—————————————————————
Name: Member
Associations:
Member belongsTo Type
Member hasAndBelongsToMany Favorite
—————————————————————
Look okay? (y/n)
[y] >Baking model class for Member…
Creating file /Users/bob/Sites/cake/app/models/member.php
Wrote /Users/bob/Sites/cake/app/models/member.php
SimpleTest is not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
あとはもうそのままですね。
これで /app/models/member.php が作られました。
中味はこんな感じです。
class Member extends AppModel {
var $name = 'Member';
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'Type' => array(
'className' => 'Type',
'foreignKey' => 'type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
var $hasAndBelongsToMany = array(
'Favorite' => array(
'className' => 'Favorite',
'joinTable' => 'members_favorites',
'foreignKey' => 'member_id',
'associationForeignKey' => 'favorite_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
よさそうな感じですね。
あとは同様にusersテーブルについても作っておきます。関連づけはなしですね。
class User extends AppModel {
var $name = 'User';
}
