手抜きの方法をいろいろとやっています。前回は,dbdesigner2cake.phpを使ってDBDesigner4で作ったデータベース設計のxmlから全自動で,モデルとコントローラ(Scaffoldを使用)を作成するというのをやりました。しかしながらCakePHPのリファレンスガイドにもある通りScaffoldはやはりScaffoldにすぎません(scaffolding is... well... just scaffolding. )し,ビューの作成をまだやっていません。そこでbake.phpを使ってコントローラとビューを作ってみます。
bake.phpは,<インストール先>\cake\scriptsにあります。そこにカレントディレクトリを移動して実行してみます。
コントローラをbakeしてみる
bake.phpを起動します。
C:\xampp\htdocs\webapp\cake\scripts>php bake.php
___ __ _ _ ___ __ _ _ __ __ __ _ _ ___
| |__| |_/ |__ |__] |__| |__] |__] |__| |_/ |__
|___ | | | \_ |___ | | | | |__] | | | \_ |___
---------------------------------------------------------------
Bake -app in C:\xampp\htdocs\webapp\app (y/n)
[y] > y
yを入力
Baking...
---------------------------------------------------------------
Name: app
Path: C:\xampp\htdocs\webapp\app
---------------------------------------------------------------
[M]odel
[C]ontroller
[V]iew
What would you like to Bake? (M/V/C)
> c
コントローラを選択
---------------------------------------------------------------
Controller Bake:
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Employees
2. EventKinds
3. Groups
4. TimeCardsEnter a number from the list above, or type in the name of another controller.
> 1
Employeesを選択
Would you like bake to build your controller interactively?
Warning: Choosing no will overwrite controller if it exist. (y/n)
[y] > n
インタラクティブにbakeするか?というのでとりあえずnを選択。yにすると以降の質問が多くなりそう。
Would you like to include some basic class methods (index(), add(), view(), edit
())? (y/n)
[y] > y
インタラクティブではないと選択したのにインタラクティブに質問してくるのはなぜ。基本的なクラスメソッドを作るかどうかということなので作ってみる。
Would you like to create the methods for admin routing? (y/n)
[y] > n
admin routingというのはアクセスするurlに関する指定のようですが,まだ不勉強ですのでここのnにしておきます。
Creating file C:\xampp\htdocs\webapp\app\controllers\employees_controller.php
WroteC:\xampp\htdocs\webapp\app\controllers\employees_controller.php
Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
testファイルを作るかどうかということですが,ここではnにしておきます。
以上の操作でコントローラが作成されました。ここでブラウザから表示させてみたら「ビューがない」というエラーが表示されましたので,続いてbake.phpでビューも焼いてみます。
ビューをbakeする
C:\xampp\htdocs\webapp\cake\scripts>php bake.php
[M]odel
[C]ontroller
[V]iew
What would you like to Bake? (M/V/C)
> v
あとは,コントローラの時よりも質問が少ないです。
---------------------------------------------------------------
View Bake:
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Employees
2. EventKinds
3. Groups
4. TimeCards
Enter a number from the list above, or type in the name of another controller.
> 1
Would you like bake to build your views interactively?
Warning: Choosing no will overwrite views if it exist. (y/n)
[y] > n
Would you like to create the views for admin routing? (y/n)
[n] > n
ここまで入力すると実行開始。
Creating file C:\xampp\htdocs\webapp\app\views\employees\index.thtml
WroteC:\xampp\htdocs\webapp\app\views\employees\index.thtml
Creating file C:\xampp\htdocs\webapp\app\views\employees\view.thtml
WroteC:\xampp\htdocs\webapp\app\views\employees\view.thtml
Creating file C:\xampp\htdocs\webapp\app\views\employees\add.thtml
WroteC:\xampp\htdocs\webapp\app\views\employees\add.thtml
Creating file C:\xampp\htdocs\webapp\app\views\employees\edit.thtml
WroteC:\xampp\htdocs\webapp\app\views\employees\edit.thtml
---------------------------------------------------------------
View Scaffolding Complete.
こちらはサマリも表示されました。コントローラの時にはなかったような。
できあがったコントローラ
<?php class EmployeesController extends AppController { %u3000 var $name = 'Employees'; var $helpers = array('Html', 'Form' ); %u3000 function index() { $this->Employee->recursive = 0; $this->set('employees', $this->Employee->findAll()); } function view($id = null) { if (!$id) { $this->Session->setFlash('Invalid id for Employee.'); $this->redirect('/employees/index'); } $this->set('employee', $this->Employee->read(null, $id)); } function add() { if (empty($this->data)) { $this->set('groups', $this->Employee->Group->generateList()); $this->render(); } else { $this->cleanUpFields(); if ($this->Employee->save($this->data)) { $this->Session->setFlash('The Employee has been saved'); $this->redirect('/employees/index'); } else { $this->Session->setFlash('Please correct errors below.'); $this->set('groups', $this->Employee->Group->generateList()); } } } function edit($id = null) { if (empty($this->data)) { if (!$id) { $this->Session->setFlash('Invalid id for Employee'); $this->redirect('/employees/index'); } $this->data = $this->Employee->read(null, $id); $this->set('groups', $this->Employee->Group->generateList()); } else { $this->cleanUpFields(); if ($this->Employee->save($this->data)) { $this->Session->setFlash('The Employee has been saved'); $this->redirect('/employees/index'); } else { $this->Session->setFlash('Please correct errors below.'); $this->set('groups', $this->Employee->Group->generateList()); } } } function delete($id = null) { if (!$id) { $this->Session->setFlash('Invalid id for Employee'); $this->redirect('/employees/index'); } if ($this->Employee->del($id)) { $this->Session->setFlash('The Employee deleted: id '.$id.''); $this->redirect('/employees/index'); } } } ?>
質問で聞いてきたとおり,基本的なメソッドを装備したコントローラが焼き上がっています。
できあがったビュー
ビューはコントローラの4つのメソッド(add,edit,view,index)に対応するビューができました。deleteのビューはありません。
indexメソッドのビュー index.thtmlを示します。
<div class="employees"> <h2>List Employees</h2> <table cellpadding="0" cellspacing="0"> <tr> <th>Id</th> <th>Group</th> <th>Employees Code</th> <th>Name</th> <th>Zip</th> <th>Address1</th> <th>Address2</th> <th>Phone</th> <th>Created</th> <th>Modified</th> <th>Actions</th> </tr> <?php foreach ($employees as $employee): ?> <tr> <td><?php echo $employee['Employee']['id']; ?></td> <td> <?php echo $html->link($employee['Group']['group_name'], '/groups/view/' .$employee['Group']['id'])?></td> <td><?php echo $employee['Employee']['employees_code']; ?></td> <td><?php echo $employee['Employee']['name']; ?></td> <td><?php echo $employee['Employee']['zip']; ?></td> <td><?php echo $employee['Employee']['address1']; ?></td> <td><?php echo $employee['Employee']['address2']; ?></td> <td><?php echo $employee['Employee']['phone']; ?></td> <td><?php echo $employee['Employee']['created']; ?></td> <td><?php echo $employee['Employee']['modified']; ?></td> <td class="actions"> <?php echo $html->link('View','/employees/view/' . $employee['Employee']['id'])?> <?php echo $html->link('Edit','/employees/edit/' . $employee['Employee']['id'])?> <?php echo $html->link('Delete','/employees/delete/' . $employee['Employee']['id'], null, 'Are you sure you want to delete id ' . $employee['Employee']['id'])?> </td> </tr> <?php endforeach; ?> </table> <ul class="actions"> <li><?php echo $html->link('New Employee', '/employees/add'); ?></li> </ul> </div>
一覧表のコードの書き方の見本になりますね。次にadd.thmlを見てみます。
<h2>New Employee</h2> <form action="<?php echo $html->url('/employees/add'); ?>" method="post"> <div class="optional"> <?php echo $form->labelTag('Employee/group_id', 'Group');?> <?php echo $html->selectTag('Employee/group_id', $groups, $html->tagValue('Employee/group_id'), array(), array(), true);?> <?php echo $html->tagErrorMsg('Employee/group_id', 'Please select the Group.') ?> </div> <div class="optional"> <?php echo $form->labelTag('Employee/employees_code', 'Employees Code');?> <?php echo $html->input('Employee/employees_code', array('size' => '60'));?> <?php echo $html->tagErrorMsg('Employee/employees_code', 'Please enter the Employees Code.');?> </div> (%u4E2D%u7565%uFF09 <div class="optional"> <?php echo $form->labelTag('Employee/phone', 'Phone');?> <?php echo $html->input('Employee/phone', array('size' => '60'));?> <?php echo $html->tagErrorMsg('Employee/phone', 'Please enter the Phone.');?> </div> <div class="submit"> <?php echo $html->submit('Add');?> </div> </form> <ul class="actions"> <li><?php echo $html->link('List Employees', '/employees/index')?></li> <li><?php echo $html->link('View Groups', '/groups/index/');?></li> <li><?php echo $html->link('Add Groups', '/groups/add/');?></li> </ul>
これは入力フォームの書き方の見本になります。
コメントする