Pear::DB_Table简介

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明

DB_Table是什么?

DB_Table 是一个访问数据库Table的OO接口,它提供了一些自动创建、插入、更新和选择的方法。自动构建往往意味着要丧失一些灵活性,DB_Table也不能例外。

安装

pear的程序库的安装没什么悬念
pear install DB_Table

DB_Table上手

官方介绍上推荐不要直接使用DB_Table Class,而是对它进行扩展,在这之上再定义字段,索引,视图等等其它自定义内容。下面来看看如何对DB_Table扩展。

基本的例子:

PHP:
  1. <?php
  2. class Guestbook extends DB_Table {
  3.     // 稍后补充 We'll add more here later in the tutorial,
  4.     // 注意,没有构造函数 but for now this is all we need.
  5. }
  6. ?>

接着,实例化一个Guestbook

PHP:
  1. <?php
  2. // 必须的class
  3. require_once 'DB.php';
  4. require_once 'DB/Table.php';
  5. require_once 'Guestbook.php';
  6.  
  7. // create a PEAR DB object
  8. $dsn = "phptype://username:password@localhost/database";
  9. $db = DB::connect($dsn);
  10.  
  11. // set up for the Guestbook and create it, connecting
  12. // to a table called 'guestbook' (which does not exist
  13. // yet, we'll get to that in the next section)
  14. $book =& new Guestbook($db, 'guestbook'); // note the "=&" (very important!)
  15. ?>

可以用PEAR_Error来查看是否有错

PHP:
  1. <?php
  2. if ($book->error) {
  3.     // error handling code goes here; for example ...
  4.  
  5.     print_r($book->error);
  6.  
  7.     // ... although that's probably a bad idea as it will print
  8.     // your database username and password.
  9. }
  10. ?>

下面看看如何详细定义Guestbook类,这个例子里定义了guestbook表的各个字段类型:

PHP:
  1. <?php
  2. class Guestbook extends DB_Table {
  3.     var $col = array(
  4.        
  5.         // unique row ID
  6.         'id' => array(
  7.             'type'    => 'integer',
  8.             'require' => true
  9.         ),
  10.        
  11.         // first name
  12.         'fname' => array(
  13.             'type'    => 'varchar',
  14.             'size'    => 32
  15.         ),
  16.        
  17.         // last name
  18.         'lname' => array(
  19.             'type'    => 'varchar',
  20.             'size'    => 64
  21.         ),
  22.        
  23.         // email address
  24.         'email' => array(
  25.             'type'    => 'varchar',
  26.             'size'    => 128,
  27.             'require' => true
  28.         ),
  29.        
  30.         // date-time signed
  31.         'signdate' => array(
  32.             'type'    => 'date',
  33.             'require' => true
  34.         )
  35.     );
  36. ?>

如果要进一步定义guestbook表的索引,可以这样进行:

PHP:
  1. <?php
  2. class Guestbook extends DB_Table {
  3.    
  4.     // snip: var $col = array(...);  这部分内容和上面那段一样,省略
  5.    
  6.     var $idx = array(
  7.         'id' => array(
  8.             'type' => 'unique',
  9.             'cols' => 'id'
  10.         ),
  11.         'signdate' => array(
  12.             'type' => 'normal',
  13.             'cols' => 'signdate'
  14.         )
  15.     );
  16. }
  17. ?>

如果索引和字段名是一样的,而且不是多字段索引,上面的定义可以简化为:

PHP:
  1. <?php
  2.     var $idx = array(
  3.         // unique index called 'id' based on the 'id' column
  4.         'id' => 'unique',
  5.        
  6.         // normal index called 'signdate' based on the 'signdate' column
  7.         'signdate' => 'normal'
  8.     );
  9. ?>

定义好表结构之后,我们看看如何进行查询,看上去非常简单,没有sql语句的痕迹:

PHP:
  1. <?php
  2. // [snip] create the $book Guestbook object
  3.  
  4. // get the 'list' view as an array
  5. $rows = $book->select('list');
  6. print_r($rows);
  7.  
  8. // get the 'emails' view as a DB_Result object
  9. $result = $book->selectResult('emails');
  10. print_r($result);
  11.  
  12. ?>

按条件查询:

PHP:
  1. <?php
  2.  
  3. // [snip] create the $book Guestbook object
  4.  
  5. // get 'list' view rows signed on August 14, 2003, ordered ascending by
  6. // last name and first name, starting at row 7 and getting 12 rows total.
  7.  
  8. $view = 'list';
  9. $filter = "signdate = '2003-08-14'";
  10. $order = 'lname, fname';
  11. $start = 7;
  12. $count = 12;
  13.  
  14. // as an array
  15. $rows = $book->select($view, $filter, $order, $start, $count);
  16. print_r($rows);
  17.  
  18. // as a DB_Result object
  19. $rows = $book->selectResult($view, $filter, $order, $start, $count);
  20. print_r($result);
  21.  
  22. ?>

数据插入

PHP:
  1. <?php
  2.  
  3. // [snip] create the Guestbook object ($book)
  4.  
  5. // assign the fields and values
  6. $cols_vals = array(
  7.     'id'       => 1,
  8.     'fname'    => 'Thomas',
  9.     'lname'    => 'Anderson',
  10.     'signdate' => '2003-10-12',
  11.     'email'    => 'neo@matrix.net'
  12. );
  13.  
  14. // insert into the table and print results
  15. $result = $book->insert($cols_vals);
  16. print_r($result);
  17.  
  18. ?>

数据更新

PHP:
  1. <?php
  2.  
  3. // [snip] create the Guestbook object ($book)
  4.  
  5. // assign the updated fields and values
  6. $cols_vals = array(
  7.     'lname' => 'Jones'
  8. );
  9.  
  10. // assign the WHERE clause
  11. $where = "lname = 'Smith'";
  12.  
  13. // attempt the update and print the results
  14. $result = $book->update($cols_vals, $where);
  15. print_r($result);
  16.  
  17. ?>

数据删除

PHP:
  1. <?php
  2.  
  3. // [snip] create the Guestbook object ($book)
  4.  
  5. // a where clause
  6. $today = date('Y-m-d'); // formatted as yyyy-mm-dd
  7. $where = "signdate <'$today'";
  8.  
  9. // attempt the delete and print the results
  10. $result = $book->delete($where);
  11. print_r($result);
  12.  
  13. ?>

在上面的例子中已经没有太多sql语句的痕迹了,这可以在一定程度上简化开发过程中sql语句的编写,但是还是那句话,自动化的同时降低了灵活性,在使用DB_Table之前需要权衡。

参考

http://wiki.ciaweb.net/yawiki/index.php?area=DB_Table&page=HomePage

下载源码

http://pear.php.net/package/DB_Table

作者: Volcano 发表于August 27, 2006 at 10:43 am

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

Tags: ,

2 条评论 »

  1. Yimin 于 2006-08-27 @ 13:59:33 留言

    要定义字段类型也是比较麻烦的,要是直接从数据库里映射出来,那就更好了。

  2. volcano 于 2006-08-27 @ 14:57:15 留言

    我觉得自动映射字段类型,应该是能够自己扩展一下的。记得以前看过类似的程序,可惜现在一时找不着了

RSS 为此帖反馈评论 · 反向跟踪 网站

留条评论