I have recently started reading about data mappers and all the articles I've found only demonstrate CRUD operations like:
$user = new User('John', 'Joe', 'john@hotmail.com');
$userMapper->insert($user);
// or this...
$user = $userMapper->fetchById(239);
Surely they are supposed to do more?
Currently in my application I use DAO's (or something similar anyway) so for example when I need a $user
object one of my factories creates a $userDAO
object and injects it into my $user
object. And to do a query from my $user
object I just do:
$this->userDAO->getNumActiveOrders($this->userId);
and it will do the query in the $userDAO
object and return the result.
After loads of reading it seems my implementation is wrong because the domain object should not know about the DAO and vice versa. Am I right or wrong?
If it's wrong to do it that way then I assume that data mappers must be used for more than CRUD operations?
So if I wanted to find out how many active orders a user has I can do something like:
$userMapper->getNumActiveOrders($userId);
Would that be correct?
And if I wanted to set that value in my $user
object I would have to do something like:
$user->setNumActiveOrders($userMapper->getNumActiveOrders($userId));
Using my implementation of DAO's seems to be a lot faster and uses less code than using data mappers but I am probably implementing data mappers wrongly.
Any advice would be great thanks.