cvs: peardoc /en/package/database/db-table intro-database.xml
| From: | David Morse | Date: | Fri, 29 Jun 2007 02:27:41 +0000 |
| Subject: | cvs: peardoc /en/package/database/db-table intro-database.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-9843@lists.php.net to get a copy of this message | ||
morse Fri Jun 29 02:27:41 2007 UTC
Modified files:
/peardoc/en/package/database/db-table intro-database.xml
Log:
Changed to reflect existence of a base class DB_Table_Base
http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/db-table/intro-database.xml?r1=1.2&r2=1.3&diff_format=u Index: peardoc/en/package/database/db-table/intro-database.xml diff -u peardoc/en/package/database/db-table/intro-database.xml:1.2 peardoc/en/package/database/db-table/intro-database.xml:1.3 --- peardoc/en/package/database/db-table/intro-database.xml:1.2 Fri Jun 8 02:26:45 2007 +++ peardoc/en/package/database/db-table/intro-database.xml Fri Jun 29 02:27:41 2007 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.2 $ --> +<!-- $Revision: 1.3 $ --> <refentry id="package.database.db-table.intro-database"> <refnamediv> @@ -142,26 +142,10 @@ <function>addLink</function></link> - Declaring linking tables</simpara> </listitem> <listitem> - <simpara> - <link + <simpara> + <link linkend="package.database.db-table.intro-database.model.delete">Deleting Tables, References, or Links</link> - </simpara> - <itemizedlist> - <listitem> - <simpara><link - linkend="package.database.db-table.intro-database.model.delete.table"> - <function>deleteTable</function></link></simpara> - </listitem> - <listitem> - <simpara><link -linkend="package.database.db-table.intro-database.model.delete.ref"> - <function>deleteRef</function></link></simpara> - </listitem> - <listitem> - <simpara><link linkend="package.database.db-table.intro-database.deleteLink"> - <function>deleteLink</function></link></simpara> - </listitem> - </itemizedlist> + </simpara> </listitem> </itemizedlist> </listitem> @@ -208,81 +192,88 @@ </listitem> <listitem> <simpara> - <link linkend="package.database.db-table.intro-database.select">Data Selection</link> + <link linkend="package.database.db-table.intro-database.select"> + Data Selection</link> + </simpara> + <itemizedlist> + <listitem> + <simpara> + <link linkend="package.database.db-table.intro-database.select.query"> + Query Arrays + </link> + </simpara> + </listitem> + <listitem> + <simpara> + <link linkend="package.database.db-table.intro-database.select.methods"> + <function>select</function>, <function>selectResult</function>, and + <function>selectCount</function> + </link> + (from DB_Table_Base) + </simpara> + </listitem> + <listitem> + <simpara> + <link linkend="package.database.db-table.intro-database.select.autoJoin"> + <function>autoJoin</function></link> + </simpara> + </listitem> + </itemizedlist> + </listitem> + <listitem> + <simpara> + <link linkend="package.database.db-table.intro-database.util"> + SQL Utilities</link> </simpara> <itemizedlist> <listitem> <simpara> - <link -linkend="package.database.db-table.intro-database.select.query">Query Arrays</link> + <link linkend="package.database.db-table.intro-database.util.quote"> + <function>quote</function></link> </simpara> </listitem> <listitem> <simpara> - <link -linkend="package.database.db-table.intro-database.select.methods">Select* -Methods: <function>select</function>, <function>selectResult</function>, - <function>selectCount</function></link> + <link linkend="package.database.db-table.intro-database.util.buildFilter"> + <function>buildFilter</function></link> + (from DB_Table_Base) </simpara> </listitem> <listitem> <simpara> - <link -linkend="package.database.db-table.intro-database.select.autoJoin"><function>autoJoin</function></link> + <link linkend="package.database.db-table.intro-database.util.buildSQL"> + <function>buildSQL</function></link> + (from DB_Table_Base) </simpara> </listitem> <listitem> <simpara> - <link linkend="package.database.db-table.intro-database.select.util">SQL Utilities</link> + <link linkend="package.database.db-table.intro-database.util.validCol"> + <function>validCol</function></link> </simpara> - <itemizedlist> - <listitem> - <simpara> - <link -linkend="package.database.db-table.intro-database.select.quote"> - <function>quote</function></link> - </simpara> - </listitem> - <listitem> - <simpara> - <link -linkend="package.database.db-table.intro-database.select.buildFilter"><function>buildFilter</function></link> - </simpara> - </listitem> - <listitem> - <simpara> - <link - linkend="package.database.db-table.intro-database.select.buildSQL"> - <function>buildSQL</function></link> - </simpara> - </listitem> - <listitem> - <simpara> - <link -linkend="package.database.db-table.intro-database.select.validCol"> - <function>validCol</function></link> - </simpara> - </listitem> - </itemizedlist> </listitem> </itemizedlist> </listitem> <listitem> <simpara> - <link linkend="package.database.db-table.intro-database.modify">Data -Modification: <function>Insert</function>, <function>Update</function>, and - <function>Delete</function></link> + <link linkend="package.database.db-table.intro-database.modify"> + Data Modification: + <function>insert</function>, <function>update</function>, and + <function>delete</function></link> </simpara> </listitem> <listitem> <simpara> - <link linkend="package.database.db-table.intro-database.serialize">PHP Serialization</link> + <link linkend="package.database.db-table.intro-database.serialize"> + PHP Serialization + </link> </simpara> <itemizedlist> <listitem> <simpara> - <link -linkend="package.database.db-table.intro-database.serialize.setTableSubclassPath"><function>setTableSubclassPath</function></link> + <link linkend="package.database.db-table.intro-database.serialize.setTableSubclassPath"> + <function>setTableSubclassPath</function> + </link> </simpara> </listitem> </itemizedlist> @@ -524,7 +515,7 @@ A <classname>DB_Table_Database</classname> object is instantiated as an empty shell, to which tables, foreign key references, and links are then added. The constructor interface is -<programlisting role="php"> +<programlisting> <![CDATA[void DB_Table_Database(DB/MDB2 object $conn, string $name)]]> </programlisting> The parameter $conn must be either a DB or MDB2 object, which establishes @@ -563,14 +554,14 @@ can be added to the parent database with the <function>DB_Table_Database::addTable</function> method. The interface for this method is -<programlisting role="php"> +<programlisting> <![CDATA[ -true|Error addTable(object &$Table) +true|PEAR_Error addTable(object &$Table) ]]> </programlisting> where $Table is a <classname>DB_Table</classname> object that is passed by reference. The method returns boolean true on normal completion, and -a PEAR_Error object on failure. +a <classname>PEAR_Error</classname> object on failure. </para><para> The following code adds the four tables of our example database to the $db <classname>DB_Table_Database</classname> object: @@ -597,10 +588,9 @@ method to add references between pairs of tables. </para><para> Synopsis (simplified): -<programlisting role="php"> +<programlisting> <![CDATA[ -true|Error addRef(string $ftable, string|array $fkey, - string $rtable, [string|array] $rkey] ) +true|PEAR_Error addRef(string $ftable, string|array $fkey, string $rtable, [string|array $rkey] ) ]]> </programlisting> Here $ftable is the name of a referencing (or foreign key) table, $fkey @@ -612,11 +602,11 @@ <classname>DB_Table</classname>: Each key may be either a column name string, for a single-column key, or a sequential array of column names, for a multi-column key. The method returns true on -normal completion, and a PEAR_Error on failure. The simplified synopsis -shown here does not include two more optional parameters (parameters 5 and -6) that can be used to specify 'on delete' and 'on update' actions. The -full interface is presented <link -linkend="package.database.db-table.intro-database.refAction.addRef.synopsis">below</link>. +normal completion, and a <classname>PEAR_Error</classname> on failure. The +simplified synopsis shown here does not include two more optional parameters +(parameters 5 and 6) that can be used to specify 'on delete' and 'on update' +actions. The full interface is presented +<link linkend="package.database.db-table.intro-database.refAction.addRef.synopsis">below</link>. For example, the command: <programlisting role="php"> @@ -660,9 +650,9 @@ others. </para><para> Synopsis: -<programlisting role="php"> +<programlisting> <![CDATA[ -true|Error addLink(string $table1, string $table2, string $link) +true|PEAR_Error addLink(string $table1, string $table2, string $link) ]]> </programlisting> Here, $link is the name of a linking table that links tables named @@ -822,7 +812,7 @@ <para> Synopsis: -<programlisting role="php"> +<programlisting> <![CDATA[void deleteTable(string $table)]]> </programlisting> Parameter $table is the name of the table to be deleted. Deletion @@ -838,7 +828,7 @@ </para> <para> Synopsis: -<programlisting role="php"> +<programlisting> <![CDATA[void deleteRef(string $ftable, string $rtable)]]> </programlisting> where $ftable and $rtable are the names of the referencing and @@ -852,7 +842,7 @@ </para> <para> Synopsis: -<programlisting role="php"> +<programlisting> <![CDATA[void deleteLink(string $table1, string $table2, [string $link])]]> </programlisting> Here $table1 and $table2 are names of the linked tables, and the @@ -924,11 +914,10 @@ </para> <para> The full interface of the <function>addRef</function> method is: -<programlisting role="php" +<programlisting id="package.database.db-table.intro-database.refAction.addRef.synopsis"> <![CDATA[ -true|Error addRef(string $ftable, mixed $fkey, string $rtable, [mixed $rkey], - [string|null $on_delete], [string|null $on_update]) +true|PEAR_Error addRef(string $ftable, mixed $fkey, string $rtable, [mixed $rkey], [string|null $on_delete], [string|null $on_update]) ]]> </programlisting> Here, $ftable is the referencing table, $fkey is the foreign key, $rtable @@ -980,7 +969,7 @@ and setOnUpdate methods. </para><para> Synopses: -<programlisting role="php"> +<programlisting> <![CDATA[ void setOnDelete(string $ftable, string $rtable, string|null $action) void setOnUpdate(string $ftable, string $rtable, string|null $action) @@ -1018,7 +1007,7 @@ </para> <para> Synopses: -<programlisting role="php"> +<programlisting> <![CDATA[ void setActOnDelete(bool $flag) void setActOnUpdate(bool $flag) @@ -1055,7 +1044,7 @@ 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 <function>setCheckFKey</function> method. The interface of this method is: -<programlisting role="php"> +<programlisting> <![CDATA[void setCheckFKey(bool $flag);]]> </programlisting> Passing a true value of $flag turns on checking of foreign keys (the @@ -1079,7 +1068,7 @@ 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 -<programlisting role="php"> +<programlisting> <![CDATA[<?php $oak = array( 'select' => 'Person.FirstName, Person.LastName, Address.Building', @@ -1128,14 +1117,16 @@ <refsect2 id="package.database.db-table.intro-database.select.methods"> <title>Select* Methods: - <function>select</function>, <function>selectResult</function>, + <function>select</function>, <function>selectResult</function>, and <function>selectCount</function> </title> <para> -The select method of <classname>DB_Table_Database</classname> has an -interface similar to that -of the <classname>DB_Table</classname> select method. The required first parameter can be -either the key for a previously stored query array, as in +The <function>select*</function> methods are inherited by both the +<classname>DB_Table_Database</classname> and <classname>DB_Table</classname> +classes from the <classname>DB_Table_Base</classname> 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 <programlisting role="php"> <![CDATA[<?php $result = $db->select('oak') @@ -1147,41 +1138,35 @@ $result = $db->select($oak) ?>]]> </programlisting> -The select method returns a result set as a numerically indexed -array of rows. Each row can represented as be either an associative -or numerical array, depending on the value of the $fetch_mode -property of the <classname>DB_Table_Database</classname> object or -(if this is null) the -fetchmode of the underlying DB or MDB2 object. The class also -contains selectCount and selectResult methods with behaviors and -interfaces similar to those of corresponding methods of -<classname>DB_Table</classname>. +The select method returns a result set as a numerically indexed array of +rows. Each row can represented as be either an associative or numerical +array, or an object, depending on the value of the $fetch_mode property of +the <classname>DB_Table_Database</classname> object or (if this is null) +the fetchmode of the underlying DB or MDB2 object. </para> <para> -The interface is the same for all three select* methods, +The common interface of three select* methods <function>select</function>, <function>selectCount</function>, and -<function>selectResult</function>. The common interface is: -<programlisting role="php"> +<function>selectResult</function> is: +<programlisting> <![CDATA[ -array select*( string|array $sql_key, [string $filter], [string $order], - [int $start], [int $count], [array $params]) +mixed select*( string|array $sql_key, [string $filter], [string $order], [int $start], [int $count], [array $params]) ]]> </programlisting> -Here, $sql_key is either the key of a baseline query array that -has been stored in the $sql property, or a query array. The -$filter parameter is an SQL logical expression string that limits -the result set. This condition is added (i.e., ANDed) to the end -of the 'where' element of the $sql_key query array. The $order -parameter is an ORDER BY clause (without the ORDER BY prefix) that -can be used to override any 'order' element of the $sql_key array. -Integer parameters $start is which the position within the full -result set of the first row that should be included in the return -value, while $count is the maximum number of rows desired within -the return value. If present, $params is an array in which the -values are parameters for placeholder substitution in a prepared -query. +As discussed above, $sql_key is either a query array, or the key of +a baseline query array that has been stored in the $sql property. The +$filter parameter is an SQL logical expression string that limits the +result set. This condition is added (i.e., ANDed) to the end of the +'where' element of the $sql_key query array. The $order parameter is +an ORDER BY clause (without the ORDER BY prefix) that can be used to +override any 'order' element of the $sql_key array. Integer parameters +$start is which the position within the full result set of the first +row that should be included in the return value, while $count is the +maximum number of rows desired within the return value. If present, +$params is an array in which the values are parameters for placeholder +substitution in a prepared query. </para> </refsect2> @@ -1324,11 +1309,12 @@ set of tables can not be joined. </para> </refsect2> +</refsect1> -<refsect2 id="package.database.db-table.intro-database.select.util"> +<refsect1 id="package.database.db-table.intro-database.util"> <title>SQL Utilities</title> -<refsect3 id="package.database.db-table.intro-database.select.quote"> +<refsect2 id="package.database.db-table.intro-database.util.quote"> <title><function>quote</function></title> <para> The quote method returns an SQL literal string representation of the @@ -1351,15 +1337,15 @@ of booleans as integers used by the <classname>DB_Table</classname> abstract data type). If $value is null, it returns the unquoted string NULL. </para> -</refsect3> -<refsect3 id="package.database.db-table.intro-database.select.buildFilter"> +</refsect2> +<refsect2 id="package.database.db-table.intro-database.util.buildFilter"> <title><function>buildFilter</function></title> <para> -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. In the simplest usage, it is passed an array +<function>buildFilter</function> 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 corresponding values. +are the required values. </para><para> The following example uses the buildFilter method to construct a filter for addresses on Pine St. in Peoria: @@ -1382,14 +1368,10 @@ require escaping. The buildFilter method uses the quote method internally to construct SQL literal string representations of values. -</para><para> -More complicated uses of buildFilter, and the full interface, which -contains two more optional parameters, is discussed in the API -documentation. </para> -</refsect3> +</refsect2> -<refsect3 id="package.database.db-table.intro-database.select.buildSQL"> +<refsect2 id="package.database.db-table.intro-database.util.buildSQL"> <title><function>buildSQL</function></title> <para> <function>buildSQL</function> - takes a query array of the form used by @@ -1409,9 +1391,9 @@ and $start and $count are the first row in the result set that should be returned, and the maximum number of rows that should be returned. </para> -</refsect3> +</refsect2> -<refsect3 id="package.database.db-table.intro-database.select.validCol"> +<refsect2 id="package.database.db-table.intro-database.util.validCol"> <title><function>validCol</function></title> <para> <function>validCol</function> - validates and (if necessary) disambiguates @@ -1454,65 +1436,79 @@ leaves one or more tables in which the column is not a foreign key column. </para> -</refsect3> </refsect2> </refsect1> <refsect1 id="package.database.db-table.intro-database.modify"> -<title>Data Modification (Insert, Update, and Delete)</title> +<title>Data Modification: <function>insert</function>, + <function>update</function>, and <function>delete</function> +</title> <para> -The -<function>insert</function>, <function>delete</function>, and +The <function>insert</function>, <function>delete</function>, and <function>update</function> methods of <classname>DB_Table_Database</classname> 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 <classname>DB_Table_Database</classname> 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. The only differences in behavior -are that each of these three <classname>DB_Table_Database</classname> methods -can be set to silently take actions to guarantee referential integrity. +update, or delete command should be applied. </para> + <para> Synopses: <programlisting role="php"> <![CDATA[ -true|Error insert(string $table, array $data) -true|Error delete(string $table, [string $where]) -true|Error update(string $table, array $data, [string $where]) -]]> -</programlisting> -In all three functions $table_name is the name of the table to -which the operation should be applied. In the insert and update -methods, $data is an associative array of data to be inserted -or updated, in which the keys are column name strings and the -values are the value to be inserted or updated in the database. -In the delete and update methods, the optional $where parameter -is a string containing an SQL logical condition that is used to -select the rows that should be deleted or updated, respectively. -That is, the $where parameter should contain the contents of the -WHERE clause of the corresponding SQL command, without the -'WHERE ' prefix. Each method returns true on normal completion, -and a PEAR Error if an error is encountered. -</para><para> -Each of these <classname>DB_Table_Database</classname> methods -internally calls the corresponding - <classname>DB_Table</classname> method to actually insert, delete, or -update the data in the specified table. Overriding any of these -methods in a subclass of <classname>DB_Table</classname> in order to customize the -behavior of a table will thus also modify the behavior of -the <classname>DB_Table_Database</classname> method. -</para><para> -By default, the insert and update methods check validity of -foreign key values before modifying the database. Both of -these methods return a PEAR Error if foreign key values are -found to be invalid. The delete and update methods can be -set to execute ON DELETE and ON UPDATE referentially -triggered actions. If either associated action is set to -'restrict', the delete or update method will return a PEAR -error if an attempt is made to delete or update the referenced -key in rows that are referenced by foreign keys in another -table. +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]) +]]> +</programlisting> +In all three functions $table_name is the name of the table to which +the operation should be applied. In the insert and update methods, +$data is an associative array of data to be inserted or updated, in +which the keys are column name strings and the values are the value +to be inserted or updated in the database. In the delete and update +methods, the optional $where parameter is a string containing an SQL +logical condition that is used to select the rows that should be +deleted or updated, respectively. That is, the $where parameter should +contain the contents of the WHERE clause of the corresponding SQL +command, without the 'WHERE ' prefix. Each method returns true on normal +completion, and a PEAR Error if an error is encountered. +</para> + +<para> +These <classname>DB_Table_Database</classname> methods are simple wrappers +that call the corresponding methods <classname>DB_Table</classname> +methods internally. As one result, overriding any of these methods in a +subclass of <classname>DB_Table</classname> in order to customize the behavior +of a specific table will automatically modify the behavior of the +<classname>DB_Table_Database</classname> method. +</para> + +<para> +The <classname>DB_Table_Database</classname> data +<function>insert</function> and <function>update</function> 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 <classname>DB_Table</classname> will take +identical actions if the <classname>DB_Table</classname> has been added to +a parent <classname>DB_Table_Database</classname> object (i.e., if it +contains a reference to a parent object), and if these actions are enabled +in the parent <classname>DB_Table_Database</classname> 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. </para> + +<para> +The <function>insert</function> and <function>update</function> methods return +a <classname>PEAR_Error</classname> object if foreign key validation fails, or if +an error occurs during any database command. A <classname>PEAR_Error</classname> +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. +</para> + </refsect1> <refsect1 id="package.database.db-table.intro-database.serialize"> @@ -1635,8 +1631,8 @@ is returned by <function>fromXML</function> are all instances of <classname>DB_Table</classname> itself, rather than of custom subclasses of <classname>DB_Table</classname>. <function>fromXML</function> returns -a PEAR_Error if the XML string cannot be parsed, if an -error is thrown during instantiation of either the +a <classname>PEAR_Error</classname> if the XML string cannot be parsed, +if an error is thrown during instantiation of either the <classname>DB_Table_Database</classname> object or any of the child <classname>DB_Table</classname> objects, or if called with a PHP 4 interpreter (it requires PHP 5). @@ -1665,9 +1661,10 @@ the <classname>DB_Table</classname> <function>insert</function> and <function>update</function> methods. The <classname>DB_Table_Database</classname> <function>insert</function> -and <function>update</function> methods call the <classname>DB_Table</classname> -insert and update methods, so changes in these properties also change -the behavior of the <classname>DB_Table_Database</classname> methods. +and <function>update</function> methods simply call the corresponding +<classname>DB_Table</classname> methods, so changes in these properties +also change the behavior of the <classname>DB_Table_Database</classname> +methods. </para> <para> The autoValidInsert and autoValidUpdate methods turn on or off the
http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/db-table/intro-database.xml?r1=1.2&r2=1.3&diff_format=u Index: peardoc/en/package/database/db-table/intro-database.xml diff -u peardoc/en/package/database/db-table/intro-database.xml:1.2 peardoc/en/package/database/db-table/intro-database.xml:1.3 --- peardoc/en/package/database/db-table/intro-database.xml:1.2 Fri Jun 8 02:26:45 2007 +++ peardoc/en/package/database/db-table/intro-database.xml Fri Jun 29 02:27:41 2007 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.2 $ --> +<!-- $Revision: 1.3 $ --> <refentry id="package.database.db-table.intro-database"> <refnamediv> @@ -142,26 +142,10 @@ <function>addLink</function></link> - Declaring linking tables</simpara> </listitem> <listitem> - <simpara> - <link + <simpara> + <link linkend="package.database.db-table.intro-database.model.delete">Deleting Tables, References, or Links</link> - </simpara> - <itemizedlist> - <listitem> - <simpara><link - linkend="package.database.db-table.intro-database.model.delete.table"> - <function>deleteTable</function></link></simpara> - </listitem> - <listitem> - <simpara><link -linkend="package.database.db-table.intro-database.model.delete.ref"> - <function>deleteRef</function></link></simpara> - </listitem> - <listitem> - <simpara><link linkend="package.database.db-table.intro-database.deleteLink"> - <function>deleteLink</function></link></simpara> - </listitem> - </itemizedlist> + </simpara> </listitem> </itemizedlist> </listitem> @@ -208,81 +192,88 @@ </listitem> <listitem> <simpara> - <link linkend="package.database.db-table.intro-database.select">Data Selection</link> + <link linkend="package.database.db-table.intro-database.select"> + Data Selection</link> + </simpara> + <itemizedlist> + <listitem> + <simpara> + <link linkend="package.database.db-table.intro-database.select.query"> + Query Arrays + </link> + </simpara> + </listitem> + <listitem> + <simpara> + <link linkend="package.database.db-table.intro-database.select.methods"> + <function>select</function>, <function>selectResult</function>, and + <function>selectCount</function> + </link> + (from DB_Table_Base) + </simpara> + </listitem> + <listitem> + <simpara> + <link linkend="package.database.db-table.intro-database.select.autoJoin"> + <function>autoJoin</function></link> + </simpara> + </listitem> + </itemizedlist> + </listitem> + <listitem> + <simpara> + <link linkend="package.database.db-table.intro-database.util"> + SQL Utilities</link> </simpara> <itemizedlist> <listitem> <simpara> - <link -linkend="package.database.db-table.intro-database.select.query">Query Arrays</link> + <link linkend="package.database.db-table.intro-database.util.quote"> + <function>quote</function></link> </simpara> </listitem> <listitem> <simpara> - <link -linkend="package.database.db-table.intro-database.select.methods">Select* -Methods: <function>select</function>, <function>selectResult</function>, - <function>selectCount</function></link> + <link linkend="package.database.db-table.intro-database.util.buildFilter"> + <function>buildFilter</function></link> + (from DB_Table_Base) </simpara> </listitem> <listitem> <simpara> - <link -linkend="package.database.db-table.intro-database.select.autoJoin"><function>autoJoin</function></link> + <link linkend="package.database.db-table.intro-database.util.buildSQL"> + <function>buildSQL</function></link> + (from DB_Table_Base) </simpara> </listitem> <listitem> <simpara> - <link linkend="package.database.db-table.intro-database.select.util">SQL Utilities</link> + <link linkend="package.database.db-table.intro-database.util.validCol"> + <function>validCol</function></link> </simpara> - <itemizedlist> - <listitem> - <simpara> - <link -linkend="package.database.db-table.intro-database.select.quote"> - <function>quote</function></link> - </simpara> - </listitem> - <listitem> - <simpara> - <link -linkend="package.database.db-table.intro-database.select.buildFilter"><function>buildFilter</function></link> - </simpara> - </listitem> - <listitem> - <simpara> - <link - linkend="package.database.db-table.intro-database.select.buildSQL"> - <function>buildSQL</function></link> - </simpara> - </listitem> - <listitem> - <simpara> - <link -linkend="package.database.db-table.intro-database.select.validCol"> - <function>validCol</function></link> - </simpara> - </listitem> - </itemizedlist> </listitem> </itemizedlist> </listitem> <listitem> <simpara> - <link linkend="package.database.db-table.intro-database.modify">Data -Modification: <function>Insert</function>, <function>Update</function>, and - <function>Delete</function></link> + <link linkend="package.database.db-table.intro-database.modify"> + Data Modification: + <function>insert</function>, <function>update</function>, and + <function>delete</function></link> </simpara> </listitem> <listitem> <simpara> - <link linkend="package.database.db-table.intro-database.serialize">PHP Serialization</link> + <link linkend="package.database.db-table.intro-database.serialize"> + PHP Serialization + </link> </simpara> <itemizedlist> <listitem> <simpara> - <link -linkend="package.database.db-table.intro-database.serialize.setTableSubclassPath"><function>setTableSubclassPath</function></link> + <link linkend="package.database.db-table.intro-database.serialize.setTableSubclassPath"> + <function>setTableSubclassPath</function> + </link> </simpara> </listitem> </itemizedlist> @@ -524,7 +515,7 @@ A <classname>DB_Table_Database</classname> object is instantiated as an empty shell, to which tables, foreign key references, and links are then added. The constructor interface is -<programlisting role="php"> +<programlisting> <![CDATA[void DB_Table_Database(DB/MDB2 object $conn, string $name)]]> </programlisting> The parameter $conn must be either a DB or MDB2 object, which establishes @@ -563,14 +554,14 @@ can be added to the parent database with the <function>DB_Table_Database::addTable</function> method. The interface for this method is -<programlisting role="php"> +<programlisting> <![CDATA[ -true|Error addTable(object &$Table) +true|PEAR_Error addTable(object &$Table) ]]> </programlisting> where $Table is a <classname>DB_Table</classname> object that is passed by reference. The method returns boolean true on normal completion, and -a PEAR_Error object on failure. +a <classname>PEAR_Error</classname> object on failure. </para><para> The following code adds the four tables of our example database to the $db <classname>DB_Table_Database</classname> object: @@ -597,10 +588,9 @@ method to add references between pairs of tables. </para><para> Synopsis (simplified): -<programlisting role="php"> +<programlisting> <![CDATA[ -true|Error addRef(string $ftable, string|array $fkey, - string $rtable, [string|array] $rkey] ) +true|PEAR_Error addRef(string $ftable, string|array $fkey, string $rtable, [string|array $rkey] ) ]]> </programlisting> Here $ftable is the name of a referencing (or foreign key) table, $fkey @@ -612,11 +602,11 @@ <classname>DB_Table</classname>: Each key may be either a column name string, for a single-column key, or a sequential array of column names, for a multi-column key. The method returns true on -normal completion, and a PEAR_Error on failure. The simplified synopsis -shown here does not include two more optional parameters (parameters 5 and -6) that can be used to specify 'on delete' and 'on update' actions. The -full interface is presented <link -linkend="package.database.db-table.intro-database.refAction.addRef.synopsis">below</link>. +normal completion, and a <classname>PEAR_Error</classname> on failure. The +simplified synopsis shown here does not include two more optional parameters +(parameters 5 and 6) that can be used to specify 'on delete' and 'on update' +actions. The full interface is presented +<link linkend="package.database.db-table.intro-database.refAction.addRef.synopsis">below</link>. For example, the command: <programlisting role="php"> @@ -660,9 +650,9 @@ others. </para><para> Synopsis: -<programlisting role="php"> +<programlisting> <![CDATA[ -true|Error addLink(string $table1, string $table2, string $link) +true|PEAR_Error addLink(string $table1, string $table2, string $link) ]]> </programlisting> Here, $link is the name of a linking table that links tables named @@ -822,7 +812,7 @@ <para> Synopsis: -<programlisting role="php"> +<programlisting> <![CDATA[void deleteTable(string $table)]]> </programlisting> Parameter $table is the name of the table to be deleted. Deletion @@ -838,7 +828,7 @@ </para> <para> Synopsis: -<programlisting role="php"> +<programlisting> <![CDATA[void deleteRef(string $ftable, string $rtable)]]> </programlisting> where $ftable and $rtable are the names of the referencing and @@ -852,7 +842,7 @@ </para> <para> Synopsis: -<programlisting role="php"> +<programlisting> <![CDATA[void deleteLink(string $table1, string $table2, [string $link])]]> </programlisting> Here $table1 and $table2 are names of the linked tables, and the @@ -924,11 +914,10 @@ </para> <para> The full interface of the <function>addRef</function> method is: -<programlisting role="php" +<programlisting id="package.database.db-table.intro-database.refAction.addRef.synopsis"> <![CDATA[ -true|Error addRef(string $ftable, mixed $fkey, string $rtable, [mixed $rkey], - [string|null $on_delete], [string|null $on_update]) +true|PEAR_Error addRef(string $ftable, mixed $fkey, string $rtable, [mixed $rkey], [string|null $on_delete], [string|null $on_update]) ]]> </programlisting> Here, $ftable is the referencing table, $fkey is the foreign key, $rtable @@ -980,7 +969,7 @@ and setOnUpdate methods. </para><para> Synopses: -<programlisting role="php"> +<programlisting> <![CDATA[ void setOnDelete(string $ftable, string $rtable, string|null $action) void setOnUpdate(string $ftable, string $rtable, string|null $action) @@ -1018,7 +1007,7 @@ </para> <para> Synopses: -<programlisting role="php"> +<programlisting> <![CDATA[ void setActOnDelete(bool $flag) void setActOnUpdate(bool $flag) @@ -1055,7 +1044,7 @@ 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 <function>setCheckFKey</function> method. The interface of this method is: -<programlisting role="php"> +<programlisting> <![CDATA[void setCheckFKey(bool $flag);]]> </programlisting> Passing a true value of $flag turns on checking of foreign keys (the @@ -1079,7 +1068,7 @@ 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 -<programlisting role="php"> +<programlisting> <![CDATA[<?php $oak = array( 'select' => 'Person.FirstName, Person.LastName, Address.Building', @@ -1128,14 +1117,16 @@ <refsect2 id="package.database.db-table.intro-database.select.methods"> <title>Select* Methods: - <function>select</function>, <function>selectResult</function>, + <function>select</function>, <function>selectResult</function>, and <function>selectCount</function> </title> <para> -The select method of <classname>DB_Table_Database</classname> has an -interface similar to that -of the <classname>DB_Table</classname> select method. The required first parameter can be -either the key for a previously stored query array, as in +The <function>select*</function> methods are inherited by both the +<classname>DB_Table_Database</classname> and <classname>DB_Table</classname> +classes from the <classname>DB_Table_Base</classname> 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 <programlisting role="php"> <![CDATA[<?php $result = $db->select('oak') @@ -1147,41 +1138,35 @@ $result = $db->select($oak) ?>]]> </programlisting> -The select method returns a result set as a numerically indexed -array of rows. Each row can represented as be either an associative -or numerical array, depending on the value of the $fetch_mode -property of the <classname>DB_Table_Database</classname> object or -(if this is null) the -fetchmode of the underlying DB or MDB2 object. The class also -contains selectCount and selectResult methods with behaviors and -interfaces similar to those of corresponding methods of -<classname>DB_Table</classname>. +The select method returns a result set as a numerically indexed array of +rows. Each row can represented as be either an associative or numerical +array, or an object, depending on the value of the $fetch_mode property of +the <classname>DB_Table_Database</classname> object or (if this is null) +the fetchmode of the underlying DB or MDB2 object. </para> <para> -The interface is the same for all three select* methods, +The common interface of three select* methods <function>select</function>, <function>selectCount</function>, and -<function>selectResult</function>. The common interface is: -<programlisting role="php"> +<function>selectResult</function> is: +<programlisting> <![CDATA[ -array select*( string|array $sql_key, [string $filter], [string $order], - [int $start], [int $count], [array $params]) +mixed select*( string|array $sql_key, [string $filter], [string $order], [int $start], [int $count], [array $params]) ]]> </programlisting> -Here, $sql_key is either the key of a baseline query array that -has been stored in the $sql property, or a query array. The -$filter parameter is an SQL logical expression string that limits -the result set. This condition is added (i.e., ANDed) to the end -of the 'where' element of the $sql_key query array. The $order -parameter is an ORDER BY clause (without the ORDER BY prefix) that -can be used to override any 'order' element of the $sql_key array. -Integer parameters $start is which the position within the full -result set of the first row that should be included in the return -value, while $count is the maximum number of rows desired within -the return value. If present, $params is an array in which the -values are parameters for placeholder substitution in a prepared -query. +As discussed above, $sql_key is either a query array, or the key of +a baseline query array that has been stored in the $sql property. The +$filter parameter is an SQL logical expression string that limits the +result set. This condition is added (i.e., ANDed) to the end of the +'where' element of the $sql_key query array. The $order parameter is +an ORDER BY clause (without the ORDER BY prefix) that can be used to +override any 'order' element of the $sql_key array. Integer parameters +$start is which the position within the full result set of the first +row that should be included in the return value, while $count is the +maximum number of rows desired within the return value. If present, +$params is an array in which the values are parameters for placeholder +substitution in a prepared query. </para> </refsect2> @@ -1324,11 +1309,12 @@ set of tables can not be joined. </para> </refsect2> +</refsect1> -<refsect2 id="package.database.db-table.intro-database.select.util"> +<refsect1 id="package.database.db-table.intro-database.util"> <title>SQL Utilities</title> -<refsect3 id="package.database.db-table.intro-database.select.quote"> +<refsect2 id="package.database.db-table.intro-database.util.quote"> <title><function>quote</function></title> <para> The quote method returns an SQL literal string representation of the @@ -1351,15 +1337,15 @@ of booleans as integers used by the <classname>DB_Table</classname> abstract data type). If $value is null, it returns the unquoted string NULL. </para> -</refsect3> -<refsect3 id="package.database.db-table.intro-database.select.buildFilter"> +</refsect2> +<refsect2 id="package.database.db-table.intro-database.util.buildFilter"> <title><function>buildFilter</function></title> <para> -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. In the simplest usage, it is passed an array +<function>buildFilter</function> 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 corresponding values. +are the required values. </para><para> The following example uses the buildFilter method to construct a filter for addresses on Pine St. in Peoria: @@ -1382,14 +1368,10 @@ require escaping. The buildFilter method uses the quote method internally to construct SQL literal string representations of values. -</para><para> -More complicated uses of buildFilter, and the full interface, which -contains two more optional parameters, is discussed in the API -documentation. </para> -</refsect3> +</refsect2> -<refsect3 id="package.database.db-table.intro-database.select.buildSQL"> +<refsect2 id="package.database.db-table.intro-database.util.buildSQL"> <title><function>buildSQL</function></title> <para> <function>buildSQL</function> - takes a query array of the form used by @@ -1409,9 +1391,9 @@ and $start and $count are the first row in the result set that should be returned, and the maximum number of rows that should be returned. </para> -</refsect3> +</refsect2> -<refsect3 id="package.database.db-table.intro-database.select.validCol"> +<refsect2 id="package.database.db-table.intro-database.util.validCol"> <title><function>validCol</function></title> <para> <function>validCol</function> - validates and (if necessary) disambiguates @@ -1454,65 +1436,79 @@ leaves one or more tables in which the column is not a foreign key column. </para> -</refsect3> </refsect2> </refsect1> <refsect1 id="package.database.db-table.intro-database.modify"> -<title>Data Modification (Insert, Update, and Delete)</title> +<title>Data Modification: <function>insert</function>, + <function>update</function>, and <function>delete</function> +</title> <para> -The -<function>insert</function>, <function>delete</function>, and +The <function>insert</function>, <function>delete</function>, and <function>update</function> methods of <classname>DB_Table_Database</classname> 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 <classname>DB_Table_Database</classname> 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. The only differences in behavior -are that each of these three <classname>DB_Table_Database</classname> methods -can be set to silently take actions to guarantee referential integrity. +update, or delete command should be applied. </para> + <para> Synopses: <programlisting role="php"> <![CDATA[ -true|Error insert(string $table, array $data) -true|Error delete(string $table, [string $where]) -true|Error update(string $table, array $data, [string $where]) -]]> -</programlisting> -In all three functions $table_name is the name of the table to -which the operation should be applied. In the insert and update -methods, $data is an associative array of data to be inserted -or updated, in which the keys are column name strings and the -values are the value to be inserted or updated in the database. -In the delete and update methods, the optional $where parameter -is a string containing an SQL logical condition that is used to -select the rows that should be deleted or updated, respectively. -That is, the $where parameter should contain the contents of the -WHERE clause of the corresponding SQL command, without the -'WHERE ' prefix. Each method returns true on normal completion, -and a PEAR Error if an error is encountered. -</para><para> -Each of these <classname>DB_Table_Database</classname> methods -internally calls the corresponding - <classname>DB_Table</classname> method to actually insert, delete, or -update the data in the specified table. Overriding any of these -methods in a subclass of <classname>DB_Table</classname> in order to customize the -behavior of a table will thus also modify the behavior of -the <classname>DB_Table_Database</classname> method. -</para><para> -By default, the insert and update methods check validity of -foreign key values before modifying the database. Both of -these methods return a PEAR Error if foreign key values are -found to be invalid. The delete and update methods can be -set to execute ON DELETE and ON UPDATE referentially -triggered actions. If either associated action is set to -'restrict', the delete or update method will return a PEAR -error if an attempt is made to delete or update the referenced -key in rows that are referenced by foreign keys in another -table. +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]) +]]> +</programlisting> +In all three functions $table_name is the name of the table to which +the operation should be applied. In the insert and update methods, +$data is an associative array of data to be inserted or updated, in +which the keys are column name strings and the values are the value +to be inserted or updated in the database. In the delete and update +methods, the optional $where parameter is a string containing an SQL +logical condition that is used to select the rows that should be +deleted or updated, respectively. That is, the $where parameter should +contain the contents of the WHERE clause of the corresponding SQL +command, without the 'WHERE ' prefix. Each method returns true on normal +completion, and a PEAR Error if an error is encountered. +</para> + +<para> +These <classname>DB_Table_Database</classname> methods are simple wrappers +that call the corresponding methods <classname>DB_Table</classname> +methods internally. As one result, overriding any of these methods in a +subclass of <classname>DB_Table</classname> in order to customize the behavior +of a specific table will automatically modify the behavior of the +<classname>DB_Table_Database</classname> method. +</para> + +<para> +The <classname>DB_Table_Database</classname> data +<function>insert</function> and <function>update</function> 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 <classname>DB_Table</classname> will take +identical actions if the <classname>DB_Table</classname> has been added to +a parent <classname>DB_Table_Database</classname> object (i.e., if it +contains a reference to a parent object), and if these actions are enabled +in the parent <classname>DB_Table_Database</classname> 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. </para> + +<para> +The <function>insert</function> and <function>update</function> methods return +a <classname>PEAR_Error</classname> object if foreign key validation fails, or if +an error occurs during any database command. A <classname>PEAR_Error</classname> +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. +</para> + </refsect1> <refsect1 id="package.database.db-table.intro-database.serialize"> @@ -1635,8 +1631,8 @@ is returned by <function>fromXML</function> are all instances of <classname>DB_Table</classname> itself, rather than of custom subclasses of <classname>DB_Table</classname>. <function>fromXML</function> returns -a PEAR_Error if the XML string cannot be parsed, if an -error is thrown during instantiation of either the +a <classname>PEAR_Error</classname> if the XML string cannot be parsed, +if an error is thrown during instantiation of either the <classname>DB_Table_Database</classname> object or any of the child <classname>DB_Table</classname> objects, or if called with a PHP 4 interpreter (it requires PHP 5). @@ -1665,9 +1661,10 @@ the <classname>DB_Table</classname> <function>insert</function> and <function>update</function> methods. The <classname>DB_Table_Database</classname> <function>insert</function> -and <function>update</function> methods call the <classname>DB_Table</classname> -insert and update methods, so changes in these properties also change -the behavior of the <classname>DB_Table_Database</classname> methods. +and <function>update</function> methods simply call the corresponding +<classname>DB_Table</classname> methods, so changes in these properties +also change the behavior of the <classname>DB_Table_Database</classname> +methods. </para> <para> The autoValidInsert and autoValidUpdate methods turn on or off the