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 Record: http://phplens.com/lens/adodb/docs-active-record.htm
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.
I am Java developer, recently took part in small PHP project, never wrote anything serious on PHP, the question is not really connected to the topic, but in my case it is. I created my first version using model objects like Portal, Slot with simple setters and getters, with manually written DAO objects having saveByID, deleteByID, etc. Now I am going to move to ORM library - haven't decided yet which one choose though, but I understand the necessity of using Builder pattern - the class like class Builder { var $object; function Builder (&$object) { $this->object = $object; return $this; } function id ($id) { $this->object->id = $id; return $this; } function name ($name) { $this->object->name = $name; return $this; } with all attributes my model objects have and creating them like: function testBuilder () { $banner = new Banner (); $builder = new Builder ($banner); $builder->id(1)->name ("test")->html ("html")->targetURL ("http://here"); assertEquals ("test", $banner->name); assertEquals ("html", $banner->html); BTW I was really suprised that you cannot write on PHP new Builder (new Banner ())->id(1)->name ("test") My question - how usually PHP developers implement (do) Builder-pattern simlar things - when you need to create model objects with different but intersecting set of attributes? PHP is so dynamic language I guess there must be some interesting solutions.

About Me

I am Omar. An OpenSource enthusiast, I do Drupal contributing and service providing. Get some more boring details here. You may contact me for any ideas, questions or help.

Tags

Online

There are currently 0 users and 0 guests online.
Powered by Drupal, an open source content management system