WebプログラミングV

 目次へ 前へ 

2015/2/3 久米

VCakePHP

7. bake (php自動生成コマンド)

ケーキを焼き上げる。

  1. スカフォールドレベルのphpを自動生成してくれる。
  2. 自動生成したphpを自分なりにカスタマイズしてゆく。
  3. こうすることにより、より作業効率がアップする。

 

7.1 テーブル作成

あらかじめ、bakeするためのテーブルをphpMyAdminなどで作っておく。

テーブル名 neko_ramens

CREATE TABLE IF NOT EXISTS `neko_ramens` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`price` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

7.2 bakeコマンド発行

データベース接続設定phpも作ってくれるが、
今回は既に自分で作ってあるものとして進める。app>Config>database.php

cd C:\xampp\htdocs\web3\cake_bookmarks
bin\cake bake all users
bin\cake bake all bookmarks
bin\cake bake all tags

コマンドプロンプトで

  1. SET PATHでpath設定を行う。環境変数に設定しておいてもよい。
  2. チェンジディレクトリコマンドcdで、cakePHPのappフォルダを指定
  3. bakeコマンドを打ち込む

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\kume>SET PATH=%PATH%;C:\xampp\php;C:\xampp\htdocs\web3cake\lib\Cake\Console

C:\Users\kume>cd C:\xampp\htdocs\web3cake\app

C:\xampp\htdocs\web3cake\app>cake bake

Welcome to CakePHP v2.4.3 Console
---------------------------------------------------------------
App : app
Path: C:\xampp\htdocs\web3cake\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)
>

7.3モデル作成

---------------------------------------------------------------
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
---------------------------------------------------------------
Bake Model
Path: C:\xampp\htdocs\web3cake\app\Model\
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >そのままEnterキー
Possible Models based on your current database:
 1. NekoBb
 2. NekoGazo
 3. NekoNikki
 4. NekoRamen
 5. NekoYohin
 6. SenseiNoNekochan
 7. User
Enter a number from the list above,
type in the name of another model, or 'q' to exit
[q] > 4
Would you like to supply validation criteria
for the fields in your model? (y/n)
[y] >そのままEnterキー

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.

[35] >そのままEnterキー

Field: name
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.

[24] >そのままEnterキー
Would you like to add another validation rule? (y/n)
[n] >そのままEnterキー

Field: price
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.

[25] >そのままEnterキー
Would you like to add another validation rule? (y/n)
[n] >そのままEnterキー

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.

[35] >そのままEnterキー

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.

[35] >そのままEnterキー
Would you like to define model associations
(hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] >そのままEnterキー
One moment while the associations are detected.
---------------------------------------------------------------
Please confirm the following associations:
---------------------------------------------------------------
Would you like to define some additional model associations? (y/n)
[n] >そのままEnterキー

---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name:       NekoRamen
DB Table:   `web3cake`.`neko_ramens`
Validation: Array
(
    [name] => Array
        (
            [notEmpty] => notEmpty
        )

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

)

Associations:
---------------------------------------------------------------
Look okay? (y/n)
[y] >そのままEnterキー

Baking model class for NekoRamen...

Creating file C:\xampp\htdocs\web3cake\app\Model\NekoRamen.php
Wrote `C:\xampp\htdocs\web3cake\app\Model\NekoRamen.php`

Baking test fixture for NekoRamen...

Creating file C:\xampp\htdocs\web3cake\app\Test\Fixture\NekoRamenFixture.php
Wrote `C:\xampp\htdocs\web3cake\app\Test\Fixture\NekoRamenFixture.php`
Bake is detecting possible fixtures...

Baking test case for NekoRamen Model ...

Creating file C:\xampp\htdocs\web3cake\app\Test\Case\Model\NekoRamenTest.php
Wrote `C:\xampp\htdocs\web3cake\app\Test\Case\Model\NekoRamenTest.php`

7.4コントローラ作成

