DB_Table_Database Class Tutorial
Описание
DB_Table_Database is an database abstraction class for a relational database. It is a layer built on top of the DB_Table class: Each table in a DB_Table_Database object is represented by a DB_Table object. The most important difference between a DB_Table_Database object and a collection of DB_Table objects is that the properties of a parent DB_Table_Database object contain a model of the entire database, including relationships between tables.
DB_Table_Database provides:
An object-oriented representation of a relations between tables in a relational database, including linking/association tables that create many-to-many relationships.
A simplified API for INSERT, UPDATE, DELETE, and SELECT commands, with an interface very similar to that of the DB_Table class.
Automated construction of join conditions for inner joins of any number of tables, based on a list of table names and/or a list of desired column names.
Optional checking of the validity of foreign key values by the PHP layer upon insertion or updating.
Optional PHP emulation of SQL ON DELETE and ON UPDATE referentially triggered actions.
PHP serialization to (and unserialization from) a string that contains the entire database schema.
Serialization to (and unserialization) from XML, using an extension of the MDB2 XML schema. (Unserialization from XML requires PHP 5).
Methods to set various properties of all the child DB_Table objects to a common value.
Various utility methods that aid the construction of SQL queries.
Like DB_Table, DB_Table_Database wraps a DB or MDB2 database connection object. The class is compatible with both PHP 4 and PHP 5, with the exception of one non-essential method: The fromXML() method, which creates a DB_Table_Database object from an XML database schema, requires PHP 5.
Class DB_Table_Database extends abstract base class DB_Table_Base. Methods or properties that are inherited from DB_Table_Base are noted as such, and are indicated in the table of contents of this page with the notation "(from DB_Table_Base)".
This tutorial uses an extended example to introduce the use of the DB_Table_Database class to create a model of an interface to a relational database.
Contents
addTable() - Adding tables
addRef() - Adding references
addLink() - Declaring linking tables
select(), selectResult(), and selectCount() (from DB_Table_Base)
buildFilter() (from DB_Table_Base)
buildSQL() (from DB_Table_Base)
Example Database
Throughout this tutorial, our examples will refer to a DB_Table_Database object for an example database named TestDB, which is described below. The child DB_Table objects that are associated with RDBMS tables must all be instantiated first, and then added to (i.e., linked with) a parent DB_Table_Database object.
The example database TestDB stores names, numbers, and addresses for a set of people, and contains 4 tables. Peoples names, phone numbers, and addresses are stored in three tables named Person, Phone, and Address, respectively. To allow for the fact that several people may share a phone number, and that a person may have more than one phone number, the database allows the creation of a many-to-many relationship between Person and Phone. This relationships are established by an additional linking table, named PersonPhone, which contains foreign key references to Person and Phone.
DB_Table objects must be instantiated before they can be added to a parent DB_Table_Database instance. The usual way of creating a DB_Table object (as discussed in the tutorial for that class) is to create one subclass of DB_Table for each table, and create one instance of each such subclass. In this tutorial, we use a convention in which the subclass of DB_Table associated with a database table named "Entity" is Entity_Table, and in which the single object of this class is $Entity. This is also the convention used in code generated by the DB_Table_Generator class.
The following code defines a subclass Person_Table that represents a database table Person:
<?php require_once 'DB/Table.php' class Person_Table extends DB_Table { // Define columns $col = array ( 'PersonID' => array('type' => 'integer', 'require' => true), 'FirstName' => array('type' => 'char', 'size' => 32, 'require' => true), 'MiddleName' => array('type' => 'char', 'size' => 32), 'LastName' => array('type' => 'char', 'size' => 64, 'require' => true), 'NameSuffix' => array('type' => 'char', 'size' => 16), 'AddressID' => array('type' => 'integer') ); // Define indices. PersonID is declared to be the primary index $idx = array( 'PersonID' => array('cols' => 'PersonID', 'type' => 'primary'), 'AddressID' => array('cols' => 'AddressID', 'type' => 'normal') ); // Declare 'PersonID' to be an auto-increment column $auto_inc_col = 'PersonID'; } ?> |
Note the assignment of a value for the $auto_inc_col property, which is a recent addition to DB_Table: The value of $auto_inc_col is the name of a column that is declared to be 'auto increment'. Auto incrementing of this column is now implemented in the insert method of DB_Table using DB or MDB2 sequences.
The following code uses the same method to create subclasses of DB_Table associated with the remaining Phone, Address, and PersonPhone tables of database TestDB:
<?php class Address_Table extends DB_Table { $col = array( 'AddressID' => array('type' => 'integer', 'require' => true), 'Building' => array('type' => 'char', 'size' =>16), 'Street' => array('type' => 'char', 'size' => 64), 'UnitType' => array('type' => 'char', 'size' => 16), 'Unit' => array('type' => 'char', 'size' => 16), 'City' => array('type' => 'char', 'size' => 64), 'StateAbb' => array('type' => 'char', 'size' => 2), 'ZipCode' => array('type' => 'char', 'size' => 16) ); $idx = array( 'AddressID' => array('cols' => 'AddressID', 'type' => 'primary'), ); $auto_inc_col = 'AddressID'; } ?> |
<?php class Phone_Table extends DB_Table { $col = array( 'PhoneID' => array('type' => 'integer', 'require' => true), 'PhoneNumber' => array('type' => 'char', 'size' => 16, 'require' => true), 'PhoneType' => array('type' => 'char', 'size' => 4) ); $idx = array( 'PhoneID' => array('cols' => 'PhoneID', 'type' => 'primary') ); $auto_inc_col = 'PhoneID'; } ?> |
<?php class PersonPhone_Table extends DB_Table { $col = array( 'PersonID' => array('type' => 'integer', 'require' => true), 'PhoneID' => array('type' => 'integer', 'require' => true) ); $idx = array( 'PersonID' => array('cols' => 'PersonID', 'type' => 'normal'), 'PhoneID' => array('cols' => 'PhoneID', 'type' => 'normal') ); } ?> |
The following code instantiates one object of each DB_Table subclass, which is associated with the corresponding table:
<?php $Person = new Person_Table($conn, 'Person', 'safe'); $Address = new Address_Table($conn, 'Address', 'safe'); $Phone = new Phone_Table($conn, 'Phone', 'safe'); $PersonPhone = new PersonPhone_Table($conn, 'PersonPhone', 'safe'); ?> |
It is recommended that constructor statements be placed in a separate file from any of the DB_Table subclass definitions. Doing so makes it easier to serialize and unserialize the DB_Table and DB_Table_Database objects, because a php file in which an instance of a DB_Table subclass is unserialized must have access to the subclass definition, but should not include the constructor statements. Putting each DB_Table subclass definition in a separate file, with a name that is the subclass name with a .php extension, also allows the subclass definitions to be autoloaded when an object is serialized, as discussed below.
An alternative way to create a DB_Table object is to create an instance of DB_Table itself, rather than of a subclass of DB_Table. In this method, one first instantiates a generic DB_Table object, which initially contains no information about the table schema, and then sets the values of the public $col and $idx properties needed to define a table schema. As an example, the following code constructs an instance of DB_Table that represents the Person table:
<?php $Person = new DB_Table($conn, 'Person'); $Person->col['PersonID'] = array('type' => 'integer', 'require' => true); $Person->col['FirstName'] = array('type' => 'char', 'size' => 32, 'require' => true); $Person->col['MiddleName'] = array('type' => 'char', 'size' => 32); $Person->col['LastName'] = array('type' => 'char', 'size' => 64, 'require' => true); $Person->col['NameSuffix'] = array('type' => 'char', 'size' => 16); $Person->col['AddressID'] = array('type' => 'integer'); $Person->idx['PersonID'] = array('cols' => 'PersonID', 'type' => 'primary'); $Person->idx['PersonID'] = array('cols' => 'PersonID', 'type' => 'normal'); $Person->auto_inc_col = 'PersonID'; ?> |
Constructor
A DB_Table_Database object is instantiated as an empty shell, to which tables, foreign key references, and links are then added. The constructor interface is
void DB_Table_Database(DB/MDB2 object $conn, string $name) |
<?php require_once 'DB/Table/Database.php' $conn = DB::connect("mysqli://$user:$password@$host"); $db = new DB_Table_Database($conn, 'TestDB'); ?> |
Building a Model
To construct a model of a relational database, after instantiating a set of DB_Table objects and a DB_Table_Database object, we must add the table objects to the database object, add declarations of foreign key references, and declare many-to-many relationships that involve linking tables, in that order.
Adding Tables
After a DB_Table object is instantiated, it can be added to the parent database with the DB_Table_Database::addTable() method. The interface for this method is
true|PEAR_Error addTable(object &$Table) |
The following code adds the four tables of our example database to the $db DB_Table_Database object:
<?php $db->addTable($Person); $db->addTable($Address); $db->addTable($Phone); $db->addTable($PersonPhone); ?> |
Adding Foreign Key References
After tables have been added to a database, we can use the addRef() method to add references between pairs of tables.
Synopsis (simplified):
true|PEAR_Error addRef(string $ftable, string|array $fkey, string $rtable, [string|array $rkey] ) |
<?php $db->addRef('Person', 'AddressID', 'Address', 'AddressID'); ?> |
<?php $db->addRef('Person', 'AddressID', 'Address'); ?> |
A reference between two tables can only be added after both the referencing and referenced DB_Table objects have been instantiated and added to the parent DB_Table_Datbase instance.
Adding Links
A table may be declared to be a "linking" table that establishes a many-to-may relationship between two others. The only effect of such a declaration is to change the action of the autoJoin method: If a table named $link is declared to be a linking table that creates a many-to-many relationship between tables named $table1 and $table2, then the autoJoin method may use the linking table to join $table1 and $table2, if necessary.
Method addLink() declares a table to be a linking or association table for two others.
Synopsis:
true|PEAR_Error addLink(string $table1, string $table2, string $link) |
For example, the command:
<?php $db->addLink('Person', 'Phone, 'PersonPhone') ?> |
The addLink() method will not prevent one from declarating more than one linking table for the same two linked tables. Doing so would make the link declaration useless, however, because the autoJoin() method will fail if it needs to use a linking table to join a pair of tables, but finds that more than one of linking table is declared for those two tables.
The command
<?php $db->addAllLinks() ?> |
The database model is complete when all of the tables have been added, all of the references have been added, and all the links have been added.
Example - Putting it Together
For our example, let us create a directory in which to put all of the code required as an interface to a database. We will put each DB_Table subclass definition in a separate file in this directory, in which each file name is simply the class name with a '.php' extension. In addition, it is convenient to create a single file, which we will call 'Database.php', in which we create a DB or MDB2 connection, create one object per table, and construct a parent DB_Table_Database object. This file structure is used by the DB_Table_Generator class for code that is auto-generated for an existing database. Below is a listing of the minimal 'Database.php' file required for our example database:
Пример 35-1. Database.php File
|
This example file is very similar to the skeleton file that would be created by DB_Table_Generator for an existing database with this structure. The main differences are that some lines in the auto-generated file would have to be uncommented or edited to produce the above (e.g., the lines that define the database DSN). In this example, the call to addAllLinks() method would correctly identify 'PersonPhoneAssoc' as a table that links 'Person' and 'Phone'. This example does not include any referentially triggered 'ON DELETE' or 'ON UPDATE' actions, discussed below, which could be added to the end of the same file.
Deleting Tables, References, and Links
The deleteTable(), deleteRef(), and deleteLinks() methods can be used to delete tables, and foreign key references, and linking table declarations, respectively, from the DB_Table_Database model.
deleteTable() - deletes a table from the database model
Synopsis:
void deleteTable(string $table) |
deleteRef() - deletes a reference from the database model
Synopsis:
void deleteRef(string $ftable, string $rtable) |
deleteLink() - deletes a linking table declaration
Synopsis:
void deleteLink(string $table1, string $table2, [string $link]) |
On Delete and On Update Actions
DB_Table_Database optionally provides actions designed to enforce referential integrity that are provided by ANSI SQL, but that are not provided by some popular databases (e.g., SQLite and the default MySQL engine). DB_Table_Database offers optional PHP emulation of referentially triggered ON DELETE and ON UPDATE actions, such as cascading deletes (discussed here), and also optionally checks the validity of foreign key values before insertion or updating (discussed below)
The ON DELETE and ON UPDATE actions associated with a reference (if any) may be declared either as additional parameters to addRef(), or by using setOnDelete() and setOnUpdate().
Declaring actions in addRef()
Actions to be taken on deletion or updating of a referenced row may may be declared when a reference is added to the model using two optional parameters of the addRef() method. The following example shows the extended form of addRef() needed to add a reference from PersonPhone to Person (as above), while also declaring a cascade action on delete of a referenced row of Person, and a restrict action on update of such a row:
<?php $db->addRef('PersonPhone', 'PersonID', 'Person', null, 'cascade', 'restrict'); ?> |
The effect of the 'cascade' values of the fifth parameter in the above example is to declare that that all referencing rows of PersonPhone should be deleted upon deletion of a corresponding referenced row of Person (a cascading delete). The 'restrict' value of the sixth parameter declares that updating of the primary key PersonID of Person should be prevented (the 'restrict' on update action), and an error should be thrown by the update method, in rows of Person that are referenced by rows of PersonPhone.
The full interface of the addRef() method is:
true|PEAR_Error addRef(string $ftable, mixed $fkey, string $rtable, [mixed $rkey], [string|null $on_delete], [string|null $on_update]) |
'cascade' | 'restrict' | 'set null' | 'set default' |
The following example declares all of the foreign key references needed in our example database, with appropriate referentially triggered actions:
<?php $db->addRef('Person', 'AddressID', 'Address', null, 'set null', 'cascade'); $db->addRef('PersonPhone', 'PersonID', 'Person', null, 'cascade', 'cascade'); $db->addRef('PersonPhone', 'PhoneID', 'Phone', null, 'cascade', 'cascade'); ?> |
setOnDelete() and setOnUpdate()
The referentially triggered actions associated with a foreign key reference may also be changed, or turned off, with the setOnDelete() and setOnUpdate() methods.
Synopses:
void setOnDelete(string $ftable, string $rtable, string|null $action) void setOnUpdate(string $ftable, string $rtable, string|null $action) |
For example, the following code would change the 'on_update' action associated with the the reference from 'PersonPhone' to 'Person' to a 'restrict' action:
<?php $db->setOnUpdate('PersonPhone', 'Person', 'restrict'); ?> |
setActOnDelete() and setActOnUpdate()
PHP emulation of referentially triggered actions may be turned on or off for the entire database by the setActOnDelete() and setActOnUpdate() methods.
Synopses:
void setActOnDelete(bool $flag) void setActOnUpdate(bool $flag) |
Foreign Key Validation
By default, DB_Table_Database checks the validity of foreign key values before inserting or updating data in a table with foreign keys. That is, before inserting a row, or updating any foreign key column values, the insert() and update() methods of DB_Table_Database actually submit a query to confirm that the inserted or updated foreign key column values correspond to values of the referenced columns of an existing row in the referenced table. By default, both methods throw an error, and do not modify the data, if this check fails.
This checking of foreign key validity by the PHP layer may be turned on or off, for insertion or updating of any table in database, with the setCheckFKey() method. The interface of this method is:
void setCheckFKey(bool $flag) |
Data Selection
DB_Table_Database provides an object-oriented interface for SQL select statements that is almost identical to that of DB_Table.
Query Arrays
As in DB_Table, queries are represented in DB_Table_Database as arrays, in which array elements represents clauses of a corresponding SQL select statement. For example, a query for names of all people that live on Oak Street in Anytown in our example database might be
<?php $oak = array( 'select' => 'Person.FirstName, Person.LastName, Address.Building', 'from' => 'Person, Address', 'where' => "Person.AddressID = Address.AddressID\n" . " AND Address.Street = 'Oak Street'\n" . " AND Address.City = 'AnyTown'", 'order' => 'Address.Building' ); ?> |
<?php echo $db->buildSQL($oak); ?> |
SELECT Person.FirstName, Person.LastName, Address.Building FROM Person, Address WHERE Person.AddressID = Address.AddressID AND Address.Street = 'Oak Street' AND Address.City = 'AnyTown' ORDER BY Address.Building |
As in DB_Table, such query arrays can be stored in the public $sql property array:
<?php $db->sql['oak'] = $oak ?> |
Select* Methods: select(), selectResult(), and selectCount()
The select*() methods are inherited by both the DB_Table_Database and DB_Table classes from the DB_Table_Base class, and thus share the same interface and behavior. The interface is also the same for all three methods. The required first parameter can be either the key for a previously stored query array, as in
<?php $result = $db->select('oak') ?> |
<?php $result = $db->select($oak) ?> |
The common interface of three select* methods select(), selectCount(), and selectResult() is:
mixed select*( array|string $sql_key, [string $filter], [string $order], [int $start], [int $count], [array $params]) |
autoJoin()
autoJoin() - accepts an array paramater containing the names of desired columns and/or an array of tables names, and returns a query array containing a WHERE clause with automatically generated join conditions.
Synopsis:
array|PEAR_Error autoJoin([array $cols], [array $tables], [string $filter]) |
The following example generates and submits query that selects a result set in which each row contains a person's name, home phone number and address, based on knowledge of the names of the desired columns. In our example database, this requires that all four tables be joined.
<?php $cols = array('FirstName', 'LastName', 'PhoneNumber', 'Building', 'Street', 'City') $report = $db->autoJoin($cols, "Phone.PhoneType = 'HOME'"); $result = $db->select($report); ?> |
The SQL command corresponding to such a query array may be obtained using buildSQL(). In this example,the command
<?php echo $db->buildSQL($report); ?> |
SELECT Person.FirstName, Person.LastName, Phone.PhoneNumber, Address.Building, Address.Street, Address.City FROM Person, Phone, Address, PersonPhone WHERE PersonPhone.PhoneID = Phone.PhoneID AND PersonPhone.PersonID = Person.PersonID AND Person.AddressID = Address.AddressID |
In the following example, the $cols parameter is null, but the names of the tables to be joined are specified in the $tables parameter:
<?php $tables = array('Person', 'Address', 'Phone'); $report = $db->autoJoin(null,$tables); $result = $db->select($report); ?> |
SELECT * FROM Person, Phone, Address, PersonPhone WHERE PersonPhone.PhoneID = Phone.PhoneID AND PersonPhone.PersonID = Person.PersonID AND Person.AddressID = Address.AddressID |
Algorithm: The algorithm used by autoJoin() is designed to find appropriate join conditions if these exist and are unambiguous, and to return a PEAR Error if the structure of references and linking tables either yields a multiply connected network of joins, or if it cannot construct an appropriate set of join conditions. The method first examines the $col and $table property to identify the list of tables that must be joined. It then creates a network of joined tables (the joined set) by starting with one table and sequentially adding tables to the joined set from the set of tables have not yet been joined (the unjoined set). The process starts by taking the first required table as a nucleus of the joined set, and then iterating through the unjoined set in search of a table that can be joined to the first. During this and each subsequent stage of addition, the method iterates through the unjoined set in search of a table that can be joined to any table in the joined set (i.e., that either references or is referenced by one of the tables in the joined set.) If it finds an unjoined table that can be joined to exactly one table in the joined set, that table is added to the joined set, and the search for another table to join begins. If the search encounters a table in the unjoined set that can be joined to two or more tables in the joined set, the method returns an error indicating that the join conditions are ambiguous -- the method will only return a set of joins that correspond to a tree graph (where tables are nodes and joins are bonds) and will reject any multiply connected set of joins. If it is found that none of the tables in the unjoined set can be directly joined to any table in the joined set, the method then cycles through the unjoined set again in search of a table that can be joined to exactly one table in the joined set through a linking tables. If it finds a table that is connected via linking tables to two or more tables in the joined set, it will also return an error. If the search does not identify any unjoined table that can be joined to a table in the joined set either through a direct reference or a linking table, the method returns an error indicating that the required set of tables can not be joined.
SQL Utilities
quote()
The quote method returns an SQL literal string representation of the parameter $value.
Synposis:
string quote(mixed $value) |
buildFilter()
buildFilter() returns a SQL logical expression that is true if the values of a specified set of database columns are equal to a corresponding set of SQL literal values. It must be passed an array parameter in which the array keys are column names and the array values are the required values.
The following example uses the buildFilter method to construct a filter for addresses on Pine St. in Peoria:
<?php $data = array('Street' => 'Pine St', 'City' => 'Peoria'); $filter = $db->buildFilter($data); ?> |
Street = 'Pine St' AND City = 'Peoria' |
buildSQL()
buildSQL() - takes a query array of the form used by the select* methods, and returns a corresponding SQL command string. It is called internally by the select*() methods. Both buildSQL() and the select*() methods are inherited from DB_Table_Base.
Synopsis: The interface is similar to that of the select*() methods:
string|PEAR_Error buildSQL(array|string $query, [string $filter], [string $order], [int $start], [int $count]) |
validCol()
validCol() - validates and (if necessary) disambiguates column names.
Synopsis:
array|PEAR_Error validCol(string $col, [array $from]) |
The $col parameter may either be a column name qualified by a table name, using the SQL syntax table.column, or a column name that is not qualified by a table name, if the identification of the column with a table is unambiguous. The return value of validCol, upon success, is a sequential array in which the second element is the unqualified column name string, and the first element is either a table name (if $col is qualified by a table name or a unique table can be identified) or a sequential array of possible column names (if $col is an unqualified column name that could refer to columns in two or more different tables). If no column with the specified name exists in the database, a PEAR error is returned.
The optional $from parameter is used only when $col is not explicitly qualified by a table name. When it is present, $from is a sequential list of tables that should be searched for a column of the specified name (as in the from clause of an SQL select statement). In this case, validCol first searches the tables in $from, and returns a table name if this yields a unique result. If a set of or more tables in $from are found to contain a column with the specified name, the return value is that set, or a subset thereof. If none of the table in $from contain a columns with the specified name, the search is instead broadened to all tables in the database. If two or more choices still remain at this point (either more than one tables in from, or more than one tables in the rest of the database) the method tries excluding tables in which the specified column is a foreign key column, if this still leaves one or more tables in which the column is not a foreign key column.
Data Modification: insert(), update(), and delete()
The insert(), delete(), and update() methods of DB_Table_Database have interfaces and behaviors similiar to those of the corresponding methods of DB_Table. The only differences in the interfaces are that each of these DB_Table_Database method requires an additional first parameter whose value is the name of the table to which the SQL insert, update, or delete command should be applied.
Synopses:
true|PEAR_Error insert(string $table, array $data) true|PEAR_Error delete(string $table, [string $where]) true|PEAR_Error update(string $table, array $data, [string $where]) |
These DB_Table_Database methods are simple wrappers that call the corresponding methods DB_Table methods internally. As one result, overriding any of these methods in a subclass of DB_Table in order to customize the behavior of a specific table will automatically modify the behavior of the DB_Table_Database method.
The DB_Table_Database data insert() and update() methods can validate foreign key values before actually modifying data in the database, and can emulate referentially triggered actions such cascading deletes, if foreign key validation and these referentially triggered actions are enabled. The corresponding methods of DB_Table will take identical actions if the DB_Table has been added to a parent DB_Table_Database object (i.e., if it contains a reference to a parent object), and if these actions are enabled in the parent DB_Table_Database object. Foreign key validation is disabled by default. Referentially triggered actions are enabled by default, for any such action that is declared in the database model.
The insert() and update() methods return a PEAR_Error object if foreign key validation fails, or if an error occurs during any database command. A PEAR_Error is also returned if a 'restrict' ON DELETE or ON UPDATE action is declared, when such actions are enabled, if an attempt is made to delete or update any row that is referenced by a foreign key of one or more rows of another table.
PHP Serialization
One way to maintain the state of DB_Table_Database between web pages is to serialize the entire database as one string, and save it in a session variable, a file, or a database. A serialized DB_Table_Database object contains serialized versions of all of its tables, and thus contains the information necessary to reconstruct the database. Serialization is accomplished by the PHP serialize function:
<?php $db_serial = serialize($db); ?> |
<?php $db = unserialize($db_serial); $db->setDBconnection($DB_object); ?> |
When a DB_Table_Database object is unserialized, each child DB_Table object is unserialized in turn by the DB_Table_Database::__wakeup() method. If the DB_Table objects are instances of subclasses of DB_Table, this requires that the definitions of these subclasses exist in memory prior to unserialization of the table. This can be accomplished by explicitly including the file or files containing the required class definitions in the file containing the unserialize command, or by taking advantage of an auto-load mechanism that is built into the wake-up method.
In order for autoloading of subclass definitions to work, each of the subclasses must be defined in a separate file in a default directory, with a filename that is given by the class name with an added '.php' extension. If the definition of a required subclass of DB_Table named "classname" is found to not exist in memory when needed during unserialization, the __wakeup() method tries to include a file named "classname.php" in this directory.
For autoloading to work, the base of each such filename must be the class name obtained by applying the built-in get_class function to the object. This yields a lower case class name PHP 4 and preserves the capitalization used in the class definition in PHP 5.
setTableSubclassPath()
setTableSubclassPath() - sets the path to the default directory for DB_Table subclass definitions.
Synopsis:
void setTableSubclassPath(string $path) |
XML Serialization
The toXML() and fromXML() methods may be used to serialize a database schema to, and unserialize it from, an XML string, respectively. The fromXML() method uses simpleXML to parse the XML string, and so requires PHP 5. (This is the only method in the class that is not compatible with PHP 4).
The XML schema used by these methods is an extension of the current MDB2_Schema DTD, extended so as to allow specification of foreign key references. This extension for foreign keys has been agreed upon for adoption in a future release of MDB2_Schema.
The toXML() method returns an XML string for the entire database, including all of its tables and foreign key references, like so:
<?php $xml_string = $db->toXML(); ?> |
<?php $db = DB_Table_Database::fromXML($xml_string); $db->setDBconnection($DB_object); ?> |
Setting DB_Table properties
Several methods of DB_Table_Database are used to set a common value of a DB_Table property for every child table in the database. These methods, which have the same names and interfaces as the corresponding DB_Table methods, are:
void autoValidInsert(bool $flag); void autoValidUpdate(bool $flag); void autoRecast(bool $flag); void autoInc(bool $flag); |
The autoValidInsert and autoValidUpdate methods turn on or off the automatic validation that data is of the expected type prior to insertion or updating of the data. The autoRecast method turns on or off the attempted recasting of data to the expected data type, if necessary, prior to insertion or updating. autoInc turns on or off the PHP implementation of auto-incrementation of the value of the $auto_inc_col column (if any) upon insertion. Note that, when the feature is on, this column is still auto-incremented only if its value is left null in the data to be inserted.
Get* Methods
Most of the properties of DB_Table_Database are private. A get* method is defined for each private property. Please see the API documentation for a discussion of all of properties and associated get* methods.
Пред. | Начало | След. |
DB_Table Forms Tutorial | Уровень выше | DB_Table_Generator Class Tutorial |