A Path Less Taken

Breaking with convention in a very conventional fashion. Powered by WordPress

"What would you attempt to do if you knew you could not fail?"
Dr. Robert Schuller

Monday, August 1, 2011

Category: PHP Development Tags: , Author: JJ 4 Comments

No, this is not about Doctrine and Propel, although I’m sure those are both really fine PHP ORM tools if you are willing to endure the learning curve. This is about 2 lesser known but notable ORM tools that deserve a nod of recognition. I’ve tried a number of PHP ORM tools over the last year and many of them had merit. In fairness, as you look at one attempt you begin to realize how much each of them owes to those that came before them. There are so many variations on a theme that you can easily get confused and give up in frustration. But my recent efforts have revealed two that I find very useful and note worthy depending on your project. Here is a quick run down on each.

Spot

Spot is the second attempt at a Data Mapper ORM implementation for PHP by Vance Lucas. Vance’s first attempt was titled PHPDataMapper a was and nifty attempt in it’s own right. Vance took what he learned and created Spot, the 2.0 implementation of his original ORM. Spot has straight forward configuration through object inheritance and is very easy to use once you get the hang of it. Spot supports HasOne, HasMany and HasManyThrough relationships. It solves the Lazy Loading problem and is hands down the best implementation of association management through an ORM model that I’ve encountered. The available documentation is spartan right now (I’m being kind), but Vance was kind enough to give me the few pointers I needed to get working quickly. If anyone is interested let me know and I can provide some demo code in a separate post.

Given how much I have enjoyed using Spot, I have only one minor quibble. As I looked at the implementation, I could not find a way to build Models dynamically at run time rather than pre-define them. Granted this is not a problem for many projects, but it was an issue for the last thing I was doing. Having said that if I do any PHP projects in the future with static data models then I’ll be using Spot to do them. If you have a similar need and value power and simplicity then I encourage you to give Spot a go!

RedBeanORM

RedBeanORM is the marvelous dynamic ORM by Gabor de Mooij. For anyone who has read my blog you probably know that I have an on-going love / hate relationship with RedBean. This ORM is crazy easy to use as it builds the data tables and relationships on the fly as you run your program. The ORM picks the most appropriate type for your data items and adds any fields that are missing to the table. It even builds associations through intersection tables. It is such a cool concept that I am constantly drawn to it. That is except for the fact that it builds associations through INTERSECTION TABLES! I was never a fan of this and I posted in the forum that using only intersection tables was overkill for many common relational models.

After much waiting I’m happy to report that as of version 2.0 Gabor has added N:1 relationship support that implements a foreign key! It’s like mana from digital heaven! The implementation is not as user friendly as Spot (I have to do some of the work myself, gasp!), but it does go a long way towards making the resulting data model more practical for larger relational problems. This coupled with the dynamic nature of RedBeanORM make it a very compelling tool. For my dynamic model science project I’m going to use RedBean. I’m not sure where it will end up, but I’m excited to jump in and give it another try!

So there you have it. A tale of two PHP ORM’s that deserve a little recognition for their authors’ hard work and vision to make something better than what was there before. It is the true spirit of open source and I salute both of them for doing such a great job. I’m excited about ORM tools again, and that’s saying something!

4 Comments to “A Tale of 2 PHP ORM’s”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">