あまり書くとPHP 関西セミナーでありもっさんが話すことがなくなっちゃいますが、ちょこっとだけ。むしろ話の中でサラっと取り込んでいただけると幸いです。
DataObject のマニュアル の設定例では Builder 走らせたりとかあるのですが、私が使っている現在の状況としてはここまでやっていません。以下サンプル。mysql 想定。
(1) DataObject class
<?php
require_once('DB/DataObject.php');
class Example_DataObject extends DB_DataObject
{
function Example_DataObject()
{
$db_phptype = 'mysql';
$db_username = 'test_db';
$db_password = 'hogehoge';
$db_hostspec = 'example.com';
$db_database = 'test_db';
$dbDSN = {$db_phptype}://{$db_username}:{$db_password}@{$db_hostspec}/{$db_username};
// $this->debugLevel(5); // debug level
$this->_database_dsn = $dbDSN;
}
}
?>
(2) テーブルへの OR mapper class
<?php
require_once(CLASS_DIR . 'DataObject.php');
class Example_DataObject_Table1 extends Example_DataObject
{
function Example_DataObject_Table1()
{
parent::Example_DataObject();
$this->tableName('table1');
}
function table()
{
return array(
'id' => 1,
'foo' => 2,
'bar' => 2
);
}
/**
* table primary keys
* @return array
*/
function keys()
{
return array('id');
}
/**
* table sequence key
* @return array
*/
function sequenceKey()
{
return array('id', true, false);
}
}
?>
(3) 使用例
$doTable1 = new Example_DataObject_Table1();
$dbh = &$doTable1->getDatabaseConnection();
$doTable1->whereAdd('foo = ' .
$dbh->quote('foo')
);
if ($doTable1->find()) {
$doTable1->fetch();
$result = $doTable1->toArray();
}
;; SELECT id, foo, bar FROM table1 WHERE foo = 'foo';
$doTable1 = new Example_DataObject_Table1();
$doTable1->id = 1;
$doTable1->foo = 'foo';
$doTable1->bar = 'bar';
$doTable1->update();
;; UPDATE table1 SET foo = 'foo', bar = 'bar' WHERE id = 1;
こんな感じです。
もうちょい複雑に複数テーブルが入り混じるクエリ作るならちゃんと作らないといけませんが、1 table に select/insert/update するならばこんな簡単に導入可能。
さらに複雑なクエリ作るならば断然 QueryTool 。このあたりお任せします。期待してます。