Well I've read Matt Zandstra's book PHP Object, Patterns and Practices, which introduce some basic patterns to handle database and the impedance mismatch issue. The very first pattern he introduced was the well known Data Mapper pattern originally proposed by Martin Fowler, the syntax is simple and easy to follow:
abstract class DataMapper{
public function findByID($id);
public function findAll();
public function createObject(ArrayList $data);
public function insert(DomainModelObject $dmo);
abstract public function update(DomainModelObject $dmo);
abstract public function doInsert(DomainModelObject $dmo);
abstract public function doCreateObject(DomainModelObject $dmo);
abstract public function update(DomainModelObject $dmo);
abstract public function selectStmt();
abstract public function selectAllStmt();
}
class UserMapper extends DataMapper(){
public function findByUsername($username);
public function findByEmail($email);
public function findByIP($ip);
public function update(DomainModelObject $dmo);
public function doInsert(DomainModelObject $dmo);
public function doCreateObject(DomainModelObject $dmo);
public function update(DomainModelObject $dmo);
public function selectStmt();
public function selectAllStmt();
}
In the examples from that book, the data mappers are responsible for manipulating domain model object, or a collection of objects. It is usually coupled with identity object, unite of work, lazy loading and query object patterns to form a complete ORM system. Its flexible and easy to code, although it does seem to need to write a bunch of lines of things.
In Doctrine however, their ORM system works a bit different. You do not create data mapper classes, and instead you simply use the annotation specific to Doctrine. The annotation can be used wither as comments, XML or YAML. I aint quite sure how Doctrine does this trick internally yet, but Id assume they have implemented an interpreter pattern behind the scene to translate those annotations into ORM elements that help with mapping data into model object. Doctrine is also highly optimized to work at maximum performance level.
So the question is, which way of ORM is better from a coder's point of view? They definitely seem to be achieving the same result, but the approaches are somewhat different. Is there a particular reason to favor Martin Fowler's approach over Doctrine's, or the other way around? What do you think?