cvs: peardoc /en/package/database/mdb-querytool intro.xml
| From: | Elmar Pitschke | Date: | Wed, 17 Jan 2007 10:30:49 +0000 |
| Subject: | cvs: peardoc /en/package/database/mdb-querytool intro.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-9324@lists.php.net to get a copy of this message | ||
hugoki Wed Jan 17 10:30:49 2007 UTC
Modified files:
/peardoc/en/package/database/mdb-querytool intro.xml
Log:
- Added docs for class feature
http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb-querytool/intro.xml?r1=1.4&r2=1.5&diff_format=u
Index: peardoc/en/package/database/mdb-querytool/intro.xml
diff -u peardoc/en/package/database/mdb-querytool/intro.xml:1.4
peardoc/en/package/database/mdb-querytool/intro.xml:1.5
--- peardoc/en/package/database/mdb-querytool/intro.xml:1.4 Mon Apr 4 16:32:27 2005
+++ peardoc/en/package/database/mdb-querytool/intro.xml Wed Jan 17 10:30:49 2007
@@ -1,4 +1,4 @@
-<!-- $Revision: 1.4 $ -->
+<!-- $Revision: 1.5 $ -->
<refentry id="package.database.mdb-querytool.intro">
<refnamediv>
<refname>Introduction</refname>
@@ -29,7 +29,7 @@
</refsect1>
<refsect1 id="package.database.mdb-querytool.intro.example1">
- <title>Example 1</title>
+ <title>Example 1 - Arrays</title>
<para>
The best way to use <classname>MDB_QueryTool</classname> is creating
a class that extends it. Here's a sample usage:
@@ -120,6 +120,105 @@
</programlisting>
</para>
</refsect1>
+ <refsect1 id="package.database.mdb-querytool.intro.example2">
+ <title>Example 2 - Classes</title>
+ <para>
+ <classname>MDB_QueryTool</classname> also offers working with
+ classes. 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);
+$car->useResult('object');
+
+//get the car #3
+$car->reset(); // reset the query-builder, so no where, order, etc. are set
+$res = $car->get(3)->fetchRow();
+var_dump($res);
+
+//get all the cars
+$car->reset(); // reset the query-builder, so no where, order, etc. are set
+$cars = $car->getAll();
+while ($res = $cars->getNext()) {
+ var_dump($res);
+}
+
+// get the first 10 cars
+$car->reset(); // reset the query-builder, so no where, order, etc. are set
+$cars = $car->getAll(0, 10);
+while ($res = $cars->getNext()) {
+ 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');
+$cars = $car->getAll();
+while ($res = $cars->getNext()) {
+ var_dump($res);
+}
+
+//add a new car to the database
+$newCar = $car->newEntity();
+$newCar->model = 'Super Trooper';
+$newCar->hp = 140;
+$newCar->color = 'black';
+$newCar->clima = 0;
+$newCar->price = 19000;
+
+$newCarId = $newCar->save();
+var_dump($newCarId);
+
+//update an existing car
+$car->reset();
+$res = $car->get($newCarId)->fetchRow();
+$res->clima = 1;
+$res->price = 20000;
+$res->save();
+var_dump($res);
+
+//remove the car from the database
+$car->reset();
+$res = $car->get($newCarId)->fetchRow();
+var_dump($res->remove());
+unset($res);
+]]>
+ </programlisting>
+ </para>
+ </refsect1>
</refentry>