Doctrine2 inheretance和overloading

时间:2011-04-26 12:26:47

标签: php orm doctrine-orm

我一直难以实施Doctrine2实体的最佳方式来归档以下内容。

我会尽力解释。

我希望获得不同任务/工作的基本费率。喜欢工作:洗衣服的费率为50.00。 然后每个客户都可以*覆盖这个工作和费率。所以顾客:妻子,工作:洗衣服的费率为65.00。

因此,对于每个客户,我希望能够做一些像$ customer-> getJobRate(洗衣店), 如果客户具有该作业的特定实现,则返回特定的速率,如果没有找到特定的实现,则返回默认速率。

我可能会考虑这个问题,但我能提出的所有解决方案对我来说都是“难看的”。

谢谢, 约翰

1 个答案:

答案 0 :(得分:1)

标准方法是拥有3个实体:CustomerJobCustomerJobRate。相关属性将是:

Customer:
    jobRates (OneToMany => CustomerJobRate)

Job:
    defaultRate (float)

CustomerJobRate:
    job (ManyToOne => Job)
    customer (ManyToOne => Customer)
    rate (float)
如您所述,可以在getJobRate()上实施

Customer

public function getJobRate(Job $job) {

    foreach($this->jobRates as $jobRate) {
        if($jobRate->getJob()->getId() === $job->getId())
            return $jobRate->getRate();
    }

    return $job->getDefaultRate();
}

这使您可以向CustomerJobRate添加更多信息,例如折扣。