edo1z blog

プログラミングなどに関するブログです

cakePHP2.2 Bakeを研究してみる

私の環境は、mac、Eclipse、XAMPP、PHP5.3、cakePHP2.2.5、MySQLです。

Bakeとはデータベースのテーブルさえ作っておけば、瞬時にcakePHPのモデルとビューとコントローラーを自動で作ってくれる機能のことであります。 便利なんじゃないでしょうか。ベースとなる部分って基本的に固定された内容を毎回作成するわけですから、そのベースを多少修正するのは当たり前としても前段となる作業の大部分を自動化できるっていうのは大変有り難いことです。加えて、cakePHPのcookbookというリファレンスは、Bakeを使う前提で解説されている箇所も結構多そうなので、とりあえずBakeはマスターしておこうと思いました。

さて、Bakeの解説はここに載っていますので、こちらを見ながら試していきたいと思います。

下準備(プロジェクト作成〜データベース設定)

すんなりいくのか不安ですが、まずはEcliplse上でcakePHPのプロジェクトを作成すると共に、MySQLのデータベースを作成し、テーブルも作成します。 そして、cakePHPとデータベースを連携させる為の設定を実施します。ここまでのやり方詳細は、こちらに記載しましたので分からない場合はご確認ください。

さて、今回はbaketestという名前でプロジェクトを作成しました。フォルダは /Applications/XAMPP/xamppfiles/htdocs/baketest にあります。 データベースは、baketestという名前でこれまた作成しました。テーブルは下記2点を作成しました。

■ postsテーブル

CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

■ usersテーブル

CREATE TABLE users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50),
    password VARCHAR(50),
    role VARCHAR(20),
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);


はい、そして、cakePHPのデータベース連携関連を設定するのは、/baketest/app/Config/database.php.default になりますので、こちらの設定を下記のようにしました。(.defaultは削除します)

public $default = array(
 'datasource' => 'Database/Mysql',
 'persistent' => false,
 'host' => 'localhost',
 'login' => 'root',
 'password' => '',
 'database' => 'baketest',
 'prefix' => '',
 //'encoding' => 'utf8',
);


これで準備が完了しましたので、いざBakeをしてみたいと思います。

Bake!!!

Bakeはどこにいるのか?

どこにいるんだい?Bakeは、cake.phpのコマンドのようだ。当然今回だと、baketestの中のどこかにあるはずだ。探してみるとどうも、下記2つの場所に存在しているようだ。

  1. /baketest/app/Console/cake.php
  2. /baketest/lib/Cake/Console/cake.php

とりあえず順番に実行してみたらいいか。

Bakeしてみる。

まずは、上の1番目からターミナル上で実行してみましょう。
ターミナルを起動して、下記を打ち込みましょう。

cd /Applications/XAMPP/xamppfiles/htdocs/baketest/app/Console/
php cake.php bake


すると、下記画面のようにしっかり起動されました。1番目でよかったみたいですね。



なんだか上の方にワーニングが沢山でているので、後で確認しましょう。
とりあえず、Bakeを続けてみたいと思います。

一旦現状表示されているの画面をテキストベースで記載します。

Welcome to CakePHP v2.2.5 Console
---------------------------------------------------------------
App : app
Path: /Applications/XAMPP/xamppfiles/htdocs/baketest/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)


うむ、なんか一つずつ作る感じなんでしょうか。全部一気に作成して欲しいものですが。。
さて、そこで、このページに良さそうなことが書いてありました。

新しいbakeコマンド

bakeをより早く、より簡単にするために新しいコマンドが追加されました。 コントローラ、モデル、ビューについて、すべての機能をbakeする all というサブコマンドです。 これは、一度にすべての素材を作ってくれて、また再作成も簡単に出来ます。

cake bake model all

このコマンドは一回ですべてのモデルをbakeします。 同じように、 cake bake controller all はすべてのコントローラーを、cake bake view all はすべてのビューファイルを生成します。 ControllerTask 上のパラメータも同様に変更されます。 cake bake controller scaffold は cake bake controller public になりました。 ViewTask は -admin フラグが追加され、このフラグを使うとRouting.admin を使ったビューのアクションをbakeできます。

ということなので、Qを押して一旦終了して、上記コマンドを打ち込んでみましょう。

(ToT)/ エラーになりました。

$ php cake.php bake model all

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Cache/CacheEngine.php on line 59

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Cache/CacheEngine.php on line 59

