I am building an application in Zend Framework. My model is made up of three layers, domain classes, data mapper classes and a service layer which provides an interface for all external communication. Currently the data mapper classes are only used within my service layer and the domain classes are simple php objects only containing information specific to their domain. A very rough example of what I mean....
//domain class
public class User{
// data and functions specific to the user domain
}
//service class
public class UserService{
// service contains an instance of the mapper class
protected $_mapper =
public function fetch($id){
return $this->_mapper->find($id);
}
}
//mapper class
public class UserMapper{
protected $_dbTable = new Zend_Db_Table('user');
public function find(){
return new User($this->_dbTable->find($id)->current());
}
}
I would retrieve a user object in the controller like so
$user = $this->_service->fetch($id);
This has worked fine so far but now I want to do this
$user = $this->_service->fetch($id);
$recipeCount = $user->getRecipeCount();
The new data comes from another table and I don't want to pull all this information from the database every time I load a user, I want to lazy load that information when the getRecipeCount function is called. I guess my question is what would be best practice to achieve this? As far as the consumer is concerned the information is specific to the domain of the user so I think that it should be called from the User class. To achieve this the domain class would need to contain its own mapper instance but is that negating the point of having the service class in the first place? I dont really want to be doing this everytime.
$recipeCount = $this->_service->getRecipeCount($user);
Any thoughts will be appreciated