---------------------------------------------------------------
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)
> C
---------------------------------------------------------------
Bake Controller
Path: C:\xampp\htdocs\web3cake\app\Controller\
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >そのままEnterキー
Possible Controllers based on your current database:
---------------------------------------------------------------
 1. NekoBbs
 2. NekoGazos
 3. NekoNikkis
 4. NekoRamens
 5. NekoYohins
 6. SenseiNoNekochans
 7. Users
Enter a number from the list above,
type in the name of another controller, or 'q' to exit
[q] > 4
---------------------------------------------------------------
Baking NekoRamensController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n)
[y] >そのままEnterキー
Would you like to use dynamic scaffolding? (y/n)
[n] >そのままEnterキー
Would you like to create some basic class methods
(index(), add(), view(), edit())? (y/n)
[n] > y
Would you like to create the basic class methods for admin routing? (y/n)
[n] >そのままEnterキー
Would you like this controller to use other helpers
besides HtmlHelper and FormHelper? (y/n)
[n] >そのままEnterキー
Would you like this controller to use other components
besides PaginatorComponent? (y/n)
[n] >そのままEnterキー
Would you like to use Session flash messages? (y/n)
[y] >そのままEnterキー

---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:
        NekoRamens
Components:
        Paginator
---------------------------------------------------------------
Look okay? (y/n)
[y] >そのままEnterキー

Baking controller class for NekoRamens...

Creating file C:\xampp\htdocs\web3cake\app\Controller\NekoRamensController.php
Wrote `C:\xampp\htdocs\web3cake\app\Controller\NekoRamensController.php`
Bake is detecting possible fixtures...

Baking test case for NekoRamens Controller ...

Creating file C:\xampp\htdocs\web3cake\app\Test\Case\Controller\NekoRamensContro
llerTest.php
Wrote `C:\xampp\htdocs\web3cake\app\Test\Case\Controller\NekoRamensControllerTes
t.php`

7.5ビュー作成

---------------------------------------------------------------
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)
> V
---------------------------------------------------------------
Bake View
Path: C:\xampp\htdocs\web3cake\app\View\
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >そのままEnterキー
Possible Controllers based on your current database:
---------------------------------------------------------------
 1. NekoBbs
 2. NekoGazos
 3. NekoNikkis
 4. NekoRamens
 5. NekoYohins
 6. SenseiNoNekochans
 7. Users
Enter a number from the list above,
type in the name of another controller, or 'q' to exit
[q] > 4
Would you like bake to build your views interactively?
Warning: Choosing no will overwrite NekoRamens views if it exist. (y/n)
[n] >そのままEnterキー

Baking `index` view file...

Creating file C:\xampp\htdocs\web3cake\app\View\NekoRamens\index.ctp
Wrote `C:\xampp\htdocs\web3cake\app\View\NekoRamens\index.ctp`

Baking `view` view file...

Creating file C:\xampp\htdocs\web3cake\app\View\NekoRamens\view.ctp
Wrote `C:\xampp\htdocs\web3cake\app\View\NekoRamens\view.ctp`

Baking `add` view file...

Creating file C:\xampp\htdocs\web3cake\app\View\NekoRamens\add.ctp
Wrote `C:\xampp\htdocs\web3cake\app\View\NekoRamens\add.ctp`

Baking `edit` view file...

Creating file C:\xampp\htdocs\web3cake\app\View\NekoRamens\edit.ctp
Wrote `C:\xampp\htdocs\web3cake\app\View\NekoRamens\edit.ctp`
---------------------------------------------------------------

View Scaffolding Complete.

---------------------------------------------------------------
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)
>Q

 

コラム バリデ―ションで自分が指定したメッセージを表示する方法

  • ソースコード中にこのrequiredが書かれていると、
    最新のブラウザでは、submit前入力チェックを行なってしまうため
    CakePHPで指定したエラーメッセージではなく、
    ブラウザが吐き出すエラーメッセージになってしまう
     
  • echo $this->Form->create('NekoRamen', array('novalidate' => true));
    のようにビューで指定すれば、このようなことにならず、
    自分が指定したメッセージが表示される。