Welcome to CakePHP v2.2.5 Console
---------------------------------------------------------------
App : app
Path: /Applications/XAMPP/xamppfiles/htdocs/baketest/app/
---------------------------------------------------------------
Warning Error: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in [/Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Model/Datasource/Database/Mysql.php, line 149]

Error: Database connection "Mysql" is missing, or could not be created.
#0 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Model/Datasource/DboSource.php(261): Mysql->connect()
#1 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Model/ConnectionManager.php(101): DboSource->__construct(Array)
#2 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Command/Task/ModelTask.php(908): ConnectionManager::getDataSource('default')
#3 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Command/Task/ModelTask.php(845): ModelTask->getAllTables('default')
#4 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Command/Task/ModelTask.php(122): ModelTask->listAll('default', false)
#5 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Command/Task/ModelTask.php(101): ModelTask->all()
#6 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Shell.php(392): ModelTask->execute()
#7 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Shell.php(389): Shell->runCommand('execute', Array)
#8 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/ShellDispatcher.php(201): Shell->runCommand('model', Array)
#9 /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/ShellDispatcher.php(69): ShellDispatcher->dispatch()
#10 /Applications/XAMPP/xamppfiles/htdocs/baketest/app/Console/cake.php(33): ShellDispatcher::run(Array)
#11 {main}


さて、この問題は、/baketest/app/Config/database.phpの、'host' => 'localhost'を、'host' => '127.0.0.1'に変更することで解決できました。
教えてくれたのはこのページです。謎ですね。。

改めて実行すると、エラーなく下記メッセージが流れました。

$ php cake.php bake model all

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Cache/CacheEngine.php on line 59

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in /Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Cache/CacheEngine.php on line 59

Welcome to CakePHP v2.2.5 Console
---------------------------------------------------------------
App : app
Path: /Applications/XAMPP/xamppfiles/htdocs/baketest/app/
---------------------------------------------------------------

You can download PHPUnit from http://phpunit.de
Baking Post

Baking model class for Post...

Creating file /Applications/XAMPP/xamppfiles/htdocs/baketest/app/Model/Post.php
Wrote `/Applications/XAMPP/xamppfiles/htdocs/baketest/app/Model/Post.php`
Warning Error: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in [/Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Command/Task/FixtureTask.php, line 325]

Warning Error: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in [/Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Command/Task/FixtureTask.php, line 325]


Baking test fixture for Post...

Creating file /Applications/XAMPP/xamppfiles/htdocs/baketest/app/Test/Fixture/PostFixture.php
Wrote `/Applications/XAMPP/xamppfiles/htdocs/baketest/app/Test/Fixture/PostFixture.php`
Bake is detecting possible fixtures...

Baking test case for Post Model ...

Creating file /Applications/XAMPP/xamppfiles/htdocs/baketest/app/Test/Case/Model/PostTest.php
Wrote `/Applications/XAMPP/xamppfiles/htdocs/baketest/app/Test/Case/Model/PostTest.php`
Baking User

Baking model class for User...

Creating file /Applications/XAMPP/xamppfiles/htdocs/baketest/app/Model/User.php
Wrote `/Applications/XAMPP/xamppfiles/htdocs/baketest/app/Model/User.php`
Warning Error: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in [/Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Command/Task/FixtureTask.php, line 325]

Warning Error: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in [/Applications/XAMPP/xamppfiles/htdocs/baketest/lib/Cake/Console/Command/Task/FixtureTask.php, line 325]


Baking test fixture for User...

Creating file /Applications/XAMPP/xamppfiles/htdocs/baketest/app/Test/Fixture/UserFixture.php
Wrote `/Applications/XAMPP/xamppfiles/htdocs/baketest/app/Test/Fixture/UserFixture.php`
Bake is detecting possible fixtures...

Baking test case for User Model ...

Creating file /Applications/XAMPP/xamppfiles/htdocs/baketest/app/Test/Case/Model/UserTest.php
Wrote `/Applications/XAMPP/xamppfiles/htdocs/baketest/app/Test/Case/Model/UserTest.php`


うむ、きちんと作られているようです。間のwarningがうるさいですが、タイムゾーン設定関連のようなので後で直しておきましょう。
残りのコントローラーとビューも下記コマンドを実行して作成しましょう。

$ php cake.php bake controller all
$ php cake.php bake view all


問題なく実行されました。(veiwから作ろうと思ったらcontrollerから作れと怒られましたが)

さて、それでは実際どのようなファイルが作られているのか確認してみましょう。

Bakeの結果・・・

すばらしいですね。沢山のファイルが全てきっちり作成されていました。内容を全てここに表示するのは難しいので、実際にbaketestを起動した画面イメージを掲載したいと思います。