{"id":23720,"date":"2024-11-21T16:47:17","date_gmt":"2024-11-21T08:47:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23720\/"},"modified":"2024-11-21T16:47:17","modified_gmt":"2024-11-21T08:47:17","slug":"thinkphp5%e6%b7%bb%e5%8a%a0%e6%b1%82%e5%92%8c%e6%96%b9%e6%b3%95%e7%9a%84%e6%b5%81%e7%a8%8b-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23720\/","title":{"rendered":"thinkphp5\u6dfb\u52a0\u6c42\u548c\u65b9\u6cd5\u7684\u6d41\u7a0b"},"content":{"rendered":"<p>\u968f\u7740 php \u6280\u672f\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5f00\u53d1\u8005\u5f00\u59cb\u5173\u6ce8 php \u6846\u67b6\uff0c\u5176\u4e2d thinkphp 5 \u662f\u4e00\u6b3e\u5168\u9762\u7684 php \u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u51cf\u5c11\u4e86\u7a0b\u5e8f\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u91cf\u3002\u800c\u5176\u4e2d\u7684\u6c42\u548c\u65b9\u6cd5\u662f\u5f88\u591a\u5f00\u53d1\u8005\u5fc5\u987b\u505a\u7684\u4e00\u4e2a\u64cd\u4f5c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60 thinkphp 5 \u4e2d\u7684\u6dfb\u52a0\u6c42\u548c\u65b9\u6cd5\u7684\u6d41\u7a0b\u3002<\/p>\n<p>\u7b2c\u4e00\u6b65\uff1a\u521b\u5efa Model \u6587\u4ef6<\/p>\n<p>\u5728 ThinkPHP 5 \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u521b\u5efa Model \u6587\u4ef6\u6765\u5b8c\u6210\u6570\u636e\u64cd\u4f5c\u3002\u6211\u4eec\u9700\u8981\u5728 application \u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3a Sum.php \u7684\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>namespace appindexmodel;\nuse thinkModel;\n\nclass Sum extends Model\n{\n    \/\/ \u6c42\u548c\u65b9\u6cd5\n    public function sum()\n    {\n        $result = $this-&gt;sum('field');\n        return $result;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8be5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a Sum \u7684 Model \u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a sum \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f7f\u7528\u4e86 sum \u51fd\u6570\u5bf9\u6307\u5b9a\u5b57\u6bb5 field \u8fdb\u884c\u4e86\u6c42\u548c\u64cd\u4f5c\uff0c\u5e76\u8fd4\u56de\u6c42\u548c\u7ed3\u679c\u3002<\/p>\n<p>\u7b2c\u4e8c\u6b65\uff1a\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728 ThinkPHP 5 \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u6211\u4eec\u9700\u8981\u5728 application \u76ee\u5f55\u4e0b\u7684 database.php \u6587\u4ef6\u4e2d\u8fdb\u884c\u914d\u7f6e\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>return [\n    \/\/ \u6570\u636e\u5e93\u7c7b\u578b\n    'type'            =&gt; 'mysql',\n    \/\/ \u670d\u52a1\u5668\u5730\u5740\n    'hostname'        =&gt; 'localhost',\n    \/\/ \u6570\u636e\u5e93\u540d\n    'database'        =&gt; 'test',\n    \/\/ \u7528\u6237\u540d\n    'username'        =&gt; 'root',\n    \/\/ \u5bc6\u7801\n    'password'        =&gt; '',\n    \/\/ \u7aef\u53e3\n    'hostport'        =&gt; '',\n    \/\/ \u8fde\u63a5dsn\n    'dsn'             =&gt; '',\n    \/\/ \u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\n    'params'          =&gt; [],\n    \/\/ \u6570\u636e\u5e93\u7f16\u7801\u9ed8\u8ba4\u91c7\u7528utf8\n    'charset'         =&gt; 'utf8',\n    \/\/ \u6570\u636e\u5e93\u8868\u524d\u7f00\n    'prefix'          =&gt; 'tp_',\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8be5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u914d\u7f6e\u4e86\u6570\u636e\u5e93\u7684\u8fde\u63a5\u4fe1\u606f\uff0c\u5305\u62ec\u6570\u636e\u5e93\u7c7b\u578b\u3001\u670d\u52a1\u5668\u5730\u5740\u3001\u6570\u636e\u5e93\u540d\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u3002\u786e\u4fdd\u8be5\u914d\u7f6e\u548c\u4f60\u7684\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u4e00\u81f4\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65\uff1a\u4f7f\u7528 Model \u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c<\/p>\n<p>\u5728\u914d\u7f6e\u597d Model \u6587\u4ef6\u548c\u6570\u636e\u5e93\u8fde\u63a5\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 Model \u6587\u4ef6\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c\u4e86\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>use appindexmodelSum;\n\n$sum = new Sum();\n$result = $sum-&gt;sum();\necho $result;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8be5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 Sum Model \u4e2d\u7684 sum \u65b9\u6cd5\uff0c\u5e76\u5c06\u6c42\u548c\u7ed3\u679c\u8f93\u51fa\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u6210\u529f\u5730\u5728 ThinkPHP 5 \u4e2d\u6c42\u548c\u4e86\u6307\u5b9a\u5b57\u6bb5\u3002<\/p>\n<p>\u7ed3\u8bba<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u5728 ThinkPHP 5 \u4e2d\u5b9e\u73b0\u6c42\u548c\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a Sum Model \u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u4e86 sum \u65b9\u6cd5\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u4e86\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\uff0c\u5e76\u4f7f\u7528 Model \u6587\u4ef6\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u4e86\u64cd\u4f5c\u3002\u6211\u5efa\u8bae\u4f60\u4eb2\u81ea\u5b9e\u8df5\u4e00\u4e0b\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3 ThinkPHP 5 \u4e2d\u7684\u6c42\u548c\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp5\u6dfb\u52a0\u6c42\u548c\u65b9\u6cd5\u7684\u6d41\u7a0b\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>\u968f\u7740 php \u6280\u672f\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5f00\u53d1\u8005\u5f00\u59cb\u5173\u6ce8 php \u6846\u67b6\uff0c\u5176\u4e2d thinkphp 5 \u662f\u4e00\u6b3e\u5168\u9762\u7684 php \u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u51cf\u5c11\u4e86\u7a0b\u5e8f\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u91cf\u3002\u800c\u5176\u4e2d\u7684\u6c42\u548c\u65b9\u6cd5\u662f\u5f88\u591a\u5f00\u53d1\u8005\u5fc5\u987b\u505a\u7684\u4e00\u4e2a\u64cd\u4f5c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60 thinkphp 5 \u4e2d\u7684\u6dfb\u52a0\u6c42\u548c\u65b9\u6cd5\u7684\u6d41\u7a0b\u3002 \u7b2c\u4e00\u6b65\uff1a\u521b\u5efa Model \u6587\u4ef6 \u5728 ThinkPHP 5 \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u521b\u5efa Model \u6587\u4ef6\u6765\u5b8c\u6210\u6570\u636e\u64cd\u4f5c\u3002\u6211\u4eec\u9700\u8981\u5728 application \u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3a Sum.php \u7684\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a namespace appindexmodel; use thinkModel; class Sum extends Model { \/\/ \u6c42\u548c\u65b9\u6cd5 public function sum() { $result = $this-&gt;sum(&#8216;field&#8217;); return $result; } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8be5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a Sum \u7684 Model \u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a sum [&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-23720","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23720","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=23720"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23720\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}