cvs: peardoc /en/package/database/mdb-querytool intro.xml

From: Date: Wed, 16 Feb 2005 20:02:57 +0000
Subject: cvs: peardoc /en/package/database/mdb-querytool intro.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-5539@lists.php.net to get a copy of this message
quipo Wed Feb 16 15:02:57 2005 EDT Modified files: /peardoc/en/package/database/mdb-querytool intro.xml Log: add an usage example http://cvs.php.net/diff.php/peardoc/en/package/database/mdb-querytool/intro.xml?r1=1.1&r2=1.2&ty=u Index: peardoc/en/package/database/mdb-querytool/intro.xml diff -u peardoc/en/package/database/mdb-querytool/intro.xml:1.1 peardoc/en/package/database/mdb-querytool/intro.xml:1.2 --- peardoc/en/package/database/mdb-querytool/intro.xml:1.1 Thu Apr 29 18:26:10 2004 +++ peardoc/en/package/database/mdb-querytool/intro.xml Wed Feb 16 15:02:54 2005 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.1 $ --> +<!-- $Revision: 1.2 $ --> <refentry id="package.database.mdb-querytool.intro"> <refnamediv> <refname>Introduction</refname> @@ -6,29 +6,123 @@ An OO-interface for easily retreiving and modifying data in a DB </refpurpose> </refnamediv> - <refsect1> + <refsect1 id="package.database.mdb-querytool.intro.description"> <title>MDB_QueryTool Description</title> <para> - This package is an OO-abstraction to the SQL-Query language, it provides methods such - as setWhere, setOrder, setGroup, setJoin, etc. to easily build queries. - It also provides an easy to learn interface that interacts nicely with HTML-forms using - arrays that contain the column data, that shall be updated/added in a DB. - This package bases on an SQL-Builder which lets you easily build - SQL-Statements and execute them. - It supports all the db engines supported by <classname>MDB</classname>. + This package is an OO-abstraction to the SQL-Query language, it provides + methods such as setWhere, setOrder, setGroup, setJoin, etc. to easily + build queries. + It also provides an easy to learn interface that interacts nicely with + HTML-forms using arrays that contain the column data, that shall be + updated/added in a DB. + This package bases on an SQL-Builder which lets you easily build + SQL-Statements and execute them. + It supports all the db engines supported by <classname>MDB</classname>. </para> -<para> - Since it's a 1:1 port of <classname>DB_QueryTool</classname>, - it has the same API, the only difference being the class name - (and the constructor name, of course). You can see the - <ulink url="http://opensource.visionp.biz/index.php?id=78"> - online documentation for DB_QueryTool here</ulink> - </para> - </refsect1> - </refentry> + <para> + Since it's a 1:1 port of <classname>DB_QueryTool</classname>, + it has the same API, the only difference being the class name + (and the constructor name, of course). You can see the + <ulink url="http://opensource.visionp.biz/index.php?id=78"> + online documentation for DB_QueryTool here</ulink> + </para> + </refsect1> + + <refsect1 id="package.database.mdb-querytool.intro.example1"> + <title>Example 1</title> + <para> + The best way to use <classname>MDB_QueryTool</classname> is creating + a class that extends it. Here's a sample usage: + <programlisting role="php"> +<![CDATA[ +require_once 'MDB/QueryTool.php'; +define('TABLE_CARS', 'cars'); +$dsn = 'mysql://user:pass@host/dbname'; + +/** + * Let's suppose the "car" table has the following fields: + * (id, model, hp, color, clima, price) + */ +class Car extends MDB_QueryTool +{ + var $table = TABLE_CARS; + var $sequenceName = TABLE_CARS; + + // this is default, but to demonstrate it here ... + var $primaryCol = 'id'; + + /** + * This table spec assigns a short name to a table name + * this short name is needed in case the table name changes + * i.e. when u put the application on a provider's db, where you have to + * prefix each table, and you dont need to change the entire application to + * where you refer to joined table columns, for that joined results the + * short name is used instead of the table name + */ + var $tableSpec = array( + array('name' => TABLE_CARS, 'shortName' => 'cars'), + //array('name' => TABLE_TIME, 'shortName' => 'time'), + ); +} + +//instanciate an object of the Car class +$car = new Car($dsn); +//get the car #3 +$car->reset(); // reset the query-builder, so no where, order, etc. are set +$res = $car->get(3); +var_dump($res); + +//get all the cars +$car->reset(); // reset the query-builder, so no where, order, etc. are set +$res = $car->getAll(); +var_dump($res); + +// get the first 10 cars +$car->reset(); // reset the query-builder, so no where, order, etc. are set +$res = $car->getAll(0, 10); +var_dump($res); + +//get all the red cars with clima, sorted by price +$car->reset(); +$car->setWhere('color="red"'); +$car->setWhere('clima=1'); +$car->setOrder('price'); +$res = $car->getAll(); +var_dump($res); + +//add a new car to the database +$data = array( + 'model' => 'Super Trooper', + 'hp' => 140, + 'color' => 'black', + 'clima' => 0, + 'price' => 19000 +); +$newCarId = $car->save($data); +var_dump($newCarId); + +//update an existing car +$data = array( + 'id' => $newCarId, + 'clima' => 1, + 'price' => 20000, +); +$res = $car->save($data); +var_dump($res); +//equivalent to $car->update($data); + +//remove the car from the database +$res = $car->remove($newCarId); +var_dump($res); +]]> + </programlisting> + </para> + </refsect1> + + </refentry> <!-- Keep this comment at the end of the file Local variables:

« previous php.pear.doc (#5539) next »