Pear:: DB_Table Profile

    What is DB_Table »

DB_Table is a visit to the database Table OO interface, it provides a number of automatically create, insert, update and the method of choice. Construction is often automatically mean the loss of some flexibility, DB_Table is no exception.
Installation

pear procedures for the installation of no concern
pear install DB_Table
DB_Table fly

Official recommendation not to introduce the direct use of DB_Table Class, but it was extended, on top of the definition in this field, index, view, etc. other custom content. Below to see how the DB_Table expansion.

For example:
PLAIN TEXT
PHP:

    1.
       <? php
    2.
       class Guestbook extends DB_Table (
    3.
           / / Later added We'll add more here later in the tutorial,
    4.
           / / Note that no structural function but for now this is all we need.
    5.
       )
    6.
       «>

Then, an instance of Guestbook
PLAIN TEXT
PHP:

    1.
       <? php
    2.
       / / Must be the 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 can use to see if there is any fault
PLAIN TEXT
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.
       «>

Below see how detailed definition Guestbook category, this example of the definition of the various fields guestbook table type:
PLAIN TEXT
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.
       «>

To further define guestbook table index, this can be:
PLAIN TEXT
PHP:

    1.
       <? php
    2.
       class Guestbook extends DB_Table (
    3.
        
    4.
           / / Snip: var $ col = array (…); this part of the contents of that period and above, omitting
    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.
       «>

If the index and field names are the same, but not many fields index, the above definition can be simplified as follows:
PLAIN TEXT
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.
       «>

Definition of a good table structure, we look at how to carry out enquiries, looks very simple, no traces of sql statement:
PLAIN TEXT
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.
       «>

The conditions for:
PLAIN TEXT
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.
       «>

Insert data
PLAIN TEXT
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.
       «>

Data Update
PLAIN TEXT
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.
       «>

Data
PLAIN TEXT
PHP:

    1.
       <? php
    2.
      
    3.
       / / [Snip] create the Guestbook object ($ book)
    4.
      
    5.
       / / A where clause
    6.
       $ today = date ( 'Ym-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.
       «>

In the above example has not too many traces of the sql statements, which can be simplified to a certain extent, in the process of developing the sql statements prepared, but still sentence, automation to reduce the flexibility in the use of DB_Table need to weigh before .
Reference

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

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

Leave a Reply

One Response to “Pear:: DB_Table Profile”

  1. Comment by Loy75

    And of course they likewise are in denial that men landed on the moon. ,

Copyright © 2007 - Antivirus—Kaspersky key|kav7.0 key|kis7.0 key|kav8.0 key|kis8.0 key

  • Partner links