I've been thinking lately that the ActiveRecord approach to ORM I've been using isn't flexible enough, and I've been looking into other patterns. I'm aware of the ones discussed in PoEAA, but not having read the book, I'm not 100% on what the best implementation would be. Here are some ideas I have, though:

The first is to have the mapper be contained by the model, passed as a parameter in the constructor, like this:

PHP Code:
<?php

class Model {
    var 
$mapper;
    
    function 
Model($mapper) {
        
$this->mapper $mapper;
        
    }

    function 
save() {
        
$this->mapper->save($this);    
    }
    
}

class 
Mapper {
    function 
save($model) {
        
        
    }
    
}

$model = new Model(new Mapper());

$model->save();


?>
I like this approach because it achieves the decoupling of the data access code from the business logic with very little change to application code; simply the extra parameter in the constructor.

However, the code could be even further decoupled like this:

PHP Code:
<?php

class Model {
    
}

class 
Mapper {
    function 
save($model) {
        
        
    }    
    
}

$model = new Model();
$mapper = new Mapper();

$mapper->save($model);


?>
However this would require rewritting a lot of the code that uses the model, and I don't currently see what advantages it has over the other approach.

Are there any other approaches that are worth looking into? How is the Data Mapper pattern typcially implemented? And how about the Row and the Table Data Gateway patterns?

Thanks...