server modifications for nfc flash chat client for nfc phpSorm - PHP Simple Object Relational Mapping

phpSorm - PHP Simple Object Relational Mapping

UPDATE 29. Sept. 03: first public release

phpSorm is mainly a base-class which provides a simple way to map a table from a relational database to a PHP-object. The extended functionality includes automatic conversion-functions, template-support (for phplib templates) and an easy to use MySQL-Fulltext-search on your objects (already implemented but not yet available in the current download)!

phpSorm is intended as a straight-forward approach for small to medium projects. It is not tuned for performance but rather for ease of usage. If you make general improvements to the code, please send them back to me so that all can benefit. Otherwise you are free to use phpSorm wherever you want.

To use phpSorm you simply

and - voila - you can access and modify your data via e.g. $customer->sName. SormBaseClass provides methods like readFromDatabase(), writeToDatabase(), removeFromDatabase(). So you can concentrate your work on implementing the special behaviour of your objects and you do not have to care about how to get the values out of the database and back in again.



Download: phpSorm.tar.gz (including an example of usage and the phpdoc-documentation)
Documentation: You can also browse the api-documentation (generated with phpDocumentor).
Contact: Feel free to contact me via eMail () and tell me what you think about phpSorm!

Requirements:
phpSorm uses phplib for the (mandatory) db-access and for the (optional) template-functionality. I suppose you could quite easly adapt phpSorm to another database api but i never needed to. I use phpSorm with MySQL but it certainly works with any database supported by phplib.

Example for phpSorm usage:
Here is an example to show you how easy it is to use phpSorm. This example is also included in the download.

Let's assume we want to store some data on customers, containing their name, address, email and phone. So we setup a table customer in our database with the primary key iCustomerID and the fields sName, sAddress, sEMail and sPhone.

Now we create our representation as a class in php:

class Customer extends SormBaseClass {
   //  variables
   var $sID          = "iCustomerID";
   var $sTableName   = "customer";
   var $sObjectName  = "Customer";

   // the following fields are read from the db and stored as object-properties
   var $asFields = array ("sName", "sAddress", "sEMail", "sPhone");

   // these (optional) conversions will be applied during reading/writing to the database
   var $asFieldsReadConversions = array(
	);
   var $asFieldsWriteConversions = array(
	);
	
	// the constructor usually just calls the inherited initalize()
	function Customer($sID=null) {
		$this->initialize($sID);
	}

	// return our phplib-db-connction
	function getDB() {
		return new SormSampleDB();
	}
}
You can now use this class like this to create a new customer and store it in the database:
// create a new customer
$customer = new Customer();

$customer->sName = "Icebear";
$customer->sAddress =
"On the rock 3
23243 Atlantis";
$customer->sPhone = "0170/929332";
$customer->sEMail = "icebear@atlantis.org";

// store this new customer
$customer->writeToDatabase();

// read the id under which the customer was stored
$iNewCustomerID = $customer->iCustomerID;

// and dispose the old customerobject
$customer = null;

We already stored the id of the new customer in $iNewCustomerID to demonstrate how to retrieve the data:
// now read this customer again; we use a new object to demonstrate this
$customer = new Customer( $iNewCustomerID);

echo "The name of the new customer is:" . $customer->sName . "<br>";
echo "The eMail is: ". $customer->sEMail . "<br>";


Advanced usage:
We could also use the template-support to publish all the data for the customer from the example above to the template:

// this part uses a template to print out the values of Customer
require_once("phplib-7.2d/php/template.inc");
$tplt = new Template();
$tplt->set_file("content", "test.tplt");

// publish this customer
$customer->publishFieldsToTemplate($tplt);

$tplt->pparse("out", "content");
Sometimes you might want to apply conversions on fields before writing to/reading from the database. You could e.g. replace entities, filter Strings, apply character conversions etc. phpSorm allows to specify which fields whould be converted by which functions. Currently only one function per field is allowed. Just add your conversions to $asFieldsReadConversions and $asFieldsWriteConversions in you class. In the declaration of the Customer-class above, this would be:
   // example: make sure that email-addresses are read and written as lowercase (apply strtolower())
   var $asFieldsReadConversions = array(
    "sEMail" => "strtolower",
	);

   var $asFieldsWriteConversions = array(
    "sEMail" => "strtolower",
	);
phpSorm can also provide a very easy to use MySQL fulltext-search. But i did not have time to adapt and document these functions. I will do so soon, as this is one of the nicest features.... I already use it in another project, but it still has to be ported to this phpSorm-version.



I hope you enjoy phpSorm!
Sebastian