Zend Framework: How to construct a simple "Data Mapper" model? - php

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?

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.

Related

PHP ORM frameworks with features similar to ADO.NET Entity Framework Code First?

I'm coming from .NET world back to PHP for some side projects. I am comfortable with PHP as a language, but am kind of lost in many PHP frameworks available today. Back in the days I did PHP we just wrote SQL queries, so I have no idea what is possible with PHP today in terms of ORM, therefore the question.
I got used to creating my database models using ADO.NET Entity Framework Code First and I like this approach, so I am looking for a PHP ORM framework with similar set of features.
If I understand you correctly, you are searching a framework with something like AR and CRUD.
I think almost every more famous PHP framework have this options.
Anyway, I am using Yii framework and I can say it's one of the best options, but you can surf a little to see which framework can fit your requirements.
You can create DB with table relations etc, and the generator will create your models + relations for the Active Record. Also CRUD generator can create and the View/Controller part.
You should review current PHP ORM frameworks, you may find something that they offer that the ADO.Net framework does not.
What is the easiest to use ORM framework for PHP?
This stack question is very detailed around your question: Good PHP ORM Library?.
Everyone has an opinion, some better than others.
have a look at Doctrine or Propel ORM

Ruby style relational tables in PHP

I know ruby (on rails) uses lots of "magic", but I use PHP, is there not a way of implementing the rails-like:
class Player < ActiveRecord::Base
has_many :cards
end
in PHP it would make so many peoples lives so much easier. Are there perhaps frameworks/ORM's that provide similar functionality etc?
The term you're looking for is an "Object Relational Mapper" (ORM). The one you cite is a component of the Rails framework, called ActiveRecord.
PHP ActiveRecord is one project that attempts to provide this, though the last release was in July 2010. I haven't actually used it; I just switched to Rails. :)
in CakePHP there are relationships such as hasMany, belongsTo, hasOne, and hasAndBelongsToMany
PHP doesn't have a ORM by itself, but there are several fairly usable ORM projects for it; I'm acquainted with Propel, and it's rather good (used e.g. in the Symfony framework, which resembles Ruby somewhat).

Which PHP ORM works best with Zend Framework?

Well, seeing as I'm dissatisfied with Zend_Db_Table after being spoiled by LINQ, I'm looking to get started learning an ORM with PHP. General consensus seems to be that Doctrine and Propel are the only good ones for serious use -- and whatever my opinion, I'd like to use something at least moderately popular so that people in the future can look at this app I'm working on without having an head explosion :P
I'm currently leaning towards Propel because it's documentation seems to be a bit more complete, and it supports the nested set model (also called "modified preorder tree transversal model") right out of the box. However, I like Doctrine's use of namespaces and other PHP 5.3 features, and it seems to be a bit more popular.
From those who have used either ORM with Zend Framework, which meshes better with the existing framework (if either)? What kind of issues should I watch out for using either framework with Zend?
I can't speak for Propel but there are a lot of good integration resources for ZF and Doctrine. See Zend Framework 1.11 with Doctrine 2 Integration
Well Doctrine 1.2 supports Nested Set... much better than propel did in < 1.3. I havent used propel 1.4/5 so i cant comment on its current state. I also havent used Doctrine 2... ive been sticking with 1.2 since mot of my projects dont use php 5.3...
Overall I liked Propel better... I prefer generated classes to ones configured dynamically at runtime. Also keep i mind Propel is an active record implementation while Doctrine 2 uses the Data Mapper pattern.

PHP Object Relational Mapping Framework

I would like to know which one of the following is the best choice for O/R mapping in PHP:
Axon
CakePHP
Doctrine
Kohana PHP
lworm
PdoMap
Propel
Rocks
Qcodo
Redbean
Sphorm
Torpor
We are going to use PHP only for Web Service implementation. We have a Java background so a framework which is inspired by Hibernate would be easier for us to use as long as it's a good, well documented and more or less easy to use O/R mapping library.
Thank you!
The problem with this question is that it is hard to tell what is the best choice for your specific task and environment. Furthermore, a full comparison would require in-depth knowledge of all the alternatives.
As for Doctrine you'll be able to find a good piece of documentation. And it is fairly easy to get going.
Like Jensgram already notes, it is hard to tell which option suites your needs.
That said, I have experience with Kohanaphp and it's integrated ORM. I must say it works perfect, but it has limited functionality if you compare it to Doctrine2. If you need advanced options like inheritance mapping and proxy classes Doctrine is the way to go. Like Doctrine 2 introduction says:
Object relational mapper (ORM) for PHP
that sits on top of a powerful
database abstraction layer (DBAL). One
of its key features is the option to
write database queries in a
proprietary object oriented SQL
dialect called Doctrine Query Language
(DQL), inspired by Hibernates HQL.
This provides developers with a
powerful alternative to SQL that
maintains flexibility without
requiring unnecessary code
duplication.
As it says, it is inspired on Hibernate HQL. I don't have experience with the other options you mention, so I can't say anything usefull about those.

PHP ORM library based on the Data Mapper pattern

For a PHP application with a complex domain model, I don't want to use the Active Record pattern, I need instead the Data Mapper pattern (as presented in Zend Framework).
Do you know any library that could help me for the ORM part, or else a link to a documentation on "how to do it right" ?
After a lot of searching, i found 2 Data Mapper ORM :
Xyster : really great, but developped by a couple of people, so I can't use it professionally (even though it seems really great)
Doctrine version 2 : will be integrated to Zend Framework 2, will support the Data Mapper pattern, and seems to be the best option ever with possibilities inspired by J2EE and Hibernate. Unfortunately, still in beta.
Note : Doctrine 1.0 is not based on the Data Mapper pattern but on Active Record, as Propel. These are not fullfilling my needs.
In conclusion, the answer, for now, is : there is none.

Categories