【发布时间】:2021-10-24 10:07:04
【问题描述】:
我正在处理一个较旧的项目,我的任务是在我们进行完全重写的同时加快某些部分的速度,因为代码维护得很差,写得不好,而且对于它应该做的事情来说已经过时了.
我偶然发现了一个项目核心问题,因此我无法在不破坏几乎所有其他内容的情况下更改它。所以我需要以雄辩的方式加载“关系”(使用Planning:with('availability'),但没有真正的外国ID,而是与多个字段重叠。
是否有一种方法可以在一个查询中使用重叠字段将其全部加载,而不是单独加载创建 n+1 问题?
+--------------+-----------------+
| Planning | Availability |
+--------------+-----------------+
| planning_id | availability_id |
| date | date |
| startHour | startHour |
| stopHour | stopHour |
| candidate_id | candidate_id |
| section_id | section_id |
+--------------+-----------------+
从上面的例子你可以看到重叠的字段是 date、startHour、stopHour、candidate_id 和 section_id。
我尝试了 get...attribute,但它仍然加载 n+1,我尝试将它包含在 ->with(['availabilities']) 中,但这不起作用,因为我要求
模型而不是关系:
为了更清楚而编辑:
规划模型:
public function availabilities()
{
return Availability::where('section_id', $this->section_id)
->where('candidate_id', $this->candidate_id)
->where('planningDate', $this->planningDate)
->where('startHour', $this->startHour)
->where('stopHour', $this->stopHour)
->get();
}
public function availabilities2()
{
return $this->hasMany('App\Models\Availability', 'candidate_id', 'candidate_id')
}
控制器:
$plannings = Planning::with(['availabilities'])->get();
$plannings = Planning::with(['availabilities2' => function ($query) {
// $this is suppose to be Planning model but doesn't work
$query->where('section_id', $this->section_id)
->where('planningDate', $this->planningDate)
->where('startHour', $this->startHour)
->where('stopHour', $this->stopHour);
// ---- OR ---- //
// Don't have access to planning table here
$query->where('section_id', 'planning.section_id')
->where('planningDate', 'planning.planningDate')
->where('startHour', 'planning.startHour')
->where('stopHour', 'planning.stopHour');
}])->get();
【问题讨论】: