{"id":23147,"date":"2024-11-21T09:55:26","date_gmt":"2024-11-21T01:55:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23147\/"},"modified":"2024-11-21T09:55:26","modified_gmt":"2024-11-21T01:55:26","slug":"thinkphp%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8orm%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23147\/","title":{"rendered":"thinkphp\u5982\u4f55\u4f7f\u7528ORM\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c"},"content":{"rendered":"<\/p>\n<p>ThinkPHP \u662f\u4e00\u6b3e\u57fa\u4e8e PHP \u7684 Web \u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u5177\u6709\u5feb\u901f\u3001\u7b80\u4fbf\u3001\u5b89\u5168\u7684\u7279\u70b9\uff0c\u5e76\u4e14\u88ab\u5e7f\u6cdb\u5730\u4f7f\u7528\u3002\u8be5\u7cfb\u7edf\u7684ORM\u6620\u5c04\u5c42\u662f\u6700\u5173\u952e\u7684\uff0c\u5b83\u80fd\u591f\u8f7b\u677e\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u800c\u65e0\u9700\u7f16\u5199SQL\u8bed\u53e5\u3002\u4f46\u662f\u6709\u65f6\u5019\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u9700\u8981\u6392\u67e5 SQL \u8bed\u53e5\u7684\u60c5\u51b5\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u9700\u8981\u8ba9 ThinkPHP \u6267\u884c SQL\u3002<\/p>\n<p>\u5728 ThinkPHP \u4e2d\uff0c\u4f7f\u7528 ORM \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u6709\u4e24\u79cd\u60c5\u51b5\uff0c\u4e00\u79cd\u662f\u901a\u8fc7\u6a21\u578b\u6765\u64cd\u4f5c\uff0c\u53e6\u4e00\u79cd\u662f\u901a\u8fc7 Query \u7c7b\u6765\u64cd\u4f5c\u3002<\/p>\n<p><strong>1\u3001\u6a21\u578b\u64cd\u4f5c<\/strong><\/p>\n<p>\u6a21\u578b\u64cd\u4f5c\u662f\u4f7f\u7528 ORM \u6765\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7\u6a21\u578b\u64cd\u4f5c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8fdb\u884c\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u800c\u4e14\u4e0d\u9700\u8981\u7f16\u5199 SQL \u8bed\u53e5\u3002\u6709\u65f6\u9700\u8981\u4f7f\u7528\u7eaf\u7cb9\u7684 SQL \u67e5\u8be2\uff0c\u4f8b\u5982\u67e5\u627e\u67d0\u4e2a\u5217\u7684\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u7b49\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728\u6a21\u578b\u67e5\u8be2\u7684\u57fa\u7840\u4e0a\uff0cThinkPHP \u63d0\u4f9b\u4e86\u4e00\u4e2a Db \u7c7b\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u6267\u884c\u539f\u751f SQL\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>use&nbsp;thinkacadeDb;\n\n\/\/&nbsp;\u6267\u884c\u539f\u751f&nbsp;SQL\nDb::query('SELECT&nbsp;*&nbsp;FROM&nbsp;`user`&nbsp;WHERE&nbsp;`id`&nbsp;=&nbsp;1');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 Db::query() \u65b9\u6cd5\u6765\u6267\u884c\u539f\u751f SQL \u8bed\u53e5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<p><strong>2\u3001Query \u7c7b\u64cd\u4f5c<\/strong><\/p>\n<p>Query \u7c7b\u662f ThinkPHP \u4e2d\u63d0\u4f9b\u7684\u4e00\u4e2a\u94fe\u5f0f\u64cd\u4f5c\u7c7b\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u6784\u5efa SQL \u8bed\u53e5\uff0c\u5e76\u4e14\u652f\u6301\u539f\u751f SQL \u67e5\u8be2\u3002<\/p>\n<p>\u4f7f\u7528 Query \u7c7b\u64cd\u4f5c SQL \u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>use&nbsp;thinkacadeDb;\n\n$query&nbsp;=&nbsp;Db::table('user');\n\n$data&nbsp;=&nbsp;$query-&amp;gt;where('id',&nbsp;1)\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;field('id,&nbsp;name')\n&nbsp;&nbsp;&nbsp;&nbsp;-&amp;gt;select();\n\n\/\/&nbsp;\u6267\u884c\u539f\u751f&nbsp;SQL\n$sql&nbsp;=&nbsp;$query-&amp;gt;fetchSql(true)-&amp;gt;find();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7 Db::table() \u65b9\u6cd5\u6765\u83b7\u53d6\u4e00\u4e2a Query \u7c7b\u5bf9\u8c61\uff0c\u7136\u540e\u5728 Query \u7c7b\u5bf9\u8c61\u4e0a\u8fdb\u884c\u94fe\u5f0f\u64cd\u4f5c\uff0c\u4f7f\u7528 where() \u65b9\u6cd5\u6dfb\u52a0\u67e5\u8be2\u6761\u4ef6\uff0c\u4f7f\u7528 field() \u65b9\u6cd5\u9009\u62e9\u8981\u67e5\u8be2\u7684\u5b57\u6bb5\uff0c\u4f7f\u7528 select() \u65b9\u6cd5\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<p>\u5982\u679c\u9700\u8981\u6267\u884c\u539f\u751f SQL \u8bed\u53e5\uff0c\u53ea\u9700\u8981\u5728 Query \u7c7b\u5bf9\u8c61\u8c03\u7528 fetchSql(true) \u65b9\u6cd5\u5373\u53ef\uff0c\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56de\u751f\u6210\u7684 SQL \u8bed\u53e5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528ORM\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\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\u4e8e PHP \u7684 Web \u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u5177\u6709\u5feb\u901f\u3001\u7b80\u4fbf\u3001\u5b89\u5168\u7684\u7279\u70b9\uff0c\u5e76\u4e14\u88ab\u5e7f\u6cdb\u5730\u4f7f\u7528\u3002\u8be5\u7cfb\u7edf\u7684ORM\u6620\u5c04\u5c42\u662f\u6700\u5173\u952e\u7684\uff0c\u5b83\u80fd\u591f\u8f7b\u677e\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u800c\u65e0\u9700\u7f16\u5199SQL\u8bed\u53e5\u3002\u4f46\u662f\u6709\u65f6\u5019\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u9700\u8981\u6392\u67e5 SQL \u8bed\u53e5\u7684\u60c5\u51b5\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u9700\u8981\u8ba9 ThinkPHP \u6267\u884c SQL\u3002 \u5728 ThinkPHP \u4e2d\uff0c\u4f7f\u7528 ORM \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u6709\u4e24\u79cd\u60c5\u51b5\uff0c\u4e00\u79cd\u662f\u901a\u8fc7\u6a21\u578b\u6765\u64cd\u4f5c\uff0c\u53e6\u4e00\u79cd\u662f\u901a\u8fc7 Query \u7c7b\u6765\u64cd\u4f5c\u3002 1\u3001\u6a21\u578b\u64cd\u4f5c \u6a21\u578b\u64cd\u4f5c\u662f\u4f7f\u7528 ORM \u6765\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7\u6a21\u578b\u64cd\u4f5c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8fdb\u884c\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u800c\u4e14\u4e0d\u9700\u8981\u7f16\u5199 SQL \u8bed\u53e5\u3002\u6709\u65f6\u9700\u8981\u4f7f\u7528\u7eaf\u7cb9\u7684 SQL \u67e5\u8be2\uff0c\u4f8b\u5982\u67e5\u627e\u67d0\u4e2a\u5217\u7684\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u7b49\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728\u6a21\u578b\u67e5\u8be2\u7684\u57fa\u7840\u4e0a\uff0cThinkPHP \u63d0\u4f9b\u4e86\u4e00\u4e2a Db \u7c7b\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u6267\u884c\u539f\u751f SQL\uff0c\u5982\u4e0b\u6240\u793a\uff1a use&nbsp;thinkacadeDb; \/\/&nbsp;\u6267\u884c\u539f\u751f&nbsp;SQL Db::query(&#8216;SELECT&nbsp;*&nbsp;FROM&nbsp;`user`&nbsp;WHERE&nbsp;`id`&nbsp;=&nbsp;1&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 Db::query() \u65b9\u6cd5\u6765\u6267\u884c\u539f\u751f SQL \u8bed\u53e5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b\u67e5\u8be2\u7ed3\u679c\u3002 2\u3001Query \u7c7b\u64cd\u4f5c Query \u7c7b\u662f ThinkPHP \u4e2d\u63d0\u4f9b\u7684\u4e00\u4e2a\u94fe\u5f0f\u64cd\u4f5c\u7c7b\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u6784\u5efa SQL \u8bed\u53e5\uff0c\u5e76\u4e14\u652f\u6301\u539f\u751f SQL \u67e5\u8be2\u3002 \u4f7f\u7528 Query \u7c7b\u64cd\u4f5c SQL [&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-23147","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23147","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=23147"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23147\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}