{"id":24428,"date":"2024-11-21T12:43:34","date_gmt":"2024-11-21T04:43:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24428\/"},"modified":"2024-11-21T12:43:34","modified_gmt":"2024-11-21T04:43:34","slug":"thinkphp6%e6%a8%a1%e5%9e%8b%e5%85%b3%e8%81%94%e6%93%8d%e4%bd%9c%ef%bc%9a%e8%ae%a9%e6%95%b0%e6%8d%ae%e5%85%b3%e8%81%94%e6%9b%b4%e7%ae%80%e4%be%bf","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24428\/","title":{"rendered":"ThinkPHP6\u6a21\u578b\u5173\u8054\u64cd\u4f5c\uff1a\u8ba9\u6570\u636e\u5173\u8054\u66f4\u7b80\u4fbf"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169200605145921.jpg\" class=\"aligncenter\" title=\"ThinkPHP6\u6a21\u578b\u5173\u8054\u64cd\u4f5c\uff1a\u8ba9\u6570\u636e\u5173\u8054\u66f4\u7b80\u4fbf\u63d2\u56fe\" alt=\"ThinkPHP6\u6a21\u578b\u5173\u8054\u64cd\u4f5c\uff1a\u8ba9\u6570\u636e\u5173\u8054\u66f4\u7b80\u4fbf\u63d2\u56fe\" \/><\/p>\n<p>ThinkPHP\u662f\u4e00\u6b3e\u57fa\u4e8ePHP\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u5feb\u6377\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u4e86\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u53d8\u5f97\u66f4\u52a0\u7b80\u4fbf\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002\u672c\u6587\u5c06\u4ecb\u7ecdThinkPHP6\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u7684\u4e00\u4e9b\u5e38\u89c1\u7528\u6cd5\u548c\u5b9e\u4f8b\u4ee3\u7801\u3002<\/p>\n<ol>\n<li>\u4e00\u5bf9\u4e00\u5173\u8054<\/li>\n<\/ol>\n<p>\u4e00\u5bf9\u4e00\u5173\u8054\u662f\u6307\u4e24\u4e2a\u8868\u4e4b\u95f4\u53ea\u5b58\u5728\u4e00\u79cd\u5bf9\u5e94\u5173\u7cfb\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528hasOne()\u548cbelongsTo()\u65b9\u6cd5\u6765\u5efa\u7acb\u4e00\u5bf9\u4e00\u5173\u8054\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u4e24\u4e2a\u76f8\u5173\u8054\u7684\u8868\uff0c\u4f8b\u5982user\u8868\u548cprofile\u8868\u3002user\u8868\u5b58\u50a8\u7528\u6237\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u800cprofile\u8868\u5219\u5b58\u50a8\u7528\u6237\u7684\u989d\u5916\u4fe1\u606f\u3002<\/p>\n<pre>\/\/ User \u6a21\u578b\u7c7b\nnamespace appmodel;\n\nuse thinkModel;\n\nclass User extends Model\n{\n    \/\/ \u5b9a\u4e49\u4e00\u5bf9\u4e00\u5173\u8054\uff0cUser \u6a21\u578b\u5173\u8054 Profile \u6a21\u578b\n    public function profile()\n    {\n        return $this-&amp;gt;hasOne('Profile', 'user_id');\n    }\n}\n\n\/\/ Profile \u6a21\u578b\u7c7b\nnamespace appmodel;\n\nuse thinkModel;\n\nclass Profile extends Model\n{\n    \/\/ \u5b9a\u4e49\u4e00\u5bf9\u4e00\u5173\u8054\uff0cProfile \u6a21\u578b\u5173\u8054 User \u6a21\u578b\n    public function user()\n    {\n        return $this-&amp;gt;belongsTo('User', 'user_id');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u6765\u83b7\u53d6\u7528\u6237\u7684\u989d\u5916\u4fe1\u606f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ UserController.php\nnamespace appcontroller;\n\nuse appmodelUser;\n\nclass UserController\n{\n    public function index()\n    {\n        \/\/ \u83b7\u53d6\u7528\u6237\u53ca\u5176\u5173\u8054\u7684 Profile \u4fe1\u606f\n        $user = User::with('profile')-&amp;gt;find(1);\n        \n        \/\/ \u83b7\u53d6\u7528\u6237\u7684\u6635\u79f0\u548c\u5934\u50cf\n        $nickname = $user-&amp;gt;profile-&amp;gt;nickname;\n        $avatar = $user-&amp;gt;profile-&amp;gt;avatar;\n        \n        \/\/ \u5176\u4ed6\u64cd\u4f5c...\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528with(&#8216;profile&#8217;)\u65b9\u6cd5\u6765\u9884\u8f7d\u5165\u5173\u8054\u6a21\u578bProfile\uff0c\u4ece\u800c\u4e00\u6b21\u6027\u83b7\u53d6\u7528\u6237\u53ca\u5176\u5173\u8054\u7684Profile\u4fe1\u606f\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7$user-&gt;profile\u6765\u8bbf\u95ee\u7528\u6237\u7684\u989d\u5916\u4fe1\u606f\u3002<\/p>\n<ol>\n<li>\u4e00\u5bf9\u591a\u5173\u8054<\/li>\n<\/ol>\n<p>\u4e00\u5bf9\u591a\u5173\u8054\u662f\u6307\u4e00\u4e2a\u6a21\u578b\u5bf9\u5e94\u591a\u4e2a\u5176\u4ed6\u6a21\u578b\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528hasMany()\u548cbelongsTo()\u65b9\u6cd5\u6765\u5efa\u7acb\u4e00\u5bf9\u591a\u5173\u8054\u5173\u7cfb\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u76f8\u5173\u7684\u6570\u636e\u5e93\u8868\uff0c\u5206\u522b\u662fpost\u8868\u548ccomment\u8868\u3002post\u8868\u5b58\u50a8\u6587\u7ae0\u7684\u4fe1\u606f\uff0c\u800ccomment\u8868\u5b58\u50a8\u6587\u7ae0\u7684\u8bc4\u8bba\u4fe1\u606f\u3002<\/p>\n<pre>\/\/ Post \u6a21\u578b\u7c7b\nnamespace appmodel;\n\nuse thinkModel;\n\nclass Post extends Model\n{\n    \/\/ \u5b9a\u4e49\u4e00\u5bf9\u591a\u5173\u8054\uff0cPost \u6a21\u578b\u5173\u8054 Comment \u6a21\u578b\n    public function comments()\n    {\n        return $this-&amp;gt;hasMany('Comment', 'post_id');\n    }\n}\n\n\/\/ Comment \u6a21\u578b\u7c7b\nnamespace appmodel;\n\nuse thinkModel;\n\nclass Comment extends Model\n{\n    \/\/ \u5b9a\u4e49\u4e00\u5bf9\u591a\u5173\u8054\uff0cComment \u6a21\u578b\u5173\u8054 Post \u6a21\u578b\n    public function post()\n    {\n        return $this-&amp;gt;belongsTo('Post', 'post_id');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\/\/ PostController.php\nnamespace appcontroller;\n\nuse appmodelPost;\n\nclass PostController\n{\n    public function show($id)\n    {\n        \/\/ \u83b7\u53d6\u6587\u7ae0\u53ca\u5176\u5173\u8054\u7684\u8bc4\u8bba\u4fe1\u606f\n        $post = Post::with('comments')-&amp;gt;find($id);\n        \n        \/\/ \u83b7\u53d6\u6587\u7ae0\u7684\u6807\u9898\u548c\u5185\u5bb9\n        $title = $post-&amp;gt;title;\n        $content = $post-&amp;gt;content;\n        \n        \/\/ \u83b7\u53d6\u6587\u7ae0\u7684\u8bc4\u8bba\u6570\u7ec4\n        $comments = $post-&amp;gt;comments;\n        \n        \/\/ \u5176\u4ed6\u64cd\u4f5c...\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528with(&#8216;comments&#8217;)\u65b9\u6cd5\u6765\u9884\u8f7d\u5165\u5173\u8054\u6a21\u578bComment\uff0c\u4ece\u800c\u4e00\u6b21\u6027\u83b7\u53d6\u6587\u7ae0\u53ca\u5176\u5173\u8054\u7684\u8bc4\u8bba\u4fe1\u606f\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7$post-&gt;comments\u6765\u8bbf\u95ee\u6587\u7ae0\u7684\u8bc4\u8bba\u6570\u7ec4\u3002<\/p>\n<ol>\n<li>\u591a\u5bf9\u591a\u5173\u8054<\/li>\n<\/ol>\n<p>\u591a\u5bf9\u591a\u5173\u8054\u662f\u6307\u4e24\u4e2a\u6a21\u578b\u4e4b\u95f4\u5b58\u5728\u591a\u79cd\u5bf9\u5e94\u5173\u7cfb\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528belongsToMany()\u65b9\u6cd5\u6765\u5efa\u7acb\u591a\u5bf9\u591a\u5173\u8054\u5173\u7cfb\u3002<\/p>\n<pre>\/\/ User \u6a21\u578b\u7c7b\nnamespace appmodel;\n\nuse thinkModel;\n\nclass User extends Model\n{\n    \/\/ \u5b9a\u4e49\u591a\u5bf9\u591a\u5173\u8054\uff0cUser \u6a21\u578b\u5173\u8054 Role \u6a21\u578b\n    public function roles()\n    {\n        return $this-&amp;gt;belongsToMany('Role', 'user_role');\n    }\n}\n\n\/\/ Role \u6a21\u578b\u7c7b\nnamespace appmodel;\n\nuse thinkModel;\n\nclass Role extends Model\n{\n    \/\/ \u5b9a\u4e49\u591a\u5bf9\u591a\u5173\u8054\uff0cRole \u6a21\u578b\u5173\u8054 User \u6a21\u578b\n    public function users()\n    {\n        return $this-&amp;gt;belongsToMany('User', 'user_role');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\/\/ UserController.php\nnamespace appcontroller;\n\nuse appmodelUser;\n\nclass UserController\n{\n    public function showRoles($id)\n    {\n        \/\/ \u83b7\u53d6\u7528\u6237\u53ca\u5176\u5173\u8054\u7684\u89d2\u8272\u4fe1\u606f\n        $user = User::with('roles')-&amp;gt;find($id);\n        \n        \/\/ \u83b7\u53d6\u7528\u6237\u7684\u89d2\u8272\u6570\u7ec4\n        $roles = $user-&amp;gt;roles;\n        \n        \/\/ \u5176\u4ed6\u64cd\u4f5c...\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528with(&#8216;roles&#8217;)\u65b9\u6cd5\u6765\u9884\u8f7d\u5165\u5173\u8054\u6a21\u578bRole\uff0c\u4ece\u800c\u4e00\u6b21\u6027\u83b7\u53d6\u7528\u6237\u53ca\u5176\u5173\u8054\u7684\u89d2\u8272\u4fe1\u606f\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7$user-&gt;roles\u6765\u8bbf\u95ee\u7528\u6237\u7684\u89d2\u8272\u6570\u7ec4\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86ThinkPHP6\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u7684\u4e00\u4e9b\u5e38\u89c1\u7528\u6cd5\u548c\u5b9e\u4f8b\u4ee3\u7801\u3002\u901a\u8fc7\u4f7f\u7528\u6a21\u578b\u5173\u8054\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7b80\u4fbf\u5730\u5904\u7406\u6570\u636e\u5173\u8054\uff0c\u4ece\u800c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u9884\u8f7d\u5165\u6280\u672f\u6765\u51cf\u5c11\u67e5\u8be2\u6b21\u6570\uff0c\u4f18\u5316\u6570\u636e\u5e93\u8bbf\u95ee\u6027\u80fd\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u5230\u5927\u5bb6\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528ThinkPHP6\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6\u6a21\u578b\u5173\u8054\u64cd\u4f5c\uff1a\u8ba9\u6570\u636e\u5173\u8054\u66f4\u7b80\u4fbf\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ThinkPHP\u662f\u4e00\u6b3e\u57fa\u4e8ePHP\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u5feb\u6377\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u4e86\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u53d8\u5f97\u66f4\u52a0\u7b80\u4fbf\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002\u672c\u6587\u5c06\u4ecb\u7ecdThinkPHP6\u6a21\u578b\u5173\u8054\u64cd\u4f5c\u7684\u4e00\u4e9b\u5e38\u89c1\u7528\u6cd5\u548c\u5b9e\u4f8b\u4ee3\u7801\u3002 \u4e00\u5bf9\u4e00\u5173\u8054 \u4e00\u5bf9\u4e00\u5173\u8054\u662f\u6307\u4e24\u4e2a\u8868\u4e4b\u95f4\u53ea\u5b58\u5728\u4e00\u79cd\u5bf9\u5e94\u5173\u7cfb\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528hasOne()\u548cbelongsTo()\u65b9\u6cd5\u6765\u5efa\u7acb\u4e00\u5bf9\u4e00\u5173\u8054\u3002 \u9996\u5148\uff0c\u5728\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u4e24\u4e2a\u76f8\u5173\u8054\u7684\u8868\uff0c\u4f8b\u5982user\u8868\u548cprofile\u8868\u3002user\u8868\u5b58\u50a8\u7528\u6237\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u800cprofile\u8868\u5219\u5b58\u50a8\u7528\u6237\u7684\u989d\u5916\u4fe1\u606f\u3002 \/\/ User \u6a21\u578b\u7c7b namespace appmodel; use thinkModel; class User extends Model { \/\/ \u5b9a\u4e49\u4e00\u5bf9\u4e00\u5173\u8054\uff0cUser \u6a21\u578b\u5173\u8054 Profile \u6a21\u578b public function profile() { return $this-&amp;gt;hasOne(&#8216;Profile&#8217;, &#8216;user_id&#8217;); } } \/\/ Profile \u6a21\u578b\u7c7b namespace appmodel; use thinkModel; class Profile extends Model { \/\/ \u5b9a\u4e49\u4e00\u5bf9\u4e00\u5173\u8054\uff0cProfile \u6a21\u578b\u5173\u8054 User \u6a21\u578b public function user() { return $this-&amp;gt;belongsTo(&#8216;User&#8217;, &#8216;user_id&#8217;); } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24428","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24428"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24428\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}