🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# thinkphp 一对多 多对一的源码实例 一对一关联: has\_one 以及相对的belongs\_to 一对多关联:has\_many 以及相对的belongs\_to 多对多关联: belongs\_to\_many: 一对多:hasmany(关联模型名,外键名,主键名【模型别名定义】) 一对一: hasone(关联模型名,外键名,关联主键名,【模型别名】,json类型) 一对多 : user表和article表 user表字段 username 和password article表字段 content和user\_id ``` <pre class="calibre13">``` user 模型 <span class="token"><</span><span class="token">?</span>php namespace app\\api\\model<span class="token1">;</span> use think\\Model<span class="token1">;</span> <span class="token2">class</span> <span class="token3">User</span> <span class="token2">extends</span> <span class="token3">Model</span> <span class="token1">{</span> <span class="token3">publicfunction guanlian</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span>$<span class="token2">this</span>\<span class="token">-</span><span class="token">></span><span class="token3">hasMany</span><span class="token1">(</span><span class="token4">'app\\api\\model\\okl\\Article'</span><span class="token1">,</span> <span class="token4">'user\_id'</span><span class="token1">,</span> <span class="token4">'id'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre13">``` article模型 <span class="token"><</span><span class="token">?</span>php namespace app\\api\\model\\okl<span class="token1">;</span> use think\\Model<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Article</span> <span class="token2">extends</span> <span class="token3">Model</span> <span class="token1">{</span> <span class="token3">publicfunction Article</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre13">``` 控制器 namespace app\\api\\controller<span class="token1">;</span> use app\\api\\model\\User<span class="token1">;</span> use think\\Controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Onetomany</span> <span class="token2">extends</span> <span class="token3">Controller</span> <span class="token1">{</span> <span class="token3">publicfunction ceshi</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $user \<span class="token">=</span> User<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token5">2</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> $user\<span class="token">-</span><span class="token">></span>guanlian<span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` 返回数据: ``` <pre class="calibre13">``` <span class="token1">[</span> <span class="token1">{</span> <span class="token4">"id"</span><span class="token1">:</span> <span class="token5">3</span><span class="token1">,</span> <span class="token4">"content"</span><span class="token1">:</span> <span class="token4">"评论3"</span><span class="token1">,</span> <span class="token4">"user_id"</span><span class="token1">:</span> <span class="token5">2</span> <span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span> <span class="token4">"id"</span><span class="token1">:</span> <span class="token5">5</span><span class="token1">,</span> <span class="token4">"content"</span><span class="token1">:</span> <span class="token4">"评论5"</span><span class="token1">,</span> <span class="token4">"user_id"</span><span class="token1">:</span> <span class="token5">2</span> <span class="token1">}</span> <span class="token1">]</span> ``` ``` 接下来用belongto ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php namespace app\\api\\model<span class="token1">;</span> use think\\Model<span class="token1">;</span> <span class="token2">class</span> <span class="token3">User</span> <span class="token2">extends</span> <span class="token3">Model</span> <span class="token1">{</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php namespace app\\api\\model\\okl<span class="token1">;</span> use think\\Model<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Article</span> <span class="token2">extends</span> <span class="token3">Model</span> <span class="token1">{</span> <span class="token3">publicfunction guanlian</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span>$<span class="token2">this</span>\<span class="token">-</span><span class="token">></span><span class="token3">belongsTo</span><span class="token1">(</span><span class="token4">'app\\api\\model\\User'</span><span class="token1">,</span> <span class="token4">'user\_id'</span><span class="token1">,</span> <span class="token4">'id'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php namespace app\\api\\controller<span class="token1">;</span> use app\\api\\model\\okl\\Article<span class="token1">;</span> use think\\Controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Onetomany</span> <span class="token2">extends</span> <span class="token3">Controller</span> <span class="token1">{</span> <span class="token3">publicfunction ceshi</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $article \<span class="token">=</span> Article<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token5">3</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> $article\<span class="token">-</span><span class="token">></span>guanlian<span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` { "id": 2, "username": "ceshi2", "password": "mima2" } ``` <pre class="calibre14">``` <span class="token"><</span><span class="token">?</span>php namespace app\\api\\controller<span class="token1">;</span> use app\\api\\model\\User<span class="token1">;</span> use think\\Controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Onetomany</span> <span class="token2">extends</span> <span class="token3">Controller</span> <span class="token1">{</span> <span class="token3">publicfunction ceshi</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $user \<span class="token">=</span> User<span class="token1">:</span><span class="token1">:</span><span class="token2">with</span><span class="token1">(</span><span class="token4">'guanlian'</span><span class="token1">)</span>\<span class="token">-</span><span class="token">></span><span class="token3">select</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">foreach</span><span class="token1">(</span>$user <span class="token2">as</span> $data<span class="token1">)</span><span class="token1">{</span> <span class="token2">return</span><span class="token1">(</span>$data\<span class="token">-</span><span class="token">></span>guanlian<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> ``` ```