cvs: peardoc /en/package/structures structures-datagrid.xml /en/package/structures/structures-datagrid custom-datasources.xml
| From: | Olivier Guilyardi | Date: | Fri, 21 Apr 2006 15:29:02 +0000 |
| Subject: | cvs: peardoc /en/package/structures structures-datagrid.xml /en/package/structures/structures-datagrid custom-datasources.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-7805@lists.php.net to get a copy of this message | ||
olivierg Fri Apr 21 15:29:02 2006 UTC
Added files:
/peardoc/en/package/structures/structures-datagrid
custom-datasources.xml
Modified files:
/peardoc/en/package/structures structures-datagrid.xml
Log:
Added custom datasource drivers howto
http://cvs.php.net/viewcvs.cgi/peardoc/en/package/structures/structures-datagrid.xml?r1=1.23&r2=1.24&diff_format=u Index: peardoc/en/package/structures/structures-datagrid.xml diff -u peardoc/en/package/structures/structures-datagrid.xml:1.23 peardoc/en/package/structures/structures-datagrid.xml:1.24 --- peardoc/en/package/structures/structures-datagrid.xml:1.23 Fri Nov 4 22:45:48 2005 +++ peardoc/en/package/structures/structures-datagrid.xml Fri Apr 21 15:29:02 2006 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1" ?> -<!-- $Revision: 1.23 $ --> +<!-- $Revision: 1.24 $ --> <sect1 id="package.structures.structures-datagrid"> <title>Structures_DataGrid</title> @@ -29,6 +29,7 @@ &package.structures.structures-datagrid.example-simple; &package.structures.structures-datagrid.example-advanced; &package.structures.structures-datagrid.example-extending; + &package.structures.structures-datagrid.custom-datasources; <!-- DataGrid Documentation --> &package.structures.structures-datagrid.structures-datagrid; http://cvs.php.net/viewcvs.cgi/peardoc/en/package/structures/structures-datagrid/custom-datasources.xml?view=markup&rev=1.1 Index: peardoc/en/package/structures/structures-datagrid/custom-datasources.xml +++ peardoc/en/package/structures/structures-datagrid/custom-datasources.xml <?xml version="1.0" encoding="iso-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.structures.structures-datagrid.custom-datasources"> <refnamediv> <refname>Custom DataSources</refname> <refpurpose> How to write your own DataSource driver. </refpurpose> </refnamediv> <refsect1 id="package.structures.structures-datagrid.custom-datasources.intro"> <title>Introduction</title> <para>Writing your own <acronym>DataSource driver</acronym> is the way to go when none of the existing driver suit your needs. It is actually pretty easy, and allows for great flexibility.</para> <para>Of course, if you're trying to fetch data from an <emphasis>exotic source</emphasis>, writing your own driver is required. But, sometimes it's also the best way to achieve the best <emphasis>optimization</emphasis>, especially (but not only) with databases.</para> <para>This document will present you the <acronym>DataSource interface</acronym>, and how to implement it.</para> </refsect1> <refsect1 id="package.structures.structures-datagrid.custom-datasources.def"> <title>Definitions</title> <para>A <acronym>DataSource driver</acronym> is a descendent of the Structures_DataGrid_DataSource class, which implements the <acronym>DataSource interface</acronym>.</para> <para><acronym>DataSource</acronym> is a synomym for <acronym>DataSource driver</acronym>.</para> <para>The <acronym>DataSource interface</acronym> consists in a set of methods that drivers must or may overload and protected properties that drivers can use, as well as recommended practices.</para> <para>A <acronym>DataSource container</acronym> is a constant or a variable of any type (string, array, object, etc...) that either contains data or describes how to retrieve data.</para> <para>Every <acronym>DataSource driver</acronym> is specific to, and knows how to handle, a given <acronym>DataSource container</acronym> type.</para> </refsect1> <refsect1 id="package.structures.structures-datagrid.custom-datasources.interface"> <title>The DataSource interface</title> <refsect2 id="package.structures.structures-datagrid.custom-datasources.interface.methods"> <title>Methods that must or may be implemented in drivers</title> <!-- <example> <title>Using an SQL query as datasource</title> <programlisting role="php"> <![CDATA[ <?php require 'Structures/DataGrid.php'; // Instantiate the DataGrid $datagrid =& new Structures_DataGrid(); // Setup your database connection $options = array('dsn' => 'mysql://user:password@host/db_name'); // Bind a basic SQL statement as datasource $datagrid->bind("SELECT * FROM my_table",$options); // Print the DataGrid with the default renderer (HTML Table) $datagrid->render(); ?> ]]> </programlisting> </example> --> <para> <funcsynopsis> <funcprototype> <funcdef><function>constructor</function></funcdef> <void/> </funcprototype> </funcsynopsis> The constructor must set default options, if any, and call the parent constructor. This method is optional. </para> <para> <funcsynopsis> <funcprototype> <funcdef>object <function>bind</function></funcdef> <paramdef choice="req">mixed <parameter>container</parameter></paramdef> <paramdef choice="opt">array <parameter>options</parameter></paramdef> </funcprototype> </funcsynopsis> <function>bind</function> is reponsible for loading a DataSource <parameter>container</parameter> into the driver, according to some binding <parameter>options</parameter>. This method is optional. It must return a PEAR_Error object in case of failure. </para> <para> <funcsynopsis> <funcprototype> <funcdef>object <function>count</function></funcdef> <void/> </funcprototype> </funcsynopsis> <function>count</function> must return the total number or records found in the container. This method is required, and is always called before <function>fetch</function>. It must return a PEAR_Error object in case of failure. </para> <para> <funcsynopsis> <funcprototype> <funcdef>object <function>sort</function></funcdef> <paramdef choice="req">mixed <parameter>sortSpec</parameter></paramdef> <paramdef choice="opt">array <parameter>sortDir</parameter></paramdef> </funcprototype> </funcsynopsis> <function>sort</function> must sort the data according to sortSpec and the optional sortDir. This method is required, and is always called before <function>fetch</function>. It must return a PEAR_Error object in case of failure. </para> <para> <funcsynopsis> <funcprototype> <funcdef>mixed <function>fetch</function></funcdef> <paramdef choice="opt">integer <parameter>offset</parameter></paramdef> <paramdef choice="opt">integer <parameter>len</parameter></paramdef> </funcprototype> </funcsynopsis> <function>fetch</function> must return a 2-dimension array of data, starting from record <parameter>offset</parameter>, containing <parameter>len</parameter> records..This method is required It must return a PEAR_Error object in case of failure. </para> </refsect2> <refsect2 id="package.structures.structures-datagrid.custom-datasources.interface.properties"> <title>Properties available to drivers</title> <para><parameter>array $_options</parameter> - Data binding options as an associative array. You can read the content of this property but you shouldn't change it directly.</para> </refsect2> </refsect1> <refsect1> <title>A simple driver</title> <para>Let's start with a very simple driver. It is rather readable and you shouldn't have much trouble understanding it. It is not extremely useful to write a custom driver for a such simple SQL query, but it should get you started.</para> <example> <title>A simple SQL adaptor</title> <programlisting role="php"> <![CDATA[ <?php require 'Structures/DataGrid/DataSource.php'; require_once 'DB.php'; class MyDataSource extends Structures_DataGrid_DataSource { var $db; var $orderBy = ''; function MyDataSource () { $dsn = 'mysql://someuser:apasswd@localhost/thedb'; $db =& DB::connect($dsn); } function count() { $query = "SELECT * FROM animals WHERE species='cat'"; return $this->db->getOne($query); } function sort($sortSpec,$sortDir = 'ASC') { $this->orderBy = "ORDER BY $sortSpec $sortDir"; } function fetch($offset = 0, $len = null) { $limit = is_null($len) ? "LIMIT $offset,18446744073709551615" : "LIMIT $offset,$len"; $query = "SELECT * FROM animals WHERE species='cat' "; $query .= $this->_orderBy . " $limit"; return $this->db->getAll($query); } } ?> ]]> </programlisting> </example> </refsect1> <refsect1 id="package.structures.structures-datagrid.custom-datasources.test"> <title>Testing your driver</title> <para>Before going live, it is very recommended to test your driver with the <function>dump</function> method.</para> <example> <title>Testing with <function>dump</function></title> <programlisting role="php"> <![CDATA[ <?php $datasource = new MyDataSource(); $count = $datasource->count(); echo "There are $count cats in the farm\n\n"; $datasource->sort('weight'); echo "Here are the 5 lightest ones : \n"; // dump() accepts the same $offset and $len argument as fetch() $datasource->dump(0,5); ?> ]]> </programlisting> </example> <para>That should output a nicely formated ascii table like :</para> <screen> <![CDATA[ There are 23 cats in the farm. Here are the 5 lightest ones : +---------+---------+-----------+--------+ | name | species | birthDate | weight | +---------+---------+-----------+--------+ | sarge | cat | 20021220 | 1.8 | | etch | cat | 20000509 | 2.5 | | potato | cat | 19980128 | 3.8 | | sid | cat | 20011101 | 4.1 | | woody | cat | 19970712 | 6.0 | +---------+---------+-----------+--------+ ]]> </screen> </refsect1> <refsect1 id="package.structures.structures-datagrid.custom-datasources.use"> <title>Using your new driver</title> <para>Okay, so you have written a driver that's tailored to your needs, and tested it. It is now time to connect it to <acronym>Structures_DataGrid</acronym>.</para> <para>For this purpose we're going to use the <link linkend="package.structures.structures_datagrid.structures_datagrid.bindDataSource"><function>bindDataSource</function></link> method.</para> <example> <title>Binding a custom datasource</title> <programlisting role="php"> <![CDATA[ <?php $datagrid =& new Structures_DataGrid(); $datasource = new MyDataSource(); $datagrid->bindDataSource($datasource); $datagrid->render(); ?> ]]> </programlisting> </example> <para>That should output a sortable HTML table.</para> <para>Of course, the usual features of <acronym>Structures_DataGrid</acronym> are now available to you : paging, other output formats as XML, MS-Excel, etc...</para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
http://cvs.php.net/viewcvs.cgi/peardoc/en/package/structures/structures-datagrid.xml?r1=1.23&r2=1.24&diff_format=u Index: peardoc/en/package/structures/structures-datagrid.xml diff -u peardoc/en/package/structures/structures-datagrid.xml:1.23 peardoc/en/package/structures/structures-datagrid.xml:1.24 --- peardoc/en/package/structures/structures-datagrid.xml:1.23 Fri Nov 4 22:45:48 2005 +++ peardoc/en/package/structures/structures-datagrid.xml Fri Apr 21 15:29:02 2006 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1" ?> -<!-- $Revision: 1.23 $ --> +<!-- $Revision: 1.24 $ --> <sect1 id="package.structures.structures-datagrid"> <title>Structures_DataGrid</title> @@ -29,6 +29,7 @@ &package.structures.structures-datagrid.example-simple; &package.structures.structures-datagrid.example-advanced; &package.structures.structures-datagrid.example-extending; + &package.structures.structures-datagrid.custom-datasources; <!-- DataGrid Documentation --> &package.structures.structures-datagrid.structures-datagrid; http://cvs.php.net/viewcvs.cgi/peardoc/en/package/structures/structures-datagrid/custom-datasources.xml?view=markup&rev=1.1 Index: peardoc/en/package/structures/structures-datagrid/custom-datasources.xml +++ peardoc/en/package/structures/structures-datagrid/custom-datasources.xml <?xml version="1.0" encoding="iso-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.structures.structures-datagrid.custom-datasources"> <refnamediv> <refname>Custom DataSources</refname> <refpurpose> How to write your own DataSource driver. </refpurpose> </refnamediv> <refsect1 id="package.structures.structures-datagrid.custom-datasources.intro"> <title>Introduction</title> <para>Writing your own <acronym>DataSource driver</acronym> is the way to go when none of the existing driver suit your needs. It is actually pretty easy, and allows for great flexibility.</para> <para>Of course, if you're trying to fetch data from an <emphasis>exotic source</emphasis>, writing your own driver is required. But, sometimes it's also the best way to achieve the best <emphasis>optimization</emphasis>, especially (but not only) with databases.</para> <para>This document will present you the <acronym>DataSource interface</acronym>, and how to implement it.</para> </refsect1> <refsect1 id="package.structures.structures-datagrid.custom-datasources.def"> <title>Definitions</title> <para>A <acronym>DataSource driver</acronym> is a descendent of the Structures_DataGrid_DataSource class, which implements the <acronym>DataSource interface</acronym>.</para> <para><acronym>DataSource</acronym> is a synomym for <acronym>DataSource driver</acronym>.</para> <para>The <acronym>DataSource interface</acronym> consists in a set of methods that drivers must or may overload and protected properties that drivers can use, as well as recommended practices.</para> <para>A <acronym>DataSource container</acronym> is a constant or a variable of any type (string, array, object, etc...) that either contains data or describes how to retrieve data.</para> <para>Every <acronym>DataSource driver</acronym> is specific to, and knows how to handle, a given <acronym>DataSource container</acronym> type.</para> </refsect1> <refsect1 id="package.structures.structures-datagrid.custom-datasources.interface"> <title>The DataSource interface</title> <refsect2 id="package.structures.structures-datagrid.custom-datasources.interface.methods"> <title>Methods that must or may be implemented in drivers</title> <!-- <example> <title>Using an SQL query as datasource</title> <programlisting role="php"> <![CDATA[ <?php require 'Structures/DataGrid.php'; // Instantiate the DataGrid $datagrid =& new Structures_DataGrid(); // Setup your database connection $options = array('dsn' => 'mysql://user:password@host/db_name'); // Bind a basic SQL statement as datasource $datagrid->bind("SELECT * FROM my_table",$options); // Print the DataGrid with the default renderer (HTML Table) $datagrid->render(); ?> ]]> </programlisting> </example> --> <para> <funcsynopsis> <funcprototype> <funcdef><function>constructor</function></funcdef> <void/> </funcprototype> </funcsynopsis> The constructor must set default options, if any, and call the parent constructor. This method is optional. </para> <para> <funcsynopsis> <funcprototype> <funcdef>object <function>bind</function></funcdef> <paramdef choice="req">mixed <parameter>container</parameter></paramdef> <paramdef choice="opt">array <parameter>options</parameter></paramdef> </funcprototype> </funcsynopsis> <function>bind</function> is reponsible for loading a DataSource <parameter>container</parameter> into the driver, according to some binding <parameter>options</parameter>. This method is optional. It must return a PEAR_Error object in case of failure. </para> <para> <funcsynopsis> <funcprototype> <funcdef>object <function>count</function></funcdef> <void/> </funcprototype> </funcsynopsis> <function>count</function> must return the total number or records found in the container. This method is required, and is always called before <function>fetch</function>. It must return a PEAR_Error object in case of failure. </para> <para> <funcsynopsis> <funcprototype> <funcdef>object <function>sort</function></funcdef> <paramdef choice="req">mixed <parameter>sortSpec</parameter></paramdef> <paramdef choice="opt">array <parameter>sortDir</parameter></paramdef> </funcprototype> </funcsynopsis> <function>sort</function> must sort the data according to sortSpec and the optional sortDir. This method is required, and is always called before <function>fetch</function>. It must return a PEAR_Error object in case of failure. </para> <para> <funcsynopsis> <funcprototype> <funcdef>mixed <function>fetch</function></funcdef> <paramdef choice="opt">integer <parameter>offset</parameter></paramdef> <paramdef choice="opt">integer <parameter>len</parameter></paramdef> </funcprototype> </funcsynopsis> <function>fetch</function> must return a 2-dimension array of data, starting from record <parameter>offset</parameter>, containing <parameter>len</parameter> records..This method is required It must return a PEAR_Error object in case of failure. </para> </refsect2> <refsect2 id="package.structures.structures-datagrid.custom-datasources.interface.properties"> <title>Properties available to drivers</title> <para><parameter>array $_options</parameter> - Data binding options as an associative array. You can read the content of this property but you shouldn't change it directly.</para> </refsect2> </refsect1> <refsect1> <title>A simple driver</title> <para>Let's start with a very simple driver. It is rather readable and you shouldn't have much trouble understanding it. It is not extremely useful to write a custom driver for a such simple SQL query, but it should get you started.</para> <example> <title>A simple SQL adaptor</title> <programlisting role="php"> <![CDATA[ <?php require 'Structures/DataGrid/DataSource.php'; require_once 'DB.php'; class MyDataSource extends Structures_DataGrid_DataSource { var $db; var $orderBy = ''; function MyDataSource () { $dsn = 'mysql://someuser:apasswd@localhost/thedb'; $db =& DB::connect($dsn); } function count() { $query = "SELECT * FROM animals WHERE species='cat'"; return $this->db->getOne($query); } function sort($sortSpec,$sortDir = 'ASC') { $this->orderBy = "ORDER BY $sortSpec $sortDir"; } function fetch($offset = 0, $len = null) { $limit = is_null($len) ? "LIMIT $offset,18446744073709551615" : "LIMIT $offset,$len"; $query = "SELECT * FROM animals WHERE species='cat' "; $query .= $this->_orderBy . " $limit"; return $this->db->getAll($query); } } ?> ]]> </programlisting> </example> </refsect1> <refsect1 id="package.structures.structures-datagrid.custom-datasources.test"> <title>Testing your driver</title> <para>Before going live, it is very recommended to test your driver with the <function>dump</function> method.</para> <example> <title>Testing with <function>dump</function></title> <programlisting role="php"> <![CDATA[ <?php $datasource = new MyDataSource(); $count = $datasource->count(); echo "There are $count cats in the farm\n\n"; $datasource->sort('weight'); echo "Here are the 5 lightest ones : \n"; // dump() accepts the same $offset and $len argument as fetch() $datasource->dump(0,5); ?> ]]> </programlisting> </example> <para>That should output a nicely formated ascii table like :</para> <screen> <![CDATA[ There are 23 cats in the farm. Here are the 5 lightest ones : +---------+---------+-----------+--------+ | name | species | birthDate | weight | +---------+---------+-----------+--------+ | sarge | cat | 20021220 | 1.8 | | etch | cat | 20000509 | 2.5 | | potato | cat | 19980128 | 3.8 | | sid | cat | 20011101 | 4.1 | | woody | cat | 19970712 | 6.0 | +---------+---------+-----------+--------+ ]]> </screen> </refsect1> <refsect1 id="package.structures.structures-datagrid.custom-datasources.use"> <title>Using your new driver</title> <para>Okay, so you have written a driver that's tailored to your needs, and tested it. It is now time to connect it to <acronym>Structures_DataGrid</acronym>.</para> <para>For this purpose we're going to use the <link linkend="package.structures.structures_datagrid.structures_datagrid.bindDataSource"><function>bindDataSource</function></link> method.</para> <example> <title>Binding a custom datasource</title> <programlisting role="php"> <![CDATA[ <?php $datagrid =& new Structures_DataGrid(); $datasource = new MyDataSource(); $datagrid->bindDataSource($datasource); $datagrid->render(); ?> ]]> </programlisting> </example> <para>That should output a sortable HTML table.</para> <para>Of course, the usual features of <acronym>Structures_DataGrid</acronym> are now available to you : paging, other output formats as XML, MS-Excel, etc...</para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->