thinkPHP5中hasOne和belongto区别!

hasOne和belongsTo这两种方法都可以应用在一对一关联上,但是他们也是有区别的:

hasOne('关联模型','外键','主键');

belongsTo('关联模型','外键','关联主键');

最主要的区别就在于:谁是主,谁是

比如有A和B两张表

A表字段:id           name             B_id

B表字段:id            name

这样A表有B表的外键字段B_id,当在A表所对应的模型就应该用belongsTo去关联B表,A表就是从属于B。反之B表则用hasOne ,B为主,里面有一个A

原文地址:https://www.cnblogs.com/shx1024/p/11634141.html