【CakePHP】Bakeコマンドを利用したMVCファイルの作成


Railsのrakeなどのように、CakePHPにはbakeコマンドというコマンドがあります。
bakeコマンドを使うと、Model, Controller, Viewファイルの雛形ファイルを自動生成することができます。
またやり方次第では拡張し、自身が指定するテンプレートを選択してファイルを自動生成することもできます。
今回はユーザ登録のあるシステム開発をしているという前提で、ユーザに関するテーブルの作成、関連するMVCファイルの作成についてbakeを利用して実際にやってみるフローをまとめます。


## テーブルを作成
ユーザテーブルを作成します。
今回ここではリレーションなどは考えず、とりあえずusersのテーブルを作成しています。

    CREATE TABLE `cakephp_composer`.`users` (
      `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `accountname` VARCHAR(64) NOT NULL,
      `email` VARCHAR(255) NOT NULL,
      `password` VARCHAR(255) NOT NULL,
      `username` VARCHAR(255) NOT NULL,
      `profile` VARCHAR(1024) NULL,
      `created` DATETIME NOT NULL,
      `modified` DATETIME NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE INDEX `account_name_UNIQUE` (`accountname` ASC),
      UNIQUE INDEX `email_UNIQUE` (`email` ASC),
      UNIQUE INDEX `password_UNIQUE` (`password` ASC));


## Modelを作成

```sh
	$ cd [cakeのプロジェクト直下のディレクトリ]
```

```sh
	$ Console/cake bake	# bakeコマンドを叩く
	Welcome to CakePHP v2.5.5 Console
	---------------------------------------------------------------
	App : cakeApp
	Path: /var/www/html/cake_composer/cakeApp/
	---------------------------------------------------------------
	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と入力することでModelを生成することができる。
	---------------------------------------------------------------
	Bake Model
	Path: /var/www/html/cake_composer/cakeApp/Model/
	---------------------------------------------------------------
	Use Database Config: (default/test)
	[default] >	<----- 何も入力しないことでデフォルトの設定が反映される。ここではデフォルトのデータベース設定を利用するためこのままEnterでOK。
	Possible Models based on your current database:
	1. User
	Enter a number from the list above,
	type in the name of another model, or 'q' to exit
	[q] > 1			<------ 1と入力することで、上部に候補として挙げられている「1. User」に対するModelを作成することができる
    A displayField could not be automatically detected
    would you like to choose one? (y/n)
    > n				<------ displayFieldが何かわかってないので近々調べるはず。とりあえず「n(no)」を入力して今回は無視。
    Would you like to supply validation criteria
    for the fields in your model? (y/n)
    [y] > y		<------ 各カラムに対するバリデーションの設定をここで行うかどうか。基本的な設定を行っておくと楽なので「y(yesの意味)」を入力して行っておく。
    
    Field: id
    Type: integer
    ---------------------------------------------------------------
    Please select one of the following validation options:
    ---------------------------------------------------------------
     1. alphaNumeric               18. maxLength
     2. between                    19. mimeType
     3. blank                      20. minLength
     4. boolean                    21. money
     5. cc                         22. multiple
     6. comparison                 23. naturalNumber
     7. custom                     24. notEmpty
     8. date                       25. numeric
     9. datetime                   26. phone
    10. decimal                    27. postal
    11. email                      28. range
    12. equalTo                    29. ssn
    13. extension                  30. time
    14. fileSize                   31. uploadError
    15. inList                     32. url
    16. ip                         33. userDefined
    17. luhn                       34. uuid
    
    35 - Do not do any validation on this field.
    ---------------------------------------------------------------
    or enter in a valid regex validation string.
    Alternatively [s] skip the rest of the fields.
    
    [35] >		<----- 35を推奨のバリデーションとして提示してくれ問題なければ「Enter」すればそれを選択したことになる。自分で上記数字を入力すると、入力した数値に対応するバリデーションの設定を行うことができる。
    
    Field: accountname
    Type: string
    ---------------------------------------------------------------
    Please select one of the following validation options:
    ---------------------------------------------------------------
     1. alphaNumeric               18. maxLength
     2. between                    19. mimeType
     3. blank                      20. minLength
     4. boolean                    21. money
     5. cc                         22. multiple
     6. comparison                 23. naturalNumber
     7. custom                     24. notEmpty
     8. date                       25. numeric
     9. datetime                   26. phone
    10. decimal                    27. postal
    11. email                      28. range
    12. equalTo                    29. ssn
    13. extension                  30. time
    14. fileSize                   31. uploadError
    15. inList                     32. url
    16. ip                         33. userDefined
    17. luhn                       34. uuid
    
    35 - Do not do any validation on this field.
    ---------------------------------------------------------------
    or enter in a valid regex validation string.
    Alternatively [s] skip the rest of the fields.
    
    [24] >		<----- 24の推奨設定でOKなので、そのまま「Enter」
    Would you like to add another validation rule
    or skip the rest of the fields? (y/n/s)
    [n] >		<----- 同じカラムに追加でバリデーション設定をする場合は「y(yesの意味)」、次のカラムへバリデーション設定を行う場合は「n(noの意味)」、残りのフィールドへのバリデーション設定を全部スキップする場合は「s(skipの意味)」を入力。今回は次のフィールドの設定へ移動するので、推奨されている「n」のままで良いので、そのまま「Enter」を押下。
    
    Field: email
    Type: string
    ---------------------------------------------------------------
    Please select one of the following validation options:
    ---------------------------------------------------------------
     1. alphaNumeric               18. maxLength
     2. between                    19. mimeType
     3. blank                      20. minLength
     4. boolean                    21. money
     5. cc                         22. multiple
     6. comparison                 23. naturalNumber
     7. custom                     24. notEmpty
     8. date                       25. numeric
     9. datetime                   26. phone
    10. decimal                    27. postal
    11. email                      28. range
    12. equalTo                    29. ssn
    13. extension                  30. time
    14. fileSize                   31. uploadError
    15. inList                     32. url
    16. ip                         33. userDefined
    17. luhn                       34. uuid
    
    35 - Do not do any validation on this field.
    ---------------------------------------------------------------
    or enter in a valid regex validation string.
    Alternatively [s] skip the rest of the fields.
    
    [11] >
    Would you like to add another validation rule
    or skip the rest of the fields? (y/n/s)
    [n] >
    
    Field: password
    Type: string
    ---------------------------------------------------------------
    Please select one of the following validation options:
    ---------------------------------------------------------------
     1. alphaNumeric               18. maxLength
     2. between                    19. mimeType
     3. blank                      20. minLength
     4. boolean                    21. money
     5. cc                         22. multiple
     6. comparison                 23. naturalNumber
     7. custom                     24. notEmpty
     8. date                       25. numeric
     9. datetime                   26. phone
    10. decimal                    27. postal
    11. email                      28. range
    12. equalTo                    29. ssn
    13. extension                  30. time
    14. fileSize                   31. uploadError
    15. inList                     32. url
    16. ip                         33. userDefined
    17. luhn                       34. uuid
    
    35 - Do not do any validation on this field.
    ---------------------------------------------------------------
    or enter in a valid regex validation string.
    Alternatively [s] skip the rest of the fields.
    
    [24] >
    Would you like to add another validation rule
    or skip the rest of the fields? (y/n/s)
    [n] >
    
    Field: username
    Type: string
    ---------------------------------------------------------------
    Please select one of the following validation options:
    ---------------------------------------------------------------
     1. alphaNumeric               18. maxLength
     2. between                    19. mimeType
     3. blank                      20. minLength
     4. boolean                    21. money
     5. cc                         22. multiple
     6. comparison                 23. naturalNumber
     7. custom                     24. notEmpty
     8. date                       25. numeric
     9. datetime                   26. phone
    10. decimal                    27. postal
    11. email                      28. range
    12. equalTo                    29. ssn
    13. extension                  30. time
    14. fileSize                   31. uploadError
    15. inList                     32. url
    16. ip                         33. userDefined
    17. luhn                       34. uuid
    
    35 - Do not do any validation on this field.
    ---------------------------------------------------------------
    or enter in a valid regex validation string.
    Alternatively [s] skip the rest of the fields.
    
    [24] >
    Would you like to add another validation rule
    or skip the rest of the fields? (y/n/s)
    [n] >
    
    Field: profile
    Type: string
    ---------------------------------------------------------------
    Please select one of the following validation options:
    ---------------------------------------------------------------
     1. alphaNumeric               18. maxLength
     2. between                    19. mimeType
     3. blank                      20. minLength
     4. boolean                    21. money
     5. cc                         22. multiple
     6. comparison                 23. naturalNumber
     7. custom                     24. notEmpty
     8. date                       25. numeric
     9. datetime                   26. phone
    10. decimal                    27. postal
    11. email                      28. range
    12. equalTo                    29. ssn
    13. extension                  30. time
    14. fileSize                   31. uploadError
    15. inList                     32. url
    16. ip                         33. userDefined
    17. luhn                       34. uuid
    
    35 - Do not do any validation on this field.
    ---------------------------------------------------------------
    or enter in a valid regex validation string.
    Alternatively [s] skip the rest of the fields.
    
    [35] >
    
    Field: created
    Type: datetime
    ---------------------------------------------------------------
    Please select one of the following validation options:
    ---------------------------------------------------------------
     1. alphaNumeric               18. maxLength
     2. between                    19. mimeType
     3. blank                      20. minLength
     4. boolean                    21. money
     5. cc                         22. multiple
     6. comparison                 23. naturalNumber
     7. custom                     24. notEmpty
     8. date                       25. numeric
     9. datetime                   26. phone
    10. decimal                    27. postal
    11. email                      28. range
    12. equalTo                    29. ssn
    13. extension                  30. time
    14. fileSize                   31. uploadError
    15. inList                     32. url
    16. ip                         33. userDefined
    17. luhn                       34. uuid
    
    35 - Do not do any validation on this field.
    ---------------------------------------------------------------
    or enter in a valid regex validation string.
    Alternatively [s] skip the rest of the fields.
    
    [35] >
    
    Field: modified
    Type: datetime
    ---------------------------------------------------------------
    Please select one of the following validation options:
    ---------------------------------------------------------------
     1. alphaNumeric               18. maxLength
     2. between                    19. mimeType
     3. blank                      20. minLength
     4. boolean                    21. money
     5. cc                         22. multiple
     6. comparison                 23. naturalNumber
     7. custom                     24. notEmpty
     8. date                       25. numeric
     9. datetime                   26. phone
    10. decimal                    27. postal
    11. email                      28. range
    12. equalTo                    29. ssn
    13. extension                  30. time
    14. fileSize                   31. uploadError
    15. inList                     32. url
    16. ip                         33. userDefined
    17. luhn                       34. uuid
    
    35 - Do not do any validation on this field.
    ---------------------------------------------------------------
    or enter in a valid regex validation string.
    Alternatively [s] skip the rest of the fields.
    
    [35] >
    Would you like to define model associations
    (hasMany, hasOne, belongsTo, etc.)? (y/n)
    [y] > n	<----- データベースリレーションの設定もやっておく？と聞かれるが、今回はUsersテーブル単体の話なので「n」を入力して「Enter」を押下。
    
    ---------------------------------------------------------------
    The following Model will be created:
    ---------------------------------------------------------------
    Name:       User
    DB Table:   `cakephp_composer`.`Users`
    Validation: Array
    (
        [accountname] => Array
            (
                [notEmpty] => notEmpty
            )
    
        [email] => Array
            (
                [email] => email
            )
    
        [password] => Array
            (
                [notEmpty] => notEmpty
            )
    
        [username] => Array
            (
                [notEmpty] => notEmpty
            )
    
    )
    
    ---------------------------------------------------------------
    Look okay? (y/n)
    [y] > y	<----- これでよい？って聞かれるので「y(yes)」と入力してEnter。
    
    Baking model class for User...
    
    Creating file /var/www/html/cake_composer/cakeApp/Model/User.php
    Wrote `/var/www/html/cake_composer/cakeApp/Model/User.php`
    
    Baking test fixture for User...
    
    Creating file /var/www/html/cake_composer/cakeApp/Test/Fixture/UserFixture.php
    Wrote `/var/www/html/cake_composer/cakeApp/Test/Fixture/UserFixture.php`
    Bake is detecting possible fixtures...
    
    Baking test case for User Model ...
    
    Creating file /var/www/html/cake_composer/cakeApp/Test/Case/Model/UserTest.php
    Wrote `/var/www/html/cake_composer/cakeApp/Test/Case/Model/UserTest.php`

```

以上で、Usersテーブルに関するModelクラスのbakeが完了です！！



## Controllerを作成
あとで。



## Viewファイルを作成
あとで。