Table of contents:
- rhaco
- モデル
- テンプレート
- ビュー
- プロジェクト
- セットアップ
- ユーティリティ
- データベース
- テスト
- arbo
- Tips
- ブログチュートリアル
- README
Updates:
データベースユーティリティ
データベース操作には,DbUtil を使用します.
データベース接続までの手順は次の通りです.
- project.xml にデータベース定義を記述.005-project/01-project.xmlを参照のこと.
- setup.php より,モデルを生成.
- setup.php より,接続情報の設定.
ここでは,次のデータベース定義を用います.
<project> <database name="sample"> <description>サンプル</description> <table name="profile"> <column name="id" type="serial" /> <column name="name" type="string" label="名前" /> <column name="age" type="integer" label="年齢" /> </table> </database> </project>
接続
setup.php にて行った接続情報は,__settings__.php に書き出され,Profile::connection() より取得できます.
<?php require_once dirname(__FILE__)."/__init__.php"; Rhaco::import("database.DbUtil"); Rhaco::import("model.Profile"); //データベースへ接続 $db = new DbUtil(Profile::connection());
操作
select
SELECT * FROM profile
<?php //: $profileList = $db->select(new Profile());
get
1行目だけを取得します
SELECT * FROM profile offset 0 LIMIT 1
<?php //: $profile = $db->get(new Profile());
insert
INSERT INTO profile (name,age) VALUES ('らこ',18)
<?php //: $profile = new Profile(); $profile->setName("らこ"); $profile->setAge(18); $db->insert($profile);
update
UPDATE profile SET name='らこ', age='18'
<?php //: $profile = new Profile(); $profile->setName("らこ"); $profile->setAge(18); $db->update($profile);
delete
DELETE FROM profile
<?php //: $db->delete(new Profile());
というわけで
ここでは,データベースへの接続と単純なSQL文の発行を示しました.
次は,WHERE句の発行方法を説明します.
API については,こちらをご覧ください.


