トランザクションの操作
以降では、DAO generator for PHP and MySQLを使用したトランザクションの作成方法を示します。DAO generator for PHP and MySQLを使用するには、example.phpスクリプトを作成し、それをgeneratedフォルダに入れます。generatedフォルダはすべてのDAOファイルを入れるフォルダです。この例のトランザクションには2つのSELECT SQLステートメントと1つのDELETE SQLステートメントが関係しています。
<?php
// Include all DAO files
require_once('include_dao.php');
$transaction = new Transaction();
//Clean table
//DAOFactory::getBooksDAO()->clean();
$transaction->rollback();
// Starting a new transaction
$transaction = new Transaction();
echo '****** Query All the books table ******'.'<br/>';
$arr = DAOFactory::getBooksDAO()->queryAll();
for($i=0;$i<count($arr);$i++){
$row = $arr[$i];
echo $row->id.' '.$row->title.' '.$row->author.' '.
$row->yearofpublication.' '.$row->publisher.' '.
$row->price.'<br/><br/>';
}
echo '****** Deleting the third row ******'.'<br/>';
$rowDeleted = DAOFactory::getBooksDAO()->delete(3);
echo 'rows deleted ='.$rowDeleted.'<br/><br/>';
echo '****** Loading the fifth row ******'.'<br/>';
$art = DAOFactory::getBooksDAO()->load(5);
echo 'Price for the fifth record book is = '.$art->price.'<br/><br/>';
echo '****** Printing all rows order by title ******'.'<br/>';
$article = DAOFactory::getBooksDAO()->queryAllOrderBy('title');
for($i=0;$i<count($article);$i++){
$row = $article[$i];
echo $row->id.' '.$row->title.' '.$row->author.' '.
$row->yearofpublication.' '.$row->publisher.' '.
$row->price.'<br/><br/>';
}
// Commit transaction
$transaction->commit();
?>
この例を実行すると、次の出力が生成されます。
****** Query All the books table ****** 1 Annabel Lee Edgar Allan Poe 1849 The Literature Page 256 2 The Ballad of Reading Gaol Oscar Wilde 1898 The Literature Page 45 3 The Sonnets Edgar Allan Poe 1602 The Literature Page 300 4 Winnetow Karl May 1956 The truth 123 5 JBoos Tools 3 Anghel Leonard 2009 Packt 569 ****** Deleting the third row ****** rows deleted =1 ****** Loading the fifth row ****** Price for the fifth record book is = 569 ****** Printing all rows order by title ****** 1 Annabel Lee Edgar Allan Poe 1849 The Literature Page 256 5 JBoos Tools 3 Anghel Leonard 2009 Packt 569 2 The Ballad of Reading Gaol Oscar Wilde 1898 The Literature Page 45 4 Winnetow Karl May 1956 The truth 123
新しいDAO関数の追加
ここでは、発行日付が1850年から2009年までの全レコードを出力する新しいDAO関数の作成方法を示します。BooksMySQLDAO.class.php内の既存のすべてのquery関数の後に、次の関数を追加します。
public function queryByYear(){
$sql = "SELECT * FROM books WHERE yearofpublication".
" BETWEEN '1850' AND '2009'";
$sqlQuery = new SqlQuery($sql);
return $this->getList($sqlQuery);
}
上記の関数を呼び出すコードをexample.phpページに追加してみましょう。次のコードを追加すると、yearofpublicationが1850~2009の範囲にあるレコードを出力できます。
echo "****** Printing all rows where yearofpublication is between.
'1850' and '2009'******".'<br/>';
$arr = DAOFactory::getBooksDAO()->queryByYear();
for($i=0;$i<count($arr);$i++){
$row = $arr[$i];
echo $row->id.' '.$row->title.' '.$row->author.' '.
$row->yearofpublication.' '.$row->publisher.' '
.$row->price.'<br/><br/>';
}
出力は次のようになります。
****** Printing all rows where yearofpublication is between '1850' AND '2009'****** 2 The Ballad of Reading Gaol Oscar Wilde 1898 The Literature Page 45 4 Winnetow Karl May 1956 The truth 123 5 JBoss Tools 3 Anghel Leonard 2009 Packt 569
ここまでDAOメカニズムの働きを見てきました。具体的には、ある特定のデータベース(bookstore)を使うDAO成果物を生成する、DAOジェネレータツールとそのファクトリクラスを使ってアプリケーションを作成する、bookstoreデータベースに対するトランザクションを作成する、生成した{databasename}MySqlDAOクラスに新しいDAO関数を追加する、といったものです。この機能はすべて手作業でコーディングできますが、このような反復的作業は自動化するほうがよいでしょう。自動化すれば、エラーが少なくなり、時間の節約にもなるからです。
