Hibernate for PHP: using Doctrine as part of a Unit-Test workflow

by jon on June 6, 2008

On the Zend general mailing list, Josh Team asked some interesting questions about form validation and best practices. Lately, I've been looking for an alternative to NHibernate for my PHP development. Thinking that I would have to use Hibernate and run PHP inside a Java bucket, I figured the barrier to entry would be too great and would require a lot of research. Then someone mentioned to Josh Team that he should use Doctrine, an object-relational mapper in PHP.

The Doctrine Documentation looks pretty solid and Ruben Savanne wrote a great article about integrating Doctrine into a Zend Framework project. I wholeheartedly agree with his statement that Zend_Db is a little too low level for 90% of tasks that application developers are aiming to accomplish. With a simple YAML configuration, Doctrine promises to generate the necessary classes and even your database schema. These two features are a must if you want to be able to model your database on the fly and run unit tests against a clean schema without writing a complex setup harness.

My only concern is the lack of any obvious enterprise support, regardless this library is worth looking at as an alternative to Zend_Db.

{ 7 comments… read them below or add one }

Tomáš Fejfar November 18, 2008 at 9:26 am

Doctrine looks fine, but I found a blocker after few minutes using it. It doesn’t support Relations with multiple cols in PK. And that’s exactly what is present in the only table I can’t modify. :D

jon November 18, 2008 at 11:02 am

Hi Tomas,

you’re right about that! I did a project with Castle ActiveRecord / NHibernate with composite keys, but haven’t dealt with composite keys in PHP since the schemas I’m writing code against are simpler.

There’s a Doctrine feature request (#460) for composite key support and its expected for the 2.0 release of Doctrine. I guess we’ll just have to wait for now :/

Tomáš Fejfar November 18, 2008 at 11:22 am

Yes, I know about #460 ticket. Unfortunately I can’t wait till 2.0 release :) I need it NOW ! :)

I usually don’t need things like 2 col PKs, but this is my bachelor final-exam, working with university employee database, so I can’t modify it.

NM January 13, 2009 at 3:11 am

Use an artifical key like GUID and define a composite as unique, it’s better anyway.

Elijah June 7, 2009 at 10:43 am

Why not look at using Hibernate by streaming through PHP-JavaBridge?

Josh Team January 19, 2010 at 4:51 pm

I heard that Josh Team guy is brilliant?! I actually adopted Doctrine 110% and integrated it with serveral Zend stuff such as:

Zend_Paginate, Zend_Navigation, Zend_Search_Lucene ..
http://joshteam.wordpress.com/2009/06/01/doctrine-zend_paginate/

Brilliant!

Beau D. Simensen April 5, 2010 at 5:23 pm

If you like the way Hibernate handles persistence you may be interested in Repose PHP ORM. I started to actively work on it again after a short break and although it is still pretty rough around the edges it works. If enough people become interested in using it and start providing feedback, I would love to dedicate many more cycles to the project.

Repose is an Object-Relational Mapping (ORM) library for PHP5. Repose implements a Unit of Work (UoW), an Identity Map and generated proxy classes to provide transparent unobtrusive persistence in PHP.

If you are interested in taking a look at it, I would be happy to work with you on it as the documentation is still a work in progress.

Leave a Comment

Previous post:

Next post: