PHP and ORM

Since the lauch of PHP3, many things has changed. PHP is now dominating (according to NetCraft), with some great number of projects created everyday on SourceForge and FreshMeat, still I see a problem with PHP developing: PHP was not built for developers. PHP was built for designers and programming newbies, maybe this helped many snthausiasts to join the developing community, but it, for sorrow, convinced some PHP developers that what they do is correct which isn't the case usually. This is, IMHO, the point that sets ASP.NET in a higher rank against PHP: it was built for developers and it's a real programming language.
PHP has lacked many basic fundamentals of the de facto languages had in the time of PHP3, the first major PHP release. Maybe Zuraski and the folks leading PHP are going the right way as I felt from the Paris Meeting (review my post about PHP 6), but I think we -PHP developers, should start reading more about how to code the right way using the right tools.
ORM or Object-relational mapping, is a topic I recently went through after a discussion with a experienced developer, ORM is aimed towards setting you free from DB with all the issues related to, such as DB design and Normalization, ERD creation and updating, etc..
ORM is a set of classes that allows you to create classes that you, whenever needed, instantiate obejcts from then do your full CRUD (create, review, update and delete) in the object level, which will automatically reflect on DB.
Certainly we need to show some code:

// Inserting:
$x = new Author();
$x->setFirstName('Omar');
$x->setLastName('Abdel-Wahab');
$x->setURL('www.owahab.com');
$x->save();
// Selecting:
$c = new Criteria();
$c->add(AuthorPeer::FIRST_NAME, "Leo");
$results = AuthorPeer::doSelect($c);
foreach($results as $author) {
print "Author: " . $author->getLastName() . ", " . $author->getFirstName() . "\n";
}
// Updating:
$obj = AuthorPeer::retrieveByPK(1); // get Author where pkey is 1
$obj->setFirstName($obj->getFirstName() . "-modiified");
$obj->save();
// Deleting:
$author = AuthorPeer::retrieveByPk(1);
$author->delete();

The above example is written for Propel, which isn't the only choice for ORM, the list is long: LPO, EZPDO, Doctrine, and many others. You can find a good list of ORM here.
Things can go really faster when you start getting used to a good ORM, specially for the PHP folks starting everything from scratch and maybe very soon we will see popular systems like PHPNuke, Joomla and osCommerce appearing with ORM built-in. Who knows?

ADOdb Active

ADOdb Active Record:
http://phplens.com/lens/adodb/docs-active-record.htm

Submitted by Neo (not verified) on 2 December 2006 - 9:23am.
ORM

I would love to see projects like Joomla make use of an ORM. One reason being most of these projects don't have support for mssql and other databases, by using an ORM they would get the benefit of supporting many more databases with little effort.

Submitted by Scott (not verified) on 9 May 2007 - 5:36pm.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p>
  • You may post PHP code. You should include <?php ?> tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options