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.3.11 Console
---------------------------------------------------------------
App : app
Path: /Users/myname/Sites/cake/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) 
> 
と表示されて入力待ちになるので、 M を入れます。
> M
---------------------------------------------------------------
Bake Model
Path: /Users/myname/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番を選びます。

あとは対話しながら選択していくだけです。

A displayField could not be automatically detected
would you like to choose one? (y/n) 
> y
displayField が自動で見つけられなかった、自分で選ぶかい? と聞かれるのでyesとすると、
1. id
2. email
3. password
4. type_id
5. birthday
6. img1
7. img2
8. created
9. modified
Choose a field from the options above:  
> 2
members テーブルのカラム名が一覧されるので、2番の email を選択します。

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.
  
[29] > 
というように、カラム id について1から29までのルールが表示されるので、その中から適用するルールを選びます。 [] 内に表示されているのはcakephpが推測する値です。 一回選ぶ度に、
Would you like to add another validation rule? (y/n) 
[n] > 
と、追加のルールがあるかどうか聞いてくるので、ルールは複数選べます。 とりあえず、
id:
29
email:
7, 10, 19
password:
7, 19
type_id:
20
birthday:
8
img1:
29
img2:
29
created:
29
modified:
29
と、番号を選択して進みます。

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] > y
Member hasAndBelongsToMany Favorite? (y/n) 
[y] > y
Would you like to define some additional model associations? (y/n) 
[n] > n
とすると、
---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name:       Member
DB Table:   `members`
Validation: Array
(
    [email] => Array
        (
            [custom] => custom
            [email] => email
            [notempty] => notempty
        )

    [password] => Array
        (
            [custom] => custom
            [notempty] => notempty
        )

    [type_id] => Array
        (
            [numeric] => numeric
        )

    [birthday] => Array
        (
            [date] => date
        )

)

Associations:
 Member belongsTo Type
 Member hasAndBelongsToMany Favorite
---------------------------------------------------------------
Look okay? (y/n) 
[y] > 
とでるので、yesすると、
Baking model class for Member...

Creating file /Users/myname/Sites/cake/app/models/member.php
Wrote `/Users/myname/Sites/cake/app/models/member.php`
モデルが作られました。

最後にtestも作っておきます。

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 Member...

Creating file /Users/myname/Sites/cake/app/tests/fixtures/member_fixture.php
Wrote `/Users/myname/Sites/cake/app/tests/fixtures/member_fixture.php`
Bake is detecting possible fixtures..

Creating file /Users/myname/Sites/cake/app/tests/cases/models/member.test.php
Wrote `/Users/myname/Sites/cake/app/tests/cases/models/member.test.php`

作成された /app/models/member.php の中味はこんな感じです。

class Member extends AppModel {
 var $name = 'Member';
 var $displayField = 'email';
 var $validate = array(
  'email' => array(
   'custom' => array(
    'rule' => array('custom'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
   'email' => array(
    'rule' => array('email'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
   'notempty' => array(
    'rule' => array('notempty'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
  ),
  'password' => array(
   'custom' => array(
    'rule' => array('custom'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
   'notempty' => array(
    'rule' => array('notempty'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
  ),
  'type_id' => array(
   'numeric' => array(
    'rule' => array('numeric'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
  ),
  'birthday' => array(
   'date' => array(
    'rule' => array('date'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
  ),
 );
 //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';
 var $displayField = 'name';
 var $validate = array(
  'username' => array(
   'notempty' => array(
    'rule' => array('notempty'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
  ),
  'password' => array(
   'notempty' => array(
    'rule' => array('notempty'),
    //'message' => 'Your custom message here',
    //'allowEmpty' => false,
    //'required' => false,
    //'last' => false, // Stop validation after this rule
    //'on' => 'create', // Limit validation to 'create' or 'update' operations
   ),
  ),
 );
}