
Using an object perstistence layer can be greatly benefitial for any medium to large size project. Or, any project at all some would say. If you don’t know what I’m talking about, I’ll give you a quick example. Usually, in PHP we would do something ugly-bugly like this to retrieve info about a certain item in the database:
$res = mysql_query(“SELECT * FROM article WHERE ID=5”); if($row == mysql_fetch_assoc($res)) { echo $row[“title”]; // do something here }
Compare it to this:
$article = new Article(5); echo $article->getTitle(); // do something here
Ah, the beauty of OO.
My first experience with this was the PEAR DataObject package. I was amazed – it could generate classes based on the tables found in my database automatically, and I could start using them instantly! Yummy!
Lately I’ve been having a look at Propel. You see, I know the Symfony guys chose Propel, and after listening to a podcast with an interview with one of the project leads there, I’m convinced that the guys behind symfony are smart. Smart people choose the best frameworks, so my logic tells me to look at Propel :)
I haven’t tried it yet – but at first glance it looks better than PEAR DO. Why? Well, Propel will generate 4 classes for each table in your database. Overkill you say? Well, read this from the Symfony Model documentation:
Why keep two versions of the data object model, one inmodel/om/
and another inmodel/
?
You will probably need to add custom methods and attributes to the model objects (think about the->getName()
method that outputs theFirstName
andLastName
together). But as your project develops, you will also add tables or columns. Whenever you change theschema.xml
, you have to make a new call tosymfony propel-build-model
to generate the object model classes. TheBase
architecture allows you to keep using thesymfony propel-build-model
command even after you added custom code to your classes. Here is how it works: theBase
classes kept in themodel/om/
directory are the ones generated by Propel. You should never modify them since every new build of the model will completely erase these files. But if you need to add custom methods, use the regular object classes of themodel/
directory that actually inherit from the previous ones.
Of course, you could just extend the PEAR DataObjects created but it just … well, it just doesn’t seem to be encouraged. I like when a framework encourage me to do stuff The Right Way(TM).
So, Propel it is, I thought – but that was before a co-worker of mine suggested EzPDO which supposedly had a bit of a different approach. They brand themselves as “A simple solution for PHP Object Relational Mapping and Data Persistence” and they seem to be worth a look.
The final solution I’m looking at for solving this issue is using Java combined with PHP. Using the new Zend Platform it’s possible to use Java objects, in PHP code – is that great or what. A little illustration for you, which I found here.
That way the db stuff can be handled in EJB’s maybe, everything is transferred via SOAP and I can use objects in PHP like I would with Propel etc. It’s all a bit sketchy, but it’s something I’m looking at.
Comments anyone? I’d love to hear some feedback from people who have actually done this ..
This post is not about design-design, but the internal design of a software system. Such a design has several general characteristics, and Code Complete 2 has a list of these. First, a related quote by R. Buckminster Fuller:
“When I am working on a problem I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong.”
Great Software Design would have all these characteristics, but that’s almost never possible. Some of them contradict each other, so often we have to make tradeoffs. Anyway, here’s the list:
And, whatever you do: DO NOT CONSIDER SPEED! NEVER OPTIMIZE WHILE DESIGNING! (yes, those really needed to be capitalized). If I have one more PHP coder tell me that the MVC solution probably won’t be speedy, I will have to slashdot him physically in public. You don’t want to start off by trading any of the characteristics above for speed.
You design the system with design in mind, and if it turns out to be slow then you optimize. You will know what the bottleneck is, and you will make a sensible and thought-through trade-off. Then you will have your good design, and your speed. You will, rule the entire planet. Hah, kidding, the last part I just made up.
Well, what are you waiting for? Run out in a flowery meadow with your pen and paper and design something beautiful!