場景
員工表通過 員工與任務(wù)的關(guān)聯(lián)表 獲取到自己的所有任務(wù)
數(shù)據(jù)表
員工表-employee employee_id
任務(wù)表- task task_id
員工與任務(wù)的關(guān)聯(lián)表 task_target
task_target_id-自增ID
target_type-參與任務(wù)的類型,員工或者門店團(tuán)隊
target_id-參與對象的ID
那么,在員工的模型中,應(yīng)該這樣寫關(guān)聯(lián)關(guān)系
public function hasManyTask()
{
return $this->belongsToMany(Task::class,'task_target','target_id','task_id','employee_id')
->wherePivot('target_type','=',Target::TARGET_TYPE_EMPLOYEE);
}
解析原 SQL
select `task`.*,
`task_target`.`target_id` as `pivot_target_id`,
`task_target`.`task_id` as `pivot_task_id`
from `task`
inner join `task_target` on `task`.`task_id` = `task_target`.`task_id`
where `task_target`.`target_id` = 1513
and `task_target`.`target_type` = 1 and `task`.`deleted_at` is null

其實這種語句就是通過一個中間表中,與另一個表的關(guān)聯(lián)字段task_id獲取到另一個表的數(shù)據(jù)。對了,其中 where 條件,其實就是第三個參數(shù)target_id值對應(yīng)的中間表的數(shù)據(jù)。
belongsToMany參數(shù)解析
參數(shù)(Task::class)1:是最終要獲取數(shù)據(jù)的表模型
參數(shù)(task_target)2:是與本表與第一個參數(shù)表的關(guān)聯(lián)表
參數(shù)(target_id)3:此參數(shù)是foreignPivotKey,是中間表task_target針對本表的外鍵
參數(shù)(task_id)4:是relatedPivotKey。related 其實是指Task::class表。所以字段其實是中間表task_target針對Task表的外鍵。
參數(shù)(employee_id)5:是parentKey。這個是 where 條件中的字段值
wherePivot 是中間表 task_target 的條件
擴(kuò)展
pivot:是樞紐的意思,其實是指兩個表的中間關(guān)聯(lián)表
外鍵:外鍵是在本表里面。其實就是本表與另一個表關(guān)聯(lián)的字段,通過外鍵的這個字段,可以獲取到另一個表的數(shù)據(jù)。
一對多關(guān)聯(lián)解析
belongsTo 的第二個參數(shù)是本表的外鍵
第三個參數(shù)是關(guān)聯(lián)表的,元本表外鍵關(guān)聯(lián)的字段
最終結(jié)果其實是where 條件中
第三個參數(shù)是 字段,第二個參數(shù)是已知的值
但是 hasOne 則不同
hasOne 的最終結(jié)果是
第二個字段是鍵字段,第三個參數(shù)是已知的值


select * from `org_employee` where `org_employee`.`employee_id` = ? limit 1
其實取的值是 where org_employee.ownerKey=foreignKey,foreignKey是本表中的已知值??汕f不能把這兩個參數(shù)單純的理解成外鍵和本件。如果ownerKey換成本表的主鍵,則會報錯,org_employee表沒有本表主鍵這個字段
之所以我們按照手冊上的操作不需要區(qū)分本地主鍵和外鍵是因為,比如一個用戶參與了很多評論
user --id
post --- id ,user_id
比如 user.id=3
post.id=2,post.user_id=3
那么在評論中獲取用戶的SQL語句其實會是
public function user()
{
return belongsTo(User::class,'user_id','id');
}
select * from user where user.id=? limit 1
參數(shù)值是3,所以得到了用戶數(shù)據(jù)
因為這種關(guān)系,所以不會報錯。但其實這個ID 是與user.id 想對應(yīng)的,并不是本地的主鍵
如果user.id 改成 user.abc 則
return belongsTo(User::class,'user_id','abc');
select * from user where user.abc=? limit 1
post表里面根本不需要這個表


通過源碼我們知道 $instance 就是關(guān)聯(lián)的表
$ownerKey = $ownerKey ?: $instance->getKeyName(); 就是關(guān)聯(lián)表的ID
hasOne 和 belongsTo 是不一樣的
employee 表主鍵改成 employee_id11
record 表還是 employee_id 而且沒有 employee_id11 字段

第三個字段是 where 條件中的值,第二個字段是where 條件中的鍵
其實外鍵此處就是關(guān)聯(lián)表的鍵(注意,不一定是主鍵)
localKey 其實就是本表的值