閲覧総計:7624 (本日:1 昨日:0)
CakePHPには「Bake」という質問に答えていくだけでCakePHPのコードを自動生成してく
れる便利なツールがある。
早速、先に勉強した「ブログチュートリアル」と同じものをBakeで自動作成したみた。 08/12/31
(参考)
ブログチュートリアルをやってみた
【前提】
●Windowsローカル環境で/cakephp/blog2/フォルダにBakeした事例です。
●データベース名はblog、テーブル名はpostを利用した。
●日本語文字コードはEUCです。
●入力は赤文字で示した。「↓」はEnterキー。
【目次】(項目をクリックすると、そこに飛べます)
プロジェクトの作成&データベース設定 †
コマンド プロンプトを起動

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\hoge>cd C:\xampp\htdocs\cake\cake\console↓
C:\xampp\htdocs\cake\cake\console>php cake.php bake -app C:\xampp\htdocs\cakephp↓
Welcome to CakePHP v1.2.0.7962 Console
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
App : cakephp
Path: C:\xampp\htdocs\cakephp
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
What is the full path for this app including the app directory name?
Example: C:\xampp\htdocs\cakephp\myapp
[C:\xampp\htdocs\cakephp\myapp] > C:\xampp\htdocs\cakephp\blog2↓
Bake Project
Skel Directory: C:\xampp\htdocs\cake\cake\console\libs\templates\skel
Will be copied to: C:\xampp\htdocs\cakephp\blog2
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Look okay? (y/n/q)
[y] >↓
Do you want verbose output? (y/n)
[n] > y↓
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Created: blog2 in C:\xampp\htdocs\cakephp\blog2
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
C:\xampp\htdocs\cakephp\blog2 created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\.htaccess copied to C:\xam
pp\htdocs\cakephp\blog2\.htaccess
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\app_controller.php copied
to C:\xampp\htdocs\cakephp\blog2\app_controller.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\app_helper.php copied to C
:\xampp\htdocs\cakephp\blog2\app_helper.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\app_model.php copied to C:
\xampp\htdocs\cakephp\blog2\app_model.php
C:\xampp\htdocs\cakephp\blog2\config created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\acl.ini.php copied
to C:\xampp\htdocs\cakephp\blog2\config\acl.ini.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\bootstrap.php copie
d to C:\xampp\htdocs\cakephp\blog2\config\bootstrap.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\core.php copied to
C:\xampp\htdocs\cakephp\blog2\config\core.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\database.php.defaul
t copied to C:\xampp\htdocs\cakephp\blog2\config\database.php.default
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\inflections.php cop
ied to C:\xampp\htdocs\cakephp\blog2\config\inflections.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\routes.php copied t
o C:\xampp\htdocs\cakephp\blog2\config\routes.php
C:\xampp\htdocs\cakephp\blog2\config\sql created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\sql\db_acl.php copi
ed to C:\xampp\htdocs\cakephp\blog2\config\sql\db_acl.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\sql\db_acl.sql copi
ed to C:\xampp\htdocs\cakephp\blog2\config\sql\db_acl.sql
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\sql\i18n.php copied
to C:\xampp\htdocs\cakephp\blog2\config\sql\i18n.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\sql\i18n.sql copied
to C:\xampp\htdocs\cakephp\blog2\config\sql\i18n.sql
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\sql\sessions.php co
pied to C:\xampp\htdocs\cakephp\blog2\config\sql\sessions.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\config\sql\sessions.sql co
pied to C:\xampp\htdocs\cakephp\blog2\config\sql\sessions.sql
C:\xampp\htdocs\cakephp\blog2\controllers created
C:\xampp\htdocs\cakephp\blog2\controllers\components created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\controllers\pages_controll
er.php copied to C:\xampp\htdocs\cakephp\blog2\controllers\pages_controller.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\index.php copied to C:\xam
pp\htdocs\cakephp\blog2\index.php
C:\xampp\htdocs\cakephp\blog2\locale created
C:\xampp\htdocs\cakephp\blog2\locale\eng created
C:\xampp\htdocs\cakephp\blog2\locale\eng\LC_MESSAGES created
C:\xampp\htdocs\cakephp\blog2\models created
C:\xampp\htdocs\cakephp\blog2\models\behaviors created
C:\xampp\htdocs\cakephp\blog2\models\datasources created
C:\xampp\htdocs\cakephp\blog2\plugins created
C:\xampp\htdocs\cakephp\blog2\tests created
C:\xampp\htdocs\cakephp\blog2\tests\cases created
C:\xampp\htdocs\cakephp\blog2\tests\cases\behaviors created
C:\xampp\htdocs\cakephp\blog2\tests\cases\components created
C:\xampp\htdocs\cakephp\blog2\tests\cases\controllers created
C:\xampp\htdocs\cakephp\blog2\tests\cases\helpers created
C:\xampp\htdocs\cakephp\blog2\tests\cases\models created
C:\xampp\htdocs\cakephp\blog2\tests\fixtures created
C:\xampp\htdocs\cakephp\blog2\tests\groups created
C:\xampp\htdocs\cakephp\blog2\tmp created
C:\xampp\htdocs\cakephp\blog2\tmp\cache created
C:\xampp\htdocs\cakephp\blog2\tmp\cache\models created
C:\xampp\htdocs\cakephp\blog2\tmp\cache\persistent created
C:\xampp\htdocs\cakephp\blog2\tmp\cache\views created
C:\xampp\htdocs\cakephp\blog2\tmp\logs created
C:\xampp\htdocs\cakephp\blog2\tmp\sessions created
C:\xampp\htdocs\cakephp\blog2\tmp\tests created
C:\xampp\htdocs\cakephp\blog2\vendors created
C:\xampp\htdocs\cakephp\blog2\vendors\shells created
C:\xampp\htdocs\cakephp\blog2\vendors\shells\tasks created
C:\xampp\htdocs\cakephp\blog2\vendors\shells\templates created
C:\xampp\htdocs\cakephp\blog2\views created
C:\xampp\htdocs\cakephp\blog2\views\elements created
C:\xampp\htdocs\cakephp\blog2\views\elements\email created
C:\xampp\htdocs\cakephp\blog2\views\elements\email\html created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\elements\email\html\
default.ctp copied to C:\xampp\htdocs\cakephp\blog2\views\elements\email\html\de
fault.ctp
C:\xampp\htdocs\cakephp\blog2\views\elements\email\text created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\elements\email\text\
default.ctp copied to C:\xampp\htdocs\cakephp\blog2\views\elements\email\text\de
fault.ctp
C:\xampp\htdocs\cakephp\blog2\views\errors created
C:\xampp\htdocs\cakephp\blog2\views\helpers created
C:\xampp\htdocs\cakephp\blog2\views\layouts created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\layouts\ajax.ctp cop
ied to C:\xampp\htdocs\cakephp\blog2\views\layouts\ajax.ctp
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\layouts\default.ctp
copied to C:\xampp\htdocs\cakephp\blog2\views\layouts\default.ctp
C:\xampp\htdocs\cakephp\blog2\views\layouts\email created
C:\xampp\htdocs\cakephp\blog2\views\layouts\email\html created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\layouts\email\html\d
efault.ctp copied to C:\xampp\htdocs\cakephp\blog2\views\layouts\email\html\defa
ult.ctp
C:\xampp\htdocs\cakephp\blog2\views\layouts\email\text created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\layouts\email\text\d
efault.ctp copied to C:\xampp\htdocs\cakephp\blog2\views\layouts\email\text\defa
ult.ctp
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\layouts\flash.ctp co
pied to C:\xampp\htdocs\cakephp\blog2\views\layouts\flash.ctp
C:\xampp\htdocs\cakephp\blog2\views\layouts\js created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\layouts\js\default.c
tp copied to C:\xampp\htdocs\cakephp\blog2\views\layouts\js\default.ctp
C:\xampp\htdocs\cakephp\blog2\views\layouts\rss created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\layouts\rss\default.
ctp copied to C:\xampp\htdocs\cakephp\blog2\views\layouts\rss\default.ctp
C:\xampp\htdocs\cakephp\blog2\views\layouts\xml created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\views\layouts\xml\default.
ctp copied to C:\xampp\htdocs\cakephp\blog2\views\layouts\xml\default.ctp
C:\xampp\htdocs\cakephp\blog2\views\pages created
C:\xampp\htdocs\cakephp\blog2\views\scaffolds created
C:\xampp\htdocs\cakephp\blog2\webroot created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\.htaccess copied t
o C:\xampp\htdocs\cakephp\blog2\webroot\.htaccess
C:\xampp\htdocs\cakephp\blog2\webroot\css created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\css\cake.generic.c
ss copied to C:\xampp\htdocs\cakephp\blog2\webroot\css\cake.generic.css
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\css.php copied to
C:\xampp\htdocs\cakephp\blog2\webroot\css.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\favicon.ico copied
to C:\xampp\htdocs\cakephp\blog2\webroot\favicon.ico
C:\xampp\htdocs\cakephp\blog2\webroot\files created
C:\xampp\htdocs\cakephp\blog2\webroot\img created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\img\cake.icon.gif
copied to C:\xampp\htdocs\cakephp\blog2\webroot\img\cake.icon.gif
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\img\cake.power.gif
copied to C:\xampp\htdocs\cakephp\blog2\webroot\img\cake.power.gif
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\index.php copied t
o C:\xampp\htdocs\cakephp\blog2\webroot\index.php
C:\xampp\htdocs\cakephp\blog2\webroot\js created
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\js\vendors.php cop
ied to C:\xampp\htdocs\cakephp\blog2\webroot\js\vendors.php
C:\xampp\htdocs\cake\cake\console\libs\templates\skel\webroot\test.php copied to
C:\xampp\htdocs\cakephp\blog2\webroot\test.php
Creating file C:\xampp\htdocs\cakephp\blog2\views\pages\home.ctp
Wrote C:\xampp\htdocs\cakephp\blog2\views\pages\home.ctp
Welcome page created
Random hash key created for 'Security.salt'
CAKE_CORE_INCLUDE_PATH set to C:\xampp\htdocs\cake in webroot/index.php
CAKE_CORE_INCLUDE_PATH set to C:\xampp\htdocs\cake in webroot/test.php
Remember to check these value after moving to production server
Your database configuration was not found. Take a moment to create one.
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Database Configuration:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Name:
[default] >↓
Driver: (db2/firebird/mssql/mysql/mysqli/odbc/oracle/postgres/sqlite/sybase)
[mysql] >↓
Persistent Connection? (y/n)
[n] >↓
Database Host:
[localhost] >↓
Port?
[n] >↓
User:
[root] >↓
Password:
>↓
The password you supplied was empty. Use an empty password? (y/n)
[n] > y↓
Database Name:
[cake] > blog↓
Table Prefix?
[n] >↓
Table encoding?
[n] > ujis↓ //通常は「utf8」ですが、私の場合は公開用サーバの文字コードはEUC(ujis)なのでujisでローカル開発してます。
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
The following database configuration will be created:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Name: default
Driver: mysql
Persistent: false
Host: localhost
User: root
Pass:
Database: blog
Encoding: ujis
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Look okay? (y/n)
[y] >↓
Do you wish to add another database configuration?
[n] >↓
Creating file C:\xampp\htdocs\cakephp\blog2\config\database.php
Wrote C:\xampp\htdocs\cakephp\blog2\config\database.php
C:\xampp\htdocs\cake\cake\console>
接続状態の確認 †
http://localhost/cakephp/blog2/でアクセスしてみる。

