cvs: peardoc /en/package/database/db-table intro-table.xml

From: Date: Sat, 02 Jun 2007 18:04:03 +0000
Subject: cvs: peardoc /en/package/database/db-table intro-table.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-9756@lists.php.net to get a copy of this message
morse Sat Jun 2 18:04:03 2007 UTC Modified files: /peardoc/en/package/database/db-table intro-table.xml Log: Edited for clarity.

http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/db-table/intro-table.xml?r1=1.1&r2=1.2&diff_format=u Index: peardoc/en/package/database/db-table/intro-table.xml diff -u peardoc/en/package/database/db-table/intro-table.xml:1.1 peardoc/en/package/database/db-table/intro-table.xml:1.2 --- peardoc/en/package/database/db-table/intro-table.xml:1.1 Tue May 29 18:42:23 2007 +++ peardoc/en/package/database/db-table/intro-table.xml Sat Jun 2 18:04:02 2007 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.1 $ --> +<!-- $Revision: 1.2 $ --> <refentry id="package.database.db-table.intro-table"> <refnamediv> @@ -45,6 +45,11 @@ </listitem> <listitem> <simpara> + Auto-increment emulation + </simpara> + </listitem> + <listitem> + <simpara> Automated creation of <classname>HTML_QuickForm</classname> elements from the column definitions. </simpara> @@ -62,7 +67,7 @@ <ulink url="http://wiki.ciaweb.net/yawiki/index.php?area=DB_Table"> here</ulink>. The original documentation includes some examples of how to customize a <classname>DB_Table</classname> subclass definition -that are not yet included here. +that are not included here. </para> <itemizedlist> @@ -146,7 +151,7 @@ <listitem> <simpara> <link linkend="package.database.db-table.intro-table.select.submit"> - Submitting Queries</link> + Submitting Queries: <function>select*</function> methods</link> </simpara> </listitem> </itemizedlist> @@ -206,15 +211,13 @@ each RDBMS table is that the subclass definition provides a natural place to specify properties and behaviors that are specific to that table. Custom behaviors and validations may be implemented by -overriding the class methods, or by defining new methods. Common -queries, or simple queries that are useful as baseline for others, -can be stored in the $sql property array of -<classname>DB_Table</classname>. The use of an array syntax for -queries allows these queries to be modified relatively easily. +overriding the class methods, or by defining new methods. +<classname>DB_Table</classname> also allows commonly used or +baseline SELECT queries to be stored in the $sql property array. </para> <para> -The package provides two ways to create both a RDMBS table and a +The package provides two ways to create both a RDBMS table and a corresponding <classname>DB_Table</classname> subclass, without writing redundant specifications: <itemizedlist> @@ -238,7 +241,7 @@ In this tutorial, we demonstrate the former method, which requires us to write subclass definitions manually. A discussion of the latter method is given in the <classname>DB_Table_Generator</classname> class -tutorial, and in the API documentation for that class. +tutorial. </para> <refsect2 id="package.database.db-table.intro-table.extend.columns"> @@ -255,7 +258,7 @@ characters or digits. The 'decimal' type also requires a 'scope' element, which is the number of digits after the decimal point. A 'require' element with a boolean true value is equivalent to NOT NULL in SQL, and indicates -that no NULL values are allowed in that column. +that NULL values are not allowed in that column. </para> <para> @@ -353,18 +356,18 @@ <refsect2 id="package.database.db-table.intro-table.extend.indices"> <title>Defining Indices</title> <para> -The $idx property array is used to declare indices. Each index can be declared -to be of type 'primary', 'unique', or 'normal'. Primary and unique indices both -define constraints that require that each key value be unique within the table. -Normal indices are introduced purely for efficiency. Both single- and -multi-column indices may be defined. +The $idx property array is used to declare indices. Each index can be +declared to be of type 'primary', 'unique', or 'normal'. Primary and unique +indices both define constraints that require that each key value be unique +within the table. Normal indices are introduced purely for efficiency. +Both single- and multi-column indices may be defined. </para> <para> -In our GuestBook example, we want two single column indices: We will define -a primary index for the "id" identifier column. Also, for purposes of the -example, we'll define a normal index on the "signdate" column, because we -expect to search for visitors by date and time. The required $idx property +In our GuestBook example, we want two single column indices: First, we will +define a primary index for the "id" identifier column. Second, for purposes +of the example, we'll define a normal index on the "signdate" column, because +we expect to search for visitors by date and time. The required $idx property declaration is given below: <example> <title>Index Definitions for GuestBook</title> @@ -399,8 +402,19 @@ </para> <para> -You can also build multi-column indices. For example, if we wanted a -multi-column index on fname and lname called "namefl": +The key of each element in the $idx array is an index name, and the +value is generally an array containing a definition for the index. +An index definition array must contain a 'type' element, for which +the value must be 'primary', 'unique' or 'normal', and 'cols' +element. The value of 'cols' element must be either the name of a +single column, for a single-column array, or a sequential array of +column names, for a multi-column index. There is also an alternative +shorthand syntax for single-column indices, which is discussed below. +</para> + +<para> +As an example of a multi-column index, here is the definition for a +multi-column index named "namefl" on the fname and lname columns. <example> <title>Defining A Multi-Column Index</title> <programlisting role="php"> @@ -430,10 +444,11 @@ </para> <para> -Finally, there is a shorthand way to declare a single-column index: -If you want single-column index for which the index name is the -same as the column name, use the column name as the key of $idx, and -the index type as the value. +There is also a shorthand way to declare a single-column index: +If you want single-column index for which the index name is the same +as the column name, use the column name as the key of $idx, and the +index type string (rather than an index definition array) as the +value. <example> <title>Shorthand for Single-Column Indices:</title> <programlisting role="php"> @@ -471,8 +486,8 @@ column. This column is normally a primary key identifier. The only effect of this declaration is to change the behavior of the <function>insert</function> -method: If the <function>insert</function> method is used to insert a row in -which the value of an auto-increment column is NULL or is not specified, +method: If the <function>insert</function> method is used to insert a +row in which the value of an auto-increment column is not set or NULL, then an auto-incremented integer will be inserted automatically. </para> @@ -574,7 +589,7 @@ <itemizedlist> <listitem> <simpara> - boolean false to not attempt creation + boolean false to not attempt creation (default) </simpara> </listitem> <listitem> @@ -671,13 +686,12 @@ <classname>DB_Table</classname> can use sequences created by the underlying <classname>DB</classname> or <classname>MDB2</classname> layer to generate auto-increment integer identifiers. -If the value of a column that has been declared to be auto-increment -is not set or NULL in the array $row that is passed to the insert method, -then an auto-incremented sequence value will be generated and inserted -for that column. In the above example, a sequence value is generated -and inserted for the 'id' column. If a non-null integer value is -provided for such a column, however, the provided value will instead -be inserted. +If the value of a column that has been declared to be auto-increment is not +set or set to PHP NULL in the array of values that is passed to the insert +method, then an auto-incremented sequence value will be generated and +inserted for that column. In the above example, a sequence value is generated +and inserted for the 'id' column. If an integer value is set for such a +column, however, the provided value will instead be inserted. </para> <para> @@ -754,7 +768,7 @@ // assign the WHERE clause $where = "lname = 'Smith'"; -// attempt the update and print the results +// attempt the update $result = $GuestBook->update($values, $where); if (PEAR::isError($result)) { // ... error handling code ... @@ -828,9 +842,10 @@ <para> Queries may be submitted to the database using either the -<function>select</function> method, which generally returns the results -as an array, or the <function>selectResult</function> method, which returns -a <classname>DB</classname> or <classname>MDB2</classname> return set object. +<function>select</function> method, which generally returns the results as +an array, or the <function>selectResult</function> method, which returns a +<classname>DB_Result</classname> or <classname>MDB2_Result_Common</classname> +return set object. </para> <refsect2 id="package.database.db-table.intro-table.select.queryarray"> @@ -840,9 +855,9 @@ the allowed elements of a query array, the key is a lower case version of the keyword that begins a clause in the SELECT statement (e.g., 'select', 'from', 'where', etc.), and the value is the text of the remainder of -that clause, excluding the keyword. Exceptions to this rule are the -'get', 'fetchmode', and 'fetchmode_object_class' elements, which specify -how the results should be returned. The allowed keys of a query array are: +that clause, excluding the keyword. +The allowed keys of a query array that define clauses of the SELECT +command are: </para> <itemizedlist> <listitem> @@ -882,26 +897,6 @@ 'order' - the ORDER BY clause </simpara> </listitem> -<listitem> -<simpara> -'get' - determines how the <function>select</function> method will return - results. Has no effect on <function>selectResult</function>. See - below for a list of allowed values. (Defaults to 'all'). -</simpara> -</listitem> -<listitem> -<simpara> -'fetchmode' - determines how rows of a result set will be returned by - <function>select</function> if 'get' is 'all' or not set. (Has - no effect on <function>selectResult</function>). -</simpara> -</listitem> -<listitem> -<simpara> -'fetchmode_object_class' - The name of the class of the objects that should - be used to encapsulate rows if rows are returned as objects. -</simpara> -</listitem> </itemizedlist> <para> The values of the 'select', 'from', 'where', 'group', 'having', and @@ -914,9 +909,35 @@ </para> <para> -The value of the 'get' element, if present, modifies the form of the -value returned by the <function>select</function>. The allowed values -of the 'get' element are: +The other allowed keys of a query array may be used to specify how the +results of query should be returned by the <function>select</function> +method. These are: +<itemizedlist> + <listitem> + <simpara> + 'get' - determines how the <function>select</function> method will + return results. See below for a list of allowed values. + (Defaults to 'all'). + </simpara> + </listitem> + <listitem> + <simpara> + 'fetchmode' - determines how rows of a result set will be returned + by <function>select</function> if 'get' is 'all' or not set. + </simpara> + </listitem> + <listitem> + <simpara> + 'fetchmode_object_class' - The name of the class of the objects that + should be used to encapsulate rows if rows are returned as + objects. + </simpara> + </listitem> +</itemizedlist> +These three elements effect only the behavior of the +<function>select</function> method, and have no effect upon the +behavior of the <function>selectResult</function> method. The +allowed values of the 'get' element are: <itemizedlist> <listitem> <simpara> @@ -964,17 +985,18 @@ </para> <para> -When the 'get' element is set to 'all', explicitly or by default, each row -in the return set may be returned as an associative array in which keys are -column names, as a sequential array, or as an object in which column names -map to property names. When a query is submitted using the -<function>DB_Table::select</function> method, the choice can be controlled -by the value 'fetchmode' element of the query or (if no 'fetchmode' element -is set), by the $fetchmode property of the <classname>DB_Table</classname> -object. Similarly, when rows are returned as objects, the name of the -object class may be specified by the 'fetchmode_object_class' element -of the query or, if this is not set, by the $fetchmode_object_class -property of the <classname>DB_Table</classname> object. +When the 'get' element is set to 'all', explicitly or by default, each +row in the array returned by the <function>DB_Table::select</function> +method returned as an associative array in which keys are column names, +as a sequential array, or as an object in which column names map to +property names. The choice of data structure for each row may be +controlled by the 'fetchmode' element of the query or, if this is not +is set, by the $fetchmode property of the +<classname>DB_Table</classname> object. Similarly, when rows are +returned as objects, the name of the object class may be specified by +the 'fetchmode_object_class' element of the query or, if this is not +set, by the $fetchmode_object_class property of the +<classname>DB_Table</classname> object. </para> <para> @@ -985,9 +1007,11 @@ (for <classname>DB</classname>) or options (for <classname>MDB2</classname>) of the underlying <classname>DB</classname> or -<classname>MDB2</classname> object. The original values of the -<classname>DB/MDB2</classname> object properties/options are restored -before the <function>select</function> method returns. +<classname>MDB2</classname> object. These values should be set +equal to the desired DB_FETCHMODE_* or MDB2_FETCHMODE_* constant +appropriate for the underlying backend. The original values of the +<classname>DB/MDB2</classname> object properties or options are +restored before the <function>select</function> method returns. </para> <example> @@ -1015,8 +1039,8 @@ </simpara> </listitem> </itemizedlist> -In the following program listing, we declare these queries as elements of the -$sql property within the GuestBook_Table class definition. +In the following program snippet, we declare these queries as elements +of the $sql property within the GuestBook_Table class definition. </para> <programlisting role="php"> @@ -1061,19 +1085,19 @@ </refsect2> <refsect2 id="package.database.db-table.intro-table.select.submit"> -<title>Submitting Queries</title> +<title>Submitting Queries: <function>select*</function> Methods</title> <para> -The <function>select</function> method submits a query and returns -the result set as an array. The <function>selectResult</function> -returns the result as a <classname>DB_Result/MDB2_Result_*</classname> +The <function>select</function> method submits a query and returns the +result set as an array. The <function>selectResult</function> method +returns the result as a <classname>DB_Result/MDB2_Result_Common</classname> object. The <function>selectCount</function> method returns an integer equal to the number of rows that would be returned by a query, without returning the actual result set. </para> <para> -The interface is the same for all three of these methods. In each, the -first parameter, which is required, is the name of a stored query +The interface is the same for all three of these methods. In each, +the first parameter, which is required, is the name of a stored query definition, which is an array key in the $sql property array. The remaining parameters, which are all optional, may be used to modify the submitted query without modifying the stored query array. The second @@ -1093,15 +1117,14 @@ <![CDATA[ <?php -// [snip] create the $GuestBook GuestBook_Table object +// [snip] create a DB/MDB2 object and connect to the database +// [snip] create the GuestBook_Table object $GuestBook -// get and print 'list' view as an array +// Submit the stored 'list' query, which returns an array of rows $rows = $GuestBook->select('list'); -print_r($rows); -// get the 'emails' view as an sequential array of associative arrays -$result = $GuestBook->select('emails'); -print_r($result); +// Print the result set +print_r($rows); ?> ]]> @@ -1124,12 +1147,12 @@ $start = 7; $count = 12; -// as an array +// Return results as an array of rows $rows = $GuestBook->select($view, $filter, $order, $start, $count); print_r($rows); -// as a DB_Result object -$rows = $GuestBook->selectResult($view, $filter, $order, $start, $count); +// Return results as a DB_Result/MDB2_Result_Common object +$result = $GuestBook->selectResult($view, $filter, $order, $start, $count); print_r($result); ?> @@ -1140,9 +1163,9 @@ <para> To have <function>select</function> return rows as associative arrays, we must set either the 'fetchmode' element of a specific query array -or the $fetchmode property of the <classname>DB_Table</classname> object -to the DB_FETCHMODE_ASSOC or MDB2_FETCHMODE_ASSOC constant values, as -appropriate: +or the $fetchmode property of the <classname>DB_Table</classname> +object to the DB_FETCHMODE_ASSOC or MDB2_FETCHMODE_ASSOC constant +values, as appropriate: </para> <example> @@ -1165,12 +1188,12 @@ <para> To have <function>select</function> return rows as objects, with -properties that correspond to column names, we must set either the -'fetchmode' element of the query array or the $fetchmode property of -the <classname>DB_Table</classname> object to the DB_FETCHMODE_OBJECT -or MDB2_FETCHMODE_OBJECT constant. If no user-defined class is -specified, all rows are returned as instances of stdClass, as in -the following example: +properties that correspond to column names, we must set either +the 'fetchmode' element of the query array or the $fetchmode +property of the <classname>DB_Table</classname> object to the +DB_FETCHMODE_OBJECT or MDB2_FETCHMODE_OBJECT constant. If no +user-defined class is specified, all rows are returned as instances +of stdClass, as in the following example: </para> <example>
« previous php.pear.doc (#9756) next »