Ive been stumped on the best way to implement Doctrine2 entities to archive the following.
Ill do my best to explain.
I want to have base rates for different tasks/jobs. Like job: do laundry with a rate of 50.00. Then each customer can* override this job and rate. So customer: Wife, job: do laundry with a rate of 65.00.
So for each customer I would like to be able to do something like $customer->getJobRate(do laundry), and if the customer have a specific implementation of that job it returns the specific rate, if no specific implementation is found if returns the default rate.
I may be thinking about this too hard, but all of the solutions I can come up with just seem 'ugly' to me.
Thanks, John
The standard way would be to have 3 entities: Customer
, Job
, CustomerJobRate
. The pertinent properties would be:
Customer:
jobRates (OneToMany => CustomerJobRate)
Job:
defaultRate (float)
CustomerJobRate:
job (ManyToOne => Job)
customer (ManyToOne => Customer)
rate (float)
getJobRate()
could be implemented on Customer
as you described:
public function getJobRate(Job $job) {
foreach($this->jobRates as $jobRate) {
if($jobRate->getJob()->getId() === $job->getId())
return $jobRate->getRate();
}
return $job->getDefaultRate();
}
This leaves you open to the possibility of adding more information to the CustomerJobRate
e.g., discount.