●オールグリーなら問題なし。
modelの作成 †
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\hoge>cd C:\xampp\htdocs\cake\cake\console↓
C:\xampp\htdocs\cake\cake\console>php cake.php bake -app C:\xampp\htdocs\cakephp\blog2↓
Welcome to CakePHP v1.2.0.7962 Console
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
App : blog2
Path: C:\xampp\htdocs\cakephp\blog2
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
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↓
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Bake Model
Path: C:\xampp\htdocs\cakephp\blog2\models\
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Possible Models based on your current database:
1. Post
Enter a number from the list above, type in the name of another model, or 'q' to
exit
[q] > 1↓
Would you like to supply validation criteria for the fields in your model? (y/n)
[y] >↓
Field: id
Type: integer
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Please select one of the following validation options:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
1 - alphaNumeric
2 - between
(中略)
28 - userDefined
29 - Do not do any validation on this field.
... or enter in a valid regex validation string.
[29] >↓
Field: title
Type: string
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
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 - file
14 - inList
15 - ip
16 - maxLength
17 - minLength
18 - money
19 - multiple
20 - notEmpty
21 - numeric
22 - phone
23 - postal
24 - range
25 - ssn
26 - time
27 - url
28 - userDefined
29 - Do not do any validation on this field.
... or enter in a valid regex validation string.
[29] > 17↓
Field: body
Type: text
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Please select one of the following validation options:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
1 - alphaNumeric
2 - between
(中略)
28 - userDefined
29 - Do not do any validation on this field.
... or enter in a valid regex validation string.
[29] > 17↓
Field: created
Type: datetime
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Please select one of the following validation options:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
1 - alphaNumeric
2 - between
(中略)
28 - userDefined
29 - Do not do any validation on this field.
... or enter in a valid regex validation string.
[29] >↓
Field: modified
Type: datetime
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Please select one of the following validation options:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
1 - alphaNumeric
2 - between
(中略)
28 - userDefined
29 - Do not do any validation on this field.
... or enter in a valid regex validation string.
[29] >↓
Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] > n↓
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
The following Model will be created:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Name: Post
Validation: Array
(
[title] => minlength
[body] => minlength
)
Associations:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Look okay? (y/n)
[y] >↓
Baking model class for Post...
Creating file C:\xampp\htdocs\cakephp\blog2\models\post.php
Wrote C:\xampp\htdocs\cakephp\blog2\models\post.php
Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
[y] >↓
You can download the Cake test suite from http://cakeforge.org/projects/testsuite/
Baking test fixture for Post...
Creating file C:\xampp\htdocs\cakephp\blog2\tests\fixtures\post_fixture.php
Wrote C:\xampp\htdocs\cakephp\blog2\tests\fixtures\post_fixture.php
Baking unit test for Post...
Creating file C:\xampp\htdocs\cakephp\blog2\tests\cases\models\post.test.php
Wrote C:\xampp\htdocs\cakephp\blog2\tests\cases\models\post.test.php
controllerの作成 †
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
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)
> c↓
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Bake Controller
Path: C:\xampp\htdocs\cakephp\blog2\controllers\
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Possible Controllers based on your current database:
1. Posts
Enter a number from the list above, type in the name of another controller, or 'q' to exit
[q] > 1↓
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Baking PostsController
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Would you like to build your controller interactively? (y/n)
[y] >↓
Would you like to use scaffolding? (y/n)
[n] >↓
Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
[n] > y↓
Would you like to create the methods for admin routing? (y/n)
[n] >↓
Would you like this controller to use other helpers besides HtmlHelper? and FormHelper?? (y/n)
[n] >↓
Would you like this controller to use any components? (y/n)
[n] >↓
Would you like to use Sessions? (y/n)
[y] > n↓
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
The following controller will be created:
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Controller Name: Posts
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Look okay? (y/n)
[y] >↓
Creating file C:\xampp\htdocs\cakephp\blog2\controllers\posts_controller.php
Wrote C:\xampp\htdocs\cakephp\blog2\controllers\posts_controller.php
Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
[y] >↓
You can download the Cake test suite from http://cakeforge.org/projects/testsuite/
Baking unit test for Posts...
Creating file C:\xampp\htdocs\cakephp\blog2\tests\cases\controllers\posts_controller.test.php
Wrote C:\xampp\htdocs\cakephp\blog2\tests\cases\controllers\posts_controller.test.php
viewの作成 †
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
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)
> v↓
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Bake View
Path: C:\xampp\htdocs\cakephp\blog2\views\
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Possible Controllers based on your current database:
1. Posts
Enter a number from the list above, type in the name of another controller, or 'q' to exit
[q] > 1↓
Would you like to create some scaffolded views (index, add, view, edit) for this controller?
NOTE: Before doing so, you'll need to create your controller and model classes (including associated models). (y/n)
[n] > y↓
Would you like to create the views for admin routing? (y/n)
[y] > n↓
Creating file C:\xampp\htdocs\cakephp\blog2\views\posts\index.ctp
Wrote C:\xampp\htdocs\cakephp\blog2\views\posts\index.ctp
Creating file C:\xampp\htdocs\cakephp\blog2\views\posts\view.ctp
Wrote C:\xampp\htdocs\cakephp\blog2\views\posts\view.ctp
Creating file C:\xampp\htdocs\cakephp\blog2\views\posts\add.ctp
Wrote C:\xampp\htdocs\cakephp\blog2\views\posts\add.ctp
Creating file C:\xampp\htdocs\cakephp\blog2\views\posts\edit.ctp
Wrote C:\xampp\htdocs\cakephp\blog2\views\posts\edit.ctp
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
View Scaffolding Complete.
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
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)
> q↓
C:\xampp\htdocs\cake\cake\console>
完成品の動作確認 †
http://www.hoge.co.jp/cakephp/blog/myapp/posts/にアクセスし、下記画面が表示されればOK。
自動生成されたコードの確認 †
/config/database.phpの自動生成コード †
/cakephp/blog2/config/database.php
<?php
<?php
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'blog',
'encoding' => 'ujis'
);
}
?>
modelの自動生成コード †
/cakephp/blog2/models/post.php
<?php
class Post extends AppModel {
var $name = 'Post';
var $validate = array(
'title' => array('minlength'),
'body' => array('minlength')
);
}
?>
controllerの自動生成コード †
特にposts_controller.phpの自動生成コードは参考になります。
/cakephp/blog2/controllers/posts_controller.php
<?php
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html', 'Form');
function index() {
$this->Post->recursive = 0;
$this->set('posts', $this->paginate());
}
function view($id = null) {
if (!$id) {
$this->flash(__('Invalid Post', true), array('action'=>'index'));
}
$this->set('post', $this->Post->read(null, $id));
}
function add() {
if (!empty($this->data)) {
$this->Post->create();
if ($this->Post->save($this->data)) {
$this->flash(__('Post saved.', true), array('action'=>'index'));
} else {
}
}
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->flash(__('Invalid Post', true), array('action'=>'index'));
}
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
$this->flash(__('The Post has been saved.', true), array('action'=>'index'));
} else {
}
}
if (empty($this->data)) {
$this->data = $this->Post->read(null, $id);
}
}
function delete($id = null) {
if (!$id) {
$this->flash(__('Invalid Post', true), array('action'=>'index'));
}
if ($this->Post->del($id)) {
$this->flash(__('Post deleted', true), array('action'=>'index'));
}
}
}
?>
viewの自動生成コード †
/cakephp/blog/myapp/views/posts/index.ctp
<div class="posts index">
<h2><?php __('Posts');?></h2>
<p>
<?php
echo $paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on reco rd
%start%, ending on %end%', true)
));
?></p>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $paginator->sort('id');?></th>
<th><?php echo $paginator->sort('title');?></th>
<th><?php echo $paginator->sort('body');?></th>
<th><?php echo $paginator->sort('created');?></th>
<th><?php echo $paginator->sort('modified');?></th>
<th class="actions"><?php __('Actions');?></th>
</tr>
<?php
$i = 0;
foreach ($posts as $post):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}
?>
<tr<?php echo $class;?>>
<td>
<?php echo $post['Post']['id']; ?>
</td>
<td>
<?php echo $post['Post']['title']; ?>
</td>
<td>
<?php echo $post['Post']['body']; ?>
</td>
<td>
<?php echo $post['Post']['created']; ?>
</td>
<td>
<?php echo $post['Post']['modified']; ?>
</td>
<td class="actions">
<?php echo $html->link(__('View', true), array('action'=>'view', $post['Post']['id'])); ?>
<?php echo $html->link(__('Edit', true), array('action'=>'edit', $post['Post']['id'])); ?>
<?php echo $html->link(__('Delete', true), array('action'=>'delete', $post['Post']['id']),
n ull, sprintf(__('Are you sure you want to delete # %s?', true), $post['Post']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div class="paging">
<?php echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));? >
| <?php echo $paginator->numbers();?>
<?php echo $paginator->next(__('next', true).' >>', array(), null, array('class'=>'disabled'));?>
</div>
<div class="actions">
<ul>
<li><?php echo $html->link(__('New Post', true), array('action'=>'add')); ?></li>
</ul>
</div>
/cakephp/blog/myapp/views/posts/view.ctp
<div class="posts view">
<h2><?php __('Post');?></h2>
<dl><?php $i = 0; $class = ' class="altrow"';?>
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Id'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
<?php echo $post['Post']['id']; ?>
</dd>
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Title'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
<?php echo $post['Post']['title']; ?>
</dd>
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Body'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
<?php echo $post['Post']['body']; ?>
</dd>
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Created'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
<?php echo $post['Post']['created']; ?>
</dd>
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Modified'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
<?php echo $post['Post']['modified']; ?>
</dd>
</dl>
</div>
<div class="actions">
<ul>
<li><?php echo $html->link(__('Edit Post', true), array('action'=>'edit', $post['Post']['id'])); ?> </li>
<li><?php echo $html->link(__('Delete Post', true), array('action'=>'delete', $post['Post']['id' ]),
null, sprintf(__('Are you sure you want to delete # %s?', true), $post['Post']['id'])); ?> </li>
<li><?php echo $html->link(__('List Posts', true), array('action'=>'index')); ?> </li>
<li><?php echo $html->link(__('New Post', true), array('action'=>'add')); ?> </li>
</ul>
</div>
/cakephp/blog/myapp/views/posts/edit.ctp
<div class="posts form">
<?php echo $form->create('Post');?>
<fieldset>
<legend><?php __('Edit Post');?></legend>
<?php
echo $form->input('id');
echo $form->input('title');
echo $form->input('body');
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
<div class="actions">
<ul>
<li><?php echo $html->link(__('Delete', true), array('action'=>'delete', $form->value('Post.id') ),
null, sprintf(__('Are you sure you want to delete # %s?', true), $form->value('Post.id'))); ?></li>
<li><?php echo $html->link(__('List Posts', true), array('action'=>'index'));?></li>
</ul>
</div>
稼働画面 †


