Zend Framework: How to construct a simple "Data Mapper" model?
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 Frame开发者_JAVA百科work?
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.
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.
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
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.
精彩评论