[UPHPU] ORM

Marcellus Barrus mbarrus73 at yahoo.com
Fri Nov 17 14:43:10 MST 2006


Hey guys,

Django looks to me like a web framework that includes a data access layer.
Propel looks just like an OO data access layer without the web framework.

If you wanted an easy to use SQL Builder and Data Modeling Layer, what would
be wrong with using the PEAR::DB_DataObject with PEAR::MDB2?  It
automatically creates all the objects for you using the provided script
createtables.php. 

A sample query with PEAR::DB_DataObject would look something like this...

<?php

//user_info is the name of my table
$obj = DB_DataObject::factory( "user_info" );  

//1 is the primary key of the user I want to select

$obj->ui_id = 1;

//specifying true within the find method puts result set as part of object
//if I didn't specify true, I would run the fetch method on the object.

$resultCount = $obj->find( true ); 

//I can now use the data within my object....

echo "You selected " . $obj->username . " from the database.";

//if your result set has multiple records, you would loop through 
while( $obj->fetch() )
{
	echo "You selected " . $obj->username . " from the database.\n";
}


//now something a little more complex...
$obj = DB_DataObject::factory( "user_info" );
$obj->ui_id = 1;

//add an address table join
//Keys that join tables together are specified in a special INI file.  
//Once they are set up we forget about them.

$objAddress = DB_DataObject::factory( "address_info" );

$obj->joinAdd( $objAddress );

$obj->find( true );

//create an array to use
$myUserArrayWithAddress = $obj->toArray();

print_r( $myUserArrayWithAddress );

//Inserts are done similarly.
$obj = DB_DataObject::factory( "user_info" );
$obj->username = "me";
$obj->password = "mypassword";
$obj->firstname = "Marcellus";
$obj->lastname = "B";
$lastInserId = $obj->insert();

//Finished Inserting

//Updates are just like inserts except the primary key is required as data
$obj = DB_DataObject::factory( "user_info" );
$obj->ui_id = 1;
$obj->username = "you"; //instead of me
$obj->password = "yourpassword";  //instead of mypassword
$obj->update();

//Finished Updating

?> 

Additional information can be found
http://pear.php.net/manual/en/package.database.db-dataobject.php

And works with PEAR::DB and is now optimized for PEAR::MDB2. 

> I actually just found that with Google.  The XML configuration isn't
> too terribly complex is it?
> 
> Dan
> 
> On 11/16/06, Alvaro Carrasco <alvaro at epliant.com> wrote:
> > My favorite ORM for php is Propel:
> > http://propel.phpdb.org
> >
> > It is similar to django in some aspects, different in others. I
> > currently use it in all of my projects and i am very happy with it.
> >
> > Alvaro




More information about the UPHPU mailing list