Hi,
I’m implementing my own interpretation of the datamapper pattern and have some questions about the pattern in general.
Everywhere I read about the datamapper pattern I see something like this:
<?php
$user_mapper = new UserMapper;
$user = $user_mapper->find(1)
$user->username = 'UserName';
$user_mapper->save($user);
?>
First; I cannot see any reason for not making the UserMapper a static class when you send it the $user object to save? This would accomplish the same thing in fewer lines with the same functionality and probably more efficient since we don’t have to instantiate a UserMapper object… What I mean is something like this:
<?php
$user = UserMapper::find(1)
$user->username = 'UserName';
UserMapper::save($user);
?>
Or what if I let the datamapper know about the current domainobject… Wouldn’t it be more convenient to just use $user_mapper->save(); and have the $user object already in the mapper? Since we already have to instantiate the UserMapper anyway…?
<?php
$user_mapper = new UserMapper;
$user = $user_mapper->find(1)
$user->username = 'UserName';
$user_mapper->save();
?>
I also see a lot of mappers with ‘static’ variables like relationships/properties (and their validation rules) used as regular variables with $this->_relations, when it is clearly that ALL mappers/models of that type will have the exact same relations/validations rules/properties and therefor should be static variables… or am I missing something? I feel that many PHP developers who use OOP, tries to make everything into objects just for the sake of it… I also see alot of singleton classes that clearly is singletons just because it makes them “object oriented”…
What I’m asking is basically if I’m implementing a datamapper pattern wrong if I use static variables for variables that doesn’t change between instances of the same class/mapper, and if I’m missing something if I leave out the $mapper->save($user) part (do $mapper->save() instead) and just makes the datamapper know about the $user object I’m currently editing by setting a variable $this->_domain_object within the datamapper so I don’t have to pass the $user domainobject back to the datamapper when saving…