Zend Framework’s ORM

Posted on October 3, 2010

In terms of MVCs for PHP, Zend Framework is at the top of my list. It’s well thought out, easily expandable, and once I learned it I found it to be rather intuitive. My current project for work was built on CodeIgniter, which I find to be lacking in some places. It’s got a little too much magic under the hood for me, I think. And our team had to modify parts of CI to do what we wanted – in particular, to add an active record pattern. In my previous uses of Zend, we didn’t fully use their built-in database classes, and instead chose to build our own models and functionality. This wasn’t a choice we made for any ideological reason, but in retrospect, we probably should have used an ORM on at least one of the projects.

Anyway, I was a few versions behind on Zend, so I started looking at the latest iteration (1.10). The basic structure has changed but all the power is still there, and it was a fairly easy transition. But having become accustomed to data mapping and such on my latest project, I started looking into Zend’s implementation of tables and such. It seems alright for basic use, I suppose, but the part I’m having a problem with is joining tables together within the objects.

As I currently understand it, you define relationships between table classes using a protected $_referenceMap. So if i have a User class, I can bring in a collection of Session classes for it. That makes sense enough. The place where this breaks down, though, is that both classes have to have references to each other. In other words, User has to have a link to Session, and Session has to have a link to User.  I don’t really like the idea of forcing the relationship to be two way.

Let’s say you had a system where you have tables of movies, TV, and video games. They’re all the generic type of media, so you build a parent table to keep track of the media and some metadata, like creation date. You would want the movie, TV and game objects to all have references to their metadata entry, but you wouldn’t want the metadata object to have to link to all three underneath. This would become even more cumbersome if you wanted to add another media subtype, as you’d have to edit the parent class as well. And what if the parent class is, for whatever reason, off-limits? Again, this is based on my understanding, so I could be wrong here. But this is based on my tinkering around with it. If I got it wrong or something, please leave a comment and let me know.

I mean, overall I’m quite happy with Zend, but this is just a little issue I came across. Writing your own CRUD functions and building your own query functions would be a rather acceptable workaround. I’ve read of people having success with using other PHP ORMs like Propel and Doctrine, so I guess that would be another option.

Leave a Reply

Your email address will not be published. Required fields are marked *