{"id":6732,"date":"2024-11-14T13:18:39","date_gmt":"2024-11-14T05:18:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6732\/"},"modified":"2024-11-14T13:18:39","modified_gmt":"2024-11-14T05:18:39","slug":"thinkphp6-%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-with-%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%e5%b9%b6%e6%89%81%e5%b9%b3%e5%8c%96%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6732\/","title":{"rendered":"ThinkPHP6 \u4e2d\u5982\u4f55\u4f7f\u7528 with() \u5173\u8054\u67e5\u8be2\u5e76\u6241\u5e73\u5316\u4e8c\u7ef4\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172983952855049.jpg\" class=\"aligncenter\" title=\"ThinkPHP6 \u4e2d\u5982\u4f55\u4f7f\u7528 with() \u5173\u8054\u67e5\u8be2\u5e76\u6241\u5e73\u5316\u4e8c\u7ef4\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"ThinkPHP6 \u4e2d\u5982\u4f55\u4f7f\u7528 with() \u5173\u8054\u67e5\u8be2\u5e76\u6241\u5e73\u5316\u4e8c\u7ef4\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>6 \u4e2d\u4f7f\u7528 with() \u5173\u8054\u67e5\u8be2\u5e76\u6241\u5e73\u5316\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 thinkphp6 \u4e2d\uff0c\u4f7f\u7528 with() \u65b9\u6cd5\u53ef\u4ee5\u5173\u8054\u67e5\u8be2\u591a\u4e2a\u6a21\u578b\u6570\u636e\u3002\u5982\u679c\u5173\u8054\u6a21\u578b\u5b58\u5728\u591a\u5bf9\u4e00\u5173\u8054\u5173\u7cfb\uff0c\u67e5\u8be2\u7ed3\u679c\u5c06\u8fd4\u56de\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5305\u542b\u7236\u6a21\u578b\u548c\u5b50\u6a21\u578b\u7684\u6570\u636e\u3002<\/p>\n<p>\u7136\u800c\uff0c\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u5c06\u4e8c\u7ef4\u6570\u7ec4\u6241\u5e73\u5316\uff0c\u7c7b\u4f3c\u4e8e sql \u4e2d\u7684 join \u67e5\u8be2\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 bindattrs() \u65b9\u6cd5\uff0c\u5c06\u5b50\u6a21\u578b\u7684\u5c5e\u6027\u7ed1\u5b9a\u5230\u7236\u6a21\u578b\u3002<\/p>\n<p><strong>\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff0ccard \u8868\u4e0e profile \u8868\u901a\u8fc7\u4e00\u5bf9\u4e00\u5173\u8054\u5173\u7cfb\u5173\u8054\uff1a<\/p>\n<pre>public static function get_card_store_list()\n{\n    return self::with(['profile'])\n        -&gt;select()\n        -&gt;appendto('profile')\n        -&gt;toarray();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5305\u542b card \u8868\u548c profile \u8868\u7684\u6570\u636e\uff1a<\/p>\n<pre>[\n {\n    \"id\": 1,\n    \"times\": 10,\n    \"add_time\": 1626933885\n }\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u6570\u7ec4\u6241\u5e73\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 bindattrs() \u65b9\u6cd5\uff1a<\/p>\n<pre>return self::with(['profile'])\n    -&gt;select()\n    -&gt;bindattrs('profile')\n    -&gt;toarray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u5305\u542b card \u8868\u548c profile \u8868\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n<pre>[\n {\n    \"id\": 1,\n    \"times\": 10,\n    \"add_time\": 1626933885\n }\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 bindattrs() \u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u6241\u5e73\u5316\u6a21\u578b\u5173\u8054\u67e5\u8be2\u7684\u7ed3\u679c\uff0c\u6d88\u9664\u975e\u522b\u540d\u51b2\u7a81\uff0c\u5e76\u83b7\u53d6\u7c7b\u4f3c\u4e8e sql join \u67e5\u8be2\u7684\u5355\u7ef4\u6570\u7ec4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6 \u4e2d\u5982\u4f55\u4f7f\u7528 with() \u5173\u8054\u67e5\u8be2\u5e76\u6241\u5e73\u5316\u4e8c\u7ef4\u6570\u7ec4\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>6 \u4e2d\u4f7f\u7528 with() \u5173\u8054\u67e5\u8be2\u5e76\u6241\u5e73\u5316\u4e8c\u7ef4\u6570\u7ec4 \u5728 thinkphp6 \u4e2d\uff0c\u4f7f\u7528 with() \u65b9\u6cd5\u53ef\u4ee5\u5173\u8054\u67e5\u8be2\u591a\u4e2a\u6a21\u578b\u6570\u636e\u3002\u5982\u679c\u5173\u8054\u6a21\u578b\u5b58\u5728\u591a\u5bf9\u4e00\u5173\u8054\u5173\u7cfb\uff0c\u67e5\u8be2\u7ed3\u679c\u5c06\u8fd4\u56de\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5305\u542b\u7236\u6a21\u578b\u548c\u5b50\u6a21\u578b\u7684\u6570\u636e\u3002 \u7136\u800c\uff0c\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u5c06\u4e8c\u7ef4\u6570\u7ec4\u6241\u5e73\u5316\uff0c\u7c7b\u4f3c\u4e8e sql \u4e2d\u7684 join \u67e5\u8be2\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 bindattrs() \u65b9\u6cd5\uff0c\u5c06\u5b50\u6a21\u578b\u7684\u5c5e\u6027\u7ed1\u5b9a\u5230\u7236\u6a21\u578b\u3002 \u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff0ccard \u8868\u4e0e profile \u8868\u901a\u8fc7\u4e00\u5bf9\u4e00\u5173\u8054\u5173\u7cfb\u5173\u8054\uff1a public static function get_card_store_list() { return self::with([&#8216;profile&#8217;]) -&gt;select() -&gt;appendto(&#8216;profile&#8217;) -&gt;toarray(); } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5305\u542b card \u8868\u548c profile \u8868\u7684\u6570\u636e\uff1a [ { &#8220;id&#8221;: 1, &#8220;times&#8221;: 10, &#8220;add_time&#8221;: 1626933885 } ] \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u6570\u7ec4\u6241\u5e73\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 bindattrs() \u65b9\u6cd5\uff1a return [&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-6732","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6732","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=6732"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6732\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}