I am trying to better implement OOP and dependency injection in my code and come across the below issue.
I provide services to clients where an employer and a company are involved (with corresponding models, mappers and database tables):
class Service
{
protected $clientId;
protected $client;
protected $employerId;
protected $employer;
protected $companyId;
protected $company;
public function setClient(Client $client)
{
$this->client = $client;
}
public function setEmployer(Employer $client)
{
$this->employer = $employer;
}
public function setCompany(Company $company)
{
$this->company = $company;
}
// more
}
To get a Service object I first instantiate the Service object which returns a clientId from the database. With the clientId I instantiate a Client object (and attach it to the Service) which involves going to the database again. Same for Employer and Company.
I could just retrieve service, client, employer and company in one go from the database with joins but that would make my mapper(s) more complex. E.g. clients, employers and companies all have addresses, so I need to alias these columns and map them to the respective models. That is less clean than just retrieve all columns from each table separately and map them to each model individually (e.g. with some logic to translate underscored columns to ZF camelCase), reusing my Client, Employer and Company Mappers.
Is there a best practice solution or is it up to personal preference and circumstances (performance vs maintainability)?