技术开发 频道

将DAO Generator应用于PHP和MySQL

  四、使用事务

  本节展示如何使用PHP和MySQL的DAO Generator来创建一个事务。这里将要使用PHP和MySQL的DAO Generator来创建example.php脚本并将其放到generated文件夹中,其中generated文件夹中存放了所有的DAO文件。在本例中,我们的事务将生成两个SQL语句,即SELECT和DELETE语句:

<?php
//包含所有的DAO文件
require_once('include_dao.php');
$transaction
= new Transaction();
//将表清空
//DAOFactory::getBooksDAO()->clean();
$transaction->rollback();
//启动一个新的事务
$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/>';
}
//提交事务
$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函数

  本节展示如何创建一个新的DAO函数,该函数用于打印出版日期介于1850到2009之间的所有记录。将其添加到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 generator工具及其工厂类创建应用程序的过程,创建一个数据库bookstore的事务过程,以及为生成的{databasename}MySqlDAO 类添加新的DAO函数的方法。当然,我们可以手工方式编程实现所有这些功能,但是最好将这些重复性的工作自动化,因为这样不仅能够降低出错率,而且还能节约宝贵的时间。


0
相关文章