Blogger Information
Blog 9
fans 0
comment 2
visits 14095
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
tp5.1 belongsTo和hasOne的区别
sky的博客
Original
3915 people have browsed it

今天写个demo,用到了一对一关联,看了一下文档,发现hasOne和belongsTo这两种方法都可以应用在一对一关联上,但是他们也是有区别的:

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

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

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

比如有A和B两张表

A表字段:id           name             B_id

B表字段:id            name

这么一来A表有B表的外键字段B_id,所以在A表所对应的模型就应该用belongsTo,A表就是从属于B

反之B表则用hasOne ,B为主,里面有一个A

例子请看下一讲

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
sky. 2018-11-06 14:39:09
666
1 floor
Author's latest blog post