cvs: peardoc /en/package/database/db-dataobject intro-purpose.xml
| From: | Christian Weiske | Date: | Sun, 09 Mar 2008 22:09:39 +0000 |
| Subject: | cvs: peardoc /en/package/database/db-dataobject intro-purpose.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-10564@lists.php.net to get a copy of this message | ||
cweiske Sun Mar 9 22:09:39 2008 UTC
Modified files:
/peardoc/en/package/database/db-dataobject intro-purpose.xml
Log:
make initial db_do examples work
http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/db-dataobject/intro-purpose.xml?r1=1.19&r2=1.20&diff_format=u Index: peardoc/en/package/database/db-dataobject/intro-purpose.xml diff -u peardoc/en/package/database/db-dataobject/intro-purpose.xml:1.19 peardoc/en/package/database/db-dataobject/intro-purpose.xml:1.20 --- peardoc/en/package/database/db-dataobject/intro-purpose.xml:1.19 Thu Jan 10 19:07:15 2008 +++ peardoc/en/package/database/db-dataobject/intro-purpose.xml Sun Mar 9 22:09:39 2008 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.19 $ --> +<!-- $Revision: 1.20 $ --> <refentry id="package.database.db-dataobject.intro-purpose"> <refnamediv> <refname>Introduction</refname> @@ -8,7 +8,7 @@ </refnamediv> <refsect1> <title>Introduction</title> - + <warning> <para> Zend Optimizer: due to a bug in the optimizer, you will have to either reduce the @@ -19,9 +19,9 @@ (It works OK in PHP5), If you need pass-by-reference, define the constant DB_DATAOBJECT_NO_OVERLOAD = 0 </para> </warning> - + <para> - <classname>DB_DataObject</classname> is a SQL Builder and Data Modeling Layer + <classname>DB_DataObject</classname> is a SQL Builder and Data Modeling Layer built on top of PEAR::&class.db;. Its main purpose is to </para> <para> @@ -37,149 +37,177 @@ </listitem> </itemizedlist> </para> - + <para> - So what does that mean in english? Well, if you look around at some of the better written PHP - applications and frameworks out there, you will notice a common approach to using classes to - wrap access to database tables or groups. The prime example of this is the person object, which would + So what does that mean in english? Well, if you look around at some of the better written PHP + applications and frameworks out there, you will notice a common approach to using classes to + wrap access to database tables or groups. The prime example of this is the person object, which would frequently look something like this. - - <example> + + <example> <title>A Classic Data Object or Container</title> <programlisting role="php"> <![CDATA[ - - class MyPerson { - - // gets an array of data about the seleted person - function getPerson($id) { - global $db; - $result = $db->query("SELECT * FROM person WHERE id=$id"); - return $result->fetchRow(); - } - - // example of checking a password. - function checkPassword($username,$password) { - global $db; - $result = $db->query("SELECT username FROM person WHERE username='$username' AND password = '$password'"); - return $result->fetchRow(); - } - - } - - // get the persons details.. - $array = MyPerson::getPerson(12); +<?php +require_once 'DB.php'; +$db = DB::connect('mysql://someuser:somepass@localhost/pear_dbdo'); +$db->setFetchMode(DB_FETCHMODE_ASSOC); + +class MyPerson +{ + + // gets an array of data about the seleted person + function getPerson($id) + { + global $db; + $result = $db->query('SELECT * FROM person WHERE id=' . $db->quote($id)); + return $result->fetchRow(); + } - - ]]> + // example of checking a password. + function checkPassword($username, $password) + { + global $db; + + $hashed = md5($password); + $result = $db->query( + 'SELECT name FROM person WHERE name=' . $db->quote($username) + . ' AND password = ' . $db->quote($hashed) + ); + return $result->fetchRow(); + } + +} + +// get the persons details.. +$array = MyPerson::getPerson(12); +echo $array['name'] . "\n"; +?> ]]> </programlisting></example> - </para> + + The examples operate on the following SQL database table: + <programlisting role="sql"><![CDATA[ +CREATE TABLE IF NOT EXISTS `person` ( + `id` int(11) NOT NULL auto_increment, + `name` varchar(64) NOT NULL, + `password` varchar(32) NOT NULL, + `birthDate` date NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ; + +INSERT INTO `person` (`id`, `name`, `password`, `birthDate`) VALUES +(12, 'John', '098f6bcd4621d373cade4e832627b4f6', '1984-02-23'); +]]></programlisting> + </para> <para> - The key benefit of this approach is that you are grouping similar actions on a single table in one place, and are + The key benefit of this approach is that you are grouping similar actions on a single table in one place, and are more likely to spot duplicated code (eg. two methods that do similar things). You will also notice the global $db variable used here - the fact is that most of the time you will use a common database connection for all your classes, so how should this be dealt with? </para> <para> The next step on this road is to use the objects variables as a storage mechanism. - + <example> <title>A Classic Data Object or Container</title> <programlisting role="php"> <![CDATA[ - - class MyPerson { - - var $id; - var $name; - var $birthDate; - - - // gets an array of data about the seleted person - function get($id) { - global $db; - $result = $db->query("SELECT * FROM person WHERE id=$id"); - $array = $result->fetchRow(); - foreach($array as $key=>$value) { - $this->$key = $value; - } - } - - function getAge() { - return date('Y') - date('Y',$this->birthDate); - } - - } - // now get the person and display the age. - $person = new MyPerson; - $person->get(12); - echo "{$person->name} is ". $person->getAge() . " years old"; - - - ]]> +<?php +require_once 'DB.php'; +$db = DB::connect('mysql://someuser:somepass@localhost/pear_dbdo'); +$db->setFetchMode(DB_FETCHMODE_ASSOC); + +class MyPerson +{ + var $id; + var $name; + var $birthDate; + + // gets an array of data about the seleted person + function get($id) { + global $db; + $result = $db->query('SELECT * FROM person WHERE id=' . $db->quote($id)); + $array = $result->fetchRow(); + foreach ($array as $key => $value) { + $this->$key = $value; + } + } + + function getAge() { + return date('Y') - date('Y', strtotime($this->birthDate)); + } + +} +// now get the person and display the age. +$person = new MyPerson(); +$person->get(12); +echo "{$person->name} is ". $person->getAge() . " years old\n"; +?> ]]> </programlisting></example> </para> <para> - As you can see, the current row of data is now stored in the Data Container, and additional methods can be added to manipulate + As you can see, the current row of data is now stored in the Data Container, and additional methods can be added to manipulate the data in the object or even call other related objects (eg. tables relationships in databases) </para> - + <para> As a next step, why not utilize the member variables to perform searches or gets on the database. - - - - + + + + <example> <title>A Classic Data Object or Container</title> <programlisting role="php"> <![CDATA[ - - class MyPerson { - - var $id; - var $name; - var $birthDate; - - - // does the query based on the value of $this->name - function find() { - global $db; - $this->result = $db->query("SELECT * FROM person WHERE name={$this->name}"); - } - - // fetches a row of data and sets the object variables to match it. - - function fetch() { - $array = $this->result->fetchRow(); - if (empty($array)) { - return false; - } - foreach($array as $key=>$value) { - $this->$key = $value; - } - } - - } - // now get the person and display the age. - $person = new MyPerson; - $person->name = "John"; - $person->find(); - while ($person->fetch()) { - echo "a {$person->name} has a birthday on {$person->birthDate}<BR>"; - } - - - ]]> +<?php +require_once 'DB.php'; +$db = DB::connect('mysql://someuser:somepass@localhost/pear_dbdo'); +$db->setFetchMode(DB_FETCHMODE_ASSOC); + +class MyPerson +{ + var $id; + var $name; + var $birthDate; + + // does the query based on the value of $this->name + function find() { + global $db; + $this->result = $db->query('SELECT * FROM person WHERE name=' . $db->quote($this->name)); + } + + // fetches a row of data and sets the object variables to match it. + function fetch() { + $array = $this->result->fetchRow(); + if (empty($array)) { + return false; + } + foreach ($array as $key=>$value) { + $this->$key = $value; + } + return true; + } + +} + +// now get the person and display the age. +$person = new MyPerson(); +$person->name = "John"; +$person->find(); +while ($person->fetch()) { + echo "a {$person->name} has a birthday on {$person->birthDate}<br/>\n"; +} +?> ]]> </programlisting></example> - </para> + </para> <para> - As you can see, by assigning values to the object before the find method is called, you can set conditions for the query. - <classname>DB_DataObject</classname>s behaves in a similar way to this, however, you can also add more conditions with the whereAdd() method, or + As you can see, by assigning values to the object before the find method is called, you can set conditions for the query. + <classname>DB_DataObject</classname>s behaves in a similar way to this, however, you can also add more conditions with the whereAdd() method, or restrict the selection using the selectAdd() method. </para> - - + + <para> Obviously you can carry on down this road and create lots of little containers for each table in your database, and all the code will nicely relate to each table. However, you can see from the examples above that all classes @@ -220,28 +248,28 @@ <simpara> Simple to integrate with other packages, with setFrom() and toArray() methods</simpara> </listitem> </itemizedlist> - - + + </para> - - + + <para> So what do my classes look like? <example> <title>At last some real DataObject Code..</title> <programlisting role="php"> <![CDATA[ - - + + // this is the common configuration code - place in a general site wide include file. - - // this the code used to load and store DataObjects Configuration. + + // this the code used to load and store DataObjects Configuration. $options = &PEAR::getStaticProperty('DB_DataObject','options'); - + // the simple examples use parse_ini_file, which is fast and efficient. // however you could as easily use wddx, xml or your own configuration array. $config = parse_ini_file('example.ini',TRUE); - + // because PEAR::getstaticProperty was called with and & (get by reference) // this will actually set the variable inside that method (a quasi static variable) $options = $config['DB_DataObject']; @@ -252,30 +280,30 @@ // this is normally contained in your DataObjects file (autogenerated by the generator) require_once 'DB/DataObject.php'; - - + + // by extending the base class DB_DataObject - you inherit all the common methods defined in it. - + class DataObjects_Person extends DB_DataObject { - + var $id; // this is a primary id (it's specified in a config file - explained later) var $name; var $friend; - + // this is a simple function to get the persons friends..? - + function getFriends() { - + $personObject = $this->factory('person'); - + // look for all people with their friend number matching this persons id. $personObject->friend = $this->id; - + // do the select query. $personObject->find(); $array = array(); - + // fetch the results into the object. while ($personObject->fetch()) { // use the clone to copy - not really needed but get used to it for PHP5 @@ -284,32 +312,32 @@ } // return the results. return $array; - + } } - - - - // and this goes on your display code - + + + + // and this goes on your display code + // create a new person class.. $person = DB_DataObject::Factory('person'); - + // get the person using the primary key. $person->get(12); // get the friends. $friends = $person->getFriends(); - + // DB_DataObjects is designed to make print_r useable to debug your applications. - print_r($friends); + print_r($friends); ]]> </programlisting></example> - + </para> - - - + + + <para> The above example illustrates the components of the <classname>DB_DataObject</classname>, by setting the options, all @@ -317,7 +345,7 @@ how to access the database. (multiple databases are supported - see section on configuration) </para> <para> - The class definition illustrates how you only need to define the data specific code in your class, ignoring + The class definition illustrates how you only need to define the data specific code in your class, ignoring all the common methods, along with showing one of the methods for retrieveing multiple rows of data. </para> <para> @@ -327,7 +355,7 @@ from the query. </para> <para> - + </para> <example> <title> In the above example, this query would be performed.</title> @@ -338,20 +366,20 @@ </programlisting> </example> - + <example> <title> To make a change to the Database you would just change the value of the objects variables and call the update method.</title> <programlisting role="php"> <![CDATA[ - + $person = DB_DataObject::factory('person'); $person->get(12); $person->name = 'Fred'; $person->update(); - + ]]> </programlisting> @@ -360,8 +388,8 @@ As a general rule of thumb method names are usually the same as the SQL statement they relate to. </para> - - + + </refsect1> </refentry> <!-- Keep this comment at the end of the file
http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/db-dataobject/intro-purpose.xml?r1=1.19&r2=1.20&diff_format=u Index: peardoc/en/package/database/db-dataobject/intro-purpose.xml diff -u peardoc/en/package/database/db-dataobject/intro-purpose.xml:1.19 peardoc/en/package/database/db-dataobject/intro-purpose.xml:1.20 --- peardoc/en/package/database/db-dataobject/intro-purpose.xml:1.19 Thu Jan 10 19:07:15 2008 +++ peardoc/en/package/database/db-dataobject/intro-purpose.xml Sun Mar 9 22:09:39 2008 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.19 $ --> +<!-- $Revision: 1.20 $ --> <refentry id="package.database.db-dataobject.intro-purpose"> <refnamediv> <refname>Introduction</refname> @@ -8,7 +8,7 @@ </refnamediv> <refsect1> <title>Introduction</title> - + <warning> <para> Zend Optimizer: due to a bug in the optimizer, you will have to either reduce the @@ -19,9 +19,9 @@ (It works OK in PHP5), If you need pass-by-reference, define the constant DB_DATAOBJECT_NO_OVERLOAD = 0 </para> </warning> - + <para> - <classname>DB_DataObject</classname> is a SQL Builder and Data Modeling Layer + <classname>DB_DataObject</classname> is a SQL Builder and Data Modeling Layer built on top of PEAR::&class.db;. Its main purpose is to </para> <para> @@ -37,149 +37,177 @@ </listitem> </itemizedlist> </para> - + <para> - So what does that mean in english? Well, if you look around at some of the better written PHP - applications and frameworks out there, you will notice a common approach to using classes to - wrap access to database tables or groups. The prime example of this is the person object, which would + So what does that mean in english? Well, if you look around at some of the better written PHP + applications and frameworks out there, you will notice a common approach to using classes to + wrap access to database tables or groups. The prime example of this is the person object, which would frequently look something like this. - - <example> + + <example> <title>A Classic Data Object or Container</title> <programlisting role="php"> <![CDATA[ - - class MyPerson { - - // gets an array of data about the seleted person - function getPerson($id) { - global $db; - $result = $db->query("SELECT * FROM person WHERE id=$id"); - return $result->fetchRow(); - } - - // example of checking a password. - function checkPassword($username,$password) { - global $db; - $result = $db->query("SELECT username FROM person WHERE username='$username' AND password = '$password'"); - return $result->fetchRow(); - } - - } - - // get the persons details.. - $array = MyPerson::getPerson(12); +<?php +require_once 'DB.php'; +$db = DB::connect('mysql://someuser:somepass@localhost/pear_dbdo'); +$db->setFetchMode(DB_FETCHMODE_ASSOC); + +class MyPerson +{ + + // gets an array of data about the seleted person + function getPerson($id) + { + global $db; + $result = $db->query('SELECT * FROM person WHERE id=' . $db->quote($id)); + return $result->fetchRow(); + } - - ]]> + // example of checking a password. + function checkPassword($username, $password) + { + global $db; + + $hashed = md5($password); + $result = $db->query( + 'SELECT name FROM person WHERE name=' . $db->quote($username) + . ' AND password = ' . $db->quote($hashed) + ); + return $result->fetchRow(); + } + +} + +// get the persons details.. +$array = MyPerson::getPerson(12); +echo $array['name'] . "\n"; +?> ]]> </programlisting></example> - </para> + + The examples operate on the following SQL database table: + <programlisting role="sql"><![CDATA[ +CREATE TABLE IF NOT EXISTS `person` ( + `id` int(11) NOT NULL auto_increment, + `name` varchar(64) NOT NULL, + `password` varchar(32) NOT NULL, + `birthDate` date NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ; + +INSERT INTO `person` (`id`, `name`, `password`, `birthDate`) VALUES +(12, 'John', '098f6bcd4621d373cade4e832627b4f6', '1984-02-23'); +]]></programlisting> + </para> <para> - The key benefit of this approach is that you are grouping similar actions on a single table in one place, and are + The key benefit of this approach is that you are grouping similar actions on a single table in one place, and are more likely to spot duplicated code (eg. two methods that do similar things). You will also notice the global $db variable used here - the fact is that most of the time you will use a common database connection for all your classes, so how should this be dealt with? </para> <para> The next step on this road is to use the objects variables as a storage mechanism. - + <example> <title>A Classic Data Object or Container</title> <programlisting role="php"> <![CDATA[ - - class MyPerson { - - var $id; - var $name; - var $birthDate; - - - // gets an array of data about the seleted person - function get($id) { - global $db; - $result = $db->query("SELECT * FROM person WHERE id=$id"); - $array = $result->fetchRow(); - foreach($array as $key=>$value) { - $this->$key = $value; - } - } - - function getAge() { - return date('Y') - date('Y',$this->birthDate); - } - - } - // now get the person and display the age. - $person = new MyPerson; - $person->get(12); - echo "{$person->name} is ". $person->getAge() . " years old"; - - - ]]> +<?php +require_once 'DB.php'; +$db = DB::connect('mysql://someuser:somepass@localhost/pear_dbdo'); +$db->setFetchMode(DB_FETCHMODE_ASSOC); + +class MyPerson +{ + var $id; + var $name; + var $birthDate; + + // gets an array of data about the seleted person + function get($id) { + global $db; + $result = $db->query('SELECT * FROM person WHERE id=' . $db->quote($id)); + $array = $result->fetchRow(); + foreach ($array as $key => $value) { + $this->$key = $value; + } + } + + function getAge() { + return date('Y') - date('Y', strtotime($this->birthDate)); + } + +} +// now get the person and display the age. +$person = new MyPerson(); +$person->get(12); +echo "{$person->name} is ". $person->getAge() . " years old\n"; +?> ]]> </programlisting></example> </para> <para> - As you can see, the current row of data is now stored in the Data Container, and additional methods can be added to manipulate + As you can see, the current row of data is now stored in the Data Container, and additional methods can be added to manipulate the data in the object or even call other related objects (eg. tables relationships in databases) </para> - + <para> As a next step, why not utilize the member variables to perform searches or gets on the database. - - - - + + + + <example> <title>A Classic Data Object or Container</title> <programlisting role="php"> <![CDATA[ - - class MyPerson { - - var $id; - var $name; - var $birthDate; - - - // does the query based on the value of $this->name - function find() { - global $db; - $this->result = $db->query("SELECT * FROM person WHERE name={$this->name}"); - } - - // fetches a row of data and sets the object variables to match it. - - function fetch() { - $array = $this->result->fetchRow(); - if (empty($array)) { - return false; - } - foreach($array as $key=>$value) { - $this->$key = $value; - } - } - - } - // now get the person and display the age. - $person = new MyPerson; - $person->name = "John"; - $person->find(); - while ($person->fetch()) { - echo "a {$person->name} has a birthday on {$person->birthDate}<BR>"; - } - - - ]]> +<?php +require_once 'DB.php'; +$db = DB::connect('mysql://someuser:somepass@localhost/pear_dbdo'); +$db->setFetchMode(DB_FETCHMODE_ASSOC); + +class MyPerson +{ + var $id; + var $name; + var $birthDate; + + // does the query based on the value of $this->name + function find() { + global $db; + $this->result = $db->query('SELECT * FROM person WHERE name=' . $db->quote($this->name)); + } + + // fetches a row of data and sets the object variables to match it. + function fetch() { + $array = $this->result->fetchRow(); + if (empty($array)) { + return false; + } + foreach ($array as $key=>$value) { + $this->$key = $value; + } + return true; + } + +} + +// now get the person and display the age. +$person = new MyPerson(); +$person->name = "John"; +$person->find(); +while ($person->fetch()) { + echo "a {$person->name} has a birthday on {$person->birthDate}<br/>\n"; +} +?> ]]> </programlisting></example> - </para> + </para> <para> - As you can see, by assigning values to the object before the find method is called, you can set conditions for the query. - <classname>DB_DataObject</classname>s behaves in a similar way to this, however, you can also add more conditions with the whereAdd() method, or + As you can see, by assigning values to the object before the find method is called, you can set conditions for the query. + <classname>DB_DataObject</classname>s behaves in a similar way to this, however, you can also add more conditions with the whereAdd() method, or restrict the selection using the selectAdd() method. </para> - - + + <para> Obviously you can carry on down this road and create lots of little containers for each table in your database, and all the code will nicely relate to each table. However, you can see from the examples above that all classes @@ -220,28 +248,28 @@ <simpara> Simple to integrate with other packages, with setFrom() and toArray() methods</simpara> </listitem> </itemizedlist> - - + + </para> - - + + <para> So what do my classes look like? <example> <title>At last some real DataObject Code..</title> <programlisting role="php"> <![CDATA[ - - + + // this is the common configuration code - place in a general site wide include file. - - // this the code used to load and store DataObjects Configuration. + + // this the code used to load and store DataObjects Configuration. $options = &PEAR::getStaticProperty('DB_DataObject','options'); - + // the simple examples use parse_ini_file, which is fast and efficient. // however you could as easily use wddx, xml or your own configuration array. $config = parse_ini_file('example.ini',TRUE); - + // because PEAR::getstaticProperty was called with and & (get by reference) // this will actually set the variable inside that method (a quasi static variable) $options = $config['DB_DataObject']; @@ -252,30 +280,30 @@ // this is normally contained in your DataObjects file (autogenerated by the generator) require_once 'DB/DataObject.php'; - - + + // by extending the base class DB_DataObject - you inherit all the common methods defined in it. - + class DataObjects_Person extends DB_DataObject { - + var $id; // this is a primary id (it's specified in a config file - explained later) var $name; var $friend; - + // this is a simple function to get the persons friends..? - + function getFriends() { - + $personObject = $this->factory('person'); - + // look for all people with their friend number matching this persons id. $personObject->friend = $this->id; - + // do the select query. $personObject->find(); $array = array(); - + // fetch the results into the object. while ($personObject->fetch()) { // use the clone to copy - not really needed but get used to it for PHP5 @@ -284,32 +312,32 @@ } // return the results. return $array; - + } } - - - - // and this goes on your display code - + + + + // and this goes on your display code + // create a new person class.. $person = DB_DataObject::Factory('person'); - + // get the person using the primary key. $person->get(12); // get the friends. $friends = $person->getFriends(); - + // DB_DataObjects is designed to make print_r useable to debug your applications. - print_r($friends); + print_r($friends); ]]> </programlisting></example> - + </para> - - - + + + <para> The above example illustrates the components of the <classname>DB_DataObject</classname>, by setting the options, all @@ -317,7 +345,7 @@ how to access the database. (multiple databases are supported - see section on configuration) </para> <para> - The class definition illustrates how you only need to define the data specific code in your class, ignoring + The class definition illustrates how you only need to define the data specific code in your class, ignoring all the common methods, along with showing one of the methods for retrieveing multiple rows of data. </para> <para> @@ -327,7 +355,7 @@ from the query. </para> <para> - + </para> <example> <title> In the above example, this query would be performed.</title> @@ -338,20 +366,20 @@ </programlisting> </example> - + <example> <title> To make a change to the Database you would just change the value of the objects variables and call the update method.</title> <programlisting role="php"> <![CDATA[ - + $person = DB_DataObject::factory('person'); $person->get(12); $person->name = 'Fred'; $person->update(); - + ]]> </programlisting> @@ -360,8 +388,8 @@ As a general rule of thumb method names are usually the same as the SQL statement they relate to. </para> - - + + </refsect1> </refentry> <!-- Keep this comment at the end of the file