hasOne()方法有3个参数
public function hasOne($related, $foreignKey = null, $localKey = null)
第一个参数为对应的model,第二个参数默认为model对应的表的外键,第三个参数默认为当前模型对应的表的主键。这个方法的sql语句为
select * from model对应的表 where model对应的表的foreignKey = 当前模型对应的表的localKey
belongsTo()方法有4个参数
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
第一个参数为model,先讲第四个参数,默认为调用belongsTo()的方法名字,第二个参数默认为第四个参数加上_id,第三个参数默认为
model的对应表的主键。相当于sql语句
select * from model对应表 where model对应表的otherKey = 当前模型的表的foreignKey
具体可以通过阅读源码更加详细。
---------------------
作者:_laomei_
来源:优快云
原文:https://blog.youkuaiyun.com/sweatott/article/details/60146782