duannei0044 2011-04-26 12:26
浏览 25
已采纳

Doctrine2 inheretance和overloading

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

  • 写回答

1条回答 默认 最新

  • duanhuang4306 2011-04-26 16:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线