views:

378

answers:

4

I'm building an application in the Zend Framework, but I'd like to implement a "Data Mapper" style ORM layer, constructing Model classes that only include the specific pieces of data they need to represent the domain concept (regardless of which tables those fields happen to belong to). Since my DB structure is highly normalized, the benefits gained from an Active Record ORM would be superficial.

So how would you implement a very simple, straightforward data mapper ORM layer in the Zend Framework?

+1  A: 

The implementation details for creating an ORM can be quite extensive, and I lack the experience to give any meaningful recommendations beyond suggesting already existing projects, so I will suggest one instead.

The Data Mapper pattern is not very popular in the PHP world (in favour of Active Record, and certainly much of that favour is attributed to the fawning of Ruby on Rails in the PHP framework community), although there exists one notable project -- and it looks promising.

Outlet ORM follows the Data Mapper pattern, although I am not sure if it would be sufficient for your needs. I have so far had no problems with it.

Adrian
I'm not sure Outlet actually uses Data Mapper. But I've only used it as a component of a larger project; I had no hand in setting it up so I can't really be certain on that point.
Brian Lacy
Oh, and I think you're right about Data Mapper being more obscure in part because of Ruby, although it seems to me that Active Record is more widely implemented throughout the CS world. I tend to think that PHP devs everywhere are starting to grow disillusioned with AR, and will begin moving to DM en masse. :)
Brian Lacy
+1  A: 

Hi,

i would suggest to use Doctrine with the Zend Framework if you want to use an ORM. There are good Tutorials and Screencasts around with a lot of information.

Doctrine 1.2 with Zend Framework Screencast

ArneRie
Thanks for the casts, I'll definitely check them out. I've pretty much decided to go with Doctrine 2, but its possible some info may still be relevant.
Brian Lacy
+1  A: 

Hi Brian,

I believe there's no true "Data Mapper" style ORM for PHP yet. If you want a true "Data Mapper", I think you might be out of luck.

There are 2 popular ORM in PHP, Propel and Doctrine. They are both more on the ActiveRecord side.

Doctrine in my own opinion is the go. At the moment, it's stable release 1.2 is not integrated with ZF yet. e.g. you cannot use the doctrine command line script to generate model classes for a modular ZF application setup.

However if you are running a single module ZF application, doctrine can be integrated pretty well. As @ArneRie pointed out, ZendCasts has got some really good videos. I learned a lot from it.

If you are interested, I also derived from it and made one of my own approach. You can find my blog post about this topic at http://blog.elinkmedia.net.au/2009/12/03/zf-doctrine-and-unit-tests/. You can download the source code of my sample app from github too.

Jim Li
Great resources, thanks.
Brian Lacy
+1  A: 

Doctrine 2, which is currently in alpha (beta due 1 March 2010), is a data mapper orm inspired by JPA/Hibernate. Zend Framework has shelved their own Zend Entity component in favour of integrating Doctrine 2 with Zend Framework.

Depending on your timescales, you might want to look at Doctrine 2.

rogerh
I've actually been playing with Doctrine 2 a bit and will probably start integrating it into one of my upcoming ZF apps, since the release timeframes match up pretty well. As far as ORM's go I'm most familiar with Propel, but so far I'm liking Doctrine's approach to things quite a bit more.
Brian Lacy