5月 26
03.データベース接続
データベースに接続するための設定を行います。
設定ファイルは /app/config/database.php.default を書き換えて、/app/config/database.php にリネームするだけなので、エディターで開いて編集すればいいのですが、ここでは console の cake コマンドを使ってやってみます。
大元の /cake/console の中にあるのがcakeコマンドです。
自分のhomeディレクトリから、
cd Sites/cake/cake/console ./cake
と打てば
Welcome to CakePHP v1.2.3.8166 Console
—————————————————————
Current Paths:
-app: app
-working: /Users/bob/Sites/cake/app
-root: /Users/bob/Sites/cake
-core: /Users/bob/Sites/cakeChanging Paths:
your working path should be the same as your application path
to change your path use the ‘-app’ param.
Example: -app relative/path/to/myapp or -app /absolute/path/to/myappAvailable Shells:
APP/vendors/shells:
– noneROOT/vendors/shells:
– noneCORE/console/libs:
acl
api
bake
console
i18n
schema
testsuiteTo run a command, type ‘cake shell_name [args]‘
To get help on a specific command, type ‘cake shell_name help’
とりあえず動くのが分かります。
で、一応このcakeコマンドにパスを通しておきます。
vi .profile
としてviエディターを起動し、
export PATH=/Users/bob/Sites/cake/cake/console:$PATH
の一行を追加します。
ターミナルに戻って、
. .profile
して、
printenv PATH
で、パスを確認します。
/Users/bob/Sites/cake/cake/console:
と入っていればOK.
基本、自分の app フォルダーの中で作業するのがいいです。
cd Sites/cake/app cake bake
別の場所にいる場合は、 app の場所を指定します。今自分がhomeにいるんだったら、
cake -app Sites/cake/app bake
すると、
Welcome to CakePHP v1.2.3.8166 Console
—————————————————————
App : app
Path: /Users/bob/Sites/cake/app
—————————————————————
Your database configuration was not found. Take a moment to create one.
—————————————————————
Database Configuration:
—————————————————————
Name:
[default] >
というように対話式の設定画面になりますので、必要な項目を入力します。
—————————————————————
The following database configuration will be created:
—————————————————————
Name: default
Driver: mysql
Persistent: false
Host: localhost
User: cake_user
Pass: *********
Database: cake_db
Encoding: utf8
—————————————————————
という感じに、データベースの設定を入れて[y]すると、自動で /Users/bob/Sites/cake/app/config/database.php
が作られます。
ブラウザで http://localhost/~bob/cake/ を開いてみると
Your database configuration file is present.
Cake is able to connect to the database.
と出てるはずです。
