{"id":23174,"date":"2024-11-21T15:45:07","date_gmt":"2024-11-21T07:45:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23174\/"},"modified":"2024-11-21T15:45:07","modified_gmt":"2024-11-21T07:45:07","slug":"thinkphp5%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%8d%e5%8f%98%e7%9a%84%e6%95%b0%e6%8d%ae%e6%9b%b4%e6%96%b0%e4%b8%ba0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23174\/","title":{"rendered":"thinkphp5\u5982\u4f55\u5c06\u4e0d\u53d8\u7684\u6570\u636e\u66f4\u65b0\u4e3a0"},"content":{"rendered":"<\/p>\n<p>ThinkPHP 5\u662f\u4e00\u79cd\u5f00\u6e90\u7684PHP\u6846\u67b6\uff0c\u80fd\u591f\u663e\u8457\u63d0\u9ad8Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u6548\u7387\u3002\u5176\u4e2d\uff0c\u6d89\u53ca\u5230\u6570\u636e\u7684\u67e5\u8be2\u3001\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u662f\u4f7f\u7528\u9891\u7387\u6700\u9ad8\u7684\u90e8\u5206\u4e4b\u4e00\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5728\u4f7f\u7528ThinkPHP 5\u6846\u67b6\u65f6\uff0c\u5982\u4f55\u5c06\u4e0d\u9700\u8981\u66f4\u65b0\u7684\u6570\u636e\u66f4\u65b0\u4e3a0\u3002<\/p>\n<p>\u4f7f\u7528ORM\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5728ThinkPHP 5\u4e2d\u64cd\u4f5c\u6570\u636e\u975e\u5e38\u65b9\u4fbf\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u6a21\u578b\u5bf9\u8c61\u7684save()\u65b9\u6cd5\u6765\u66f4\u65b0\u6570\u636e\u3002\u5f53\u4f7f\u7528save()\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u8868\u5355\u4e2d\u67d0\u4e2a\u5b57\u6bb5\u6ca1\u6709\u88ab\u4fee\u6539\uff0c\u5219\u8be5\u5b57\u6bb5\u7684\u503c\u5c06\u4e0d\u4f1a\u88ab\u66f4\u65b0\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u62e5\u6709\u4e00\u5f20\u7528\u6237\u4fe1\u606f\u8868\u683c\uff0c\u5176\u4e2d\u6db5\u76d6\u4e86\u7528\u6237\u7684ID\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u6027\u522b\u548c\u5e74\u9f84\u7b49\u8be6\u7ec6\u4fe1\u606f\u3002\u7528\u6237\u4ec5\u4fee\u6539\u4e86\u5bc6\u7801\u548c\u5e74\u9f84\u5b57\u6bb5\uff0c\u7528\u6237\u540d\u548c\u6027\u522b\u5b57\u6bb5\u672a\u4f5c\u4efb\u4f55\u66f4\u6539\u3002\u6267\u884c\u66f4\u65b0\u64cd\u4f5c\u65f6\uff0csave\uff08\uff09\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u7528\u6237\u540d\u548c\u6027\u522b\u5b57\u6bb5\u7684\u503c\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u5c06\u4e0d\u53d8\u7684\u6570\u636e\u66f4\u65b0\u4e3a0\uff0c\u6211\u4eec\u8be5\u5982\u4f55\u505a\u5462\uff1f<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u91cd\u5199\u6a21\u578b\u5bf9\u8c61\u7684save()\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>1.\u5728\u6a21\u578b\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u6570\u7ec4$zero_fields\uff0c\u7528\u4e8e\u5b58\u50a8\u9700\u8981\u88ab\u66f4\u65b0\u4e3a0\u7684\u5b57\u6bb5\u6570\u7ec4\u3002<\/p>\n<pre>protected&nbsp;static&nbsp;$zero_fields&nbsp;=&nbsp;['field1',&nbsp;'field2',&nbsp;...];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2.\u5728save()\u65b9\u6cd5\u4e2d\uff0c\u5224\u65ad\u9700\u8981\u88ab\u66f4\u65b0\u4e3a0\u7684\u5b57\u6bb5\u662f\u5426\u5df2\u7ecf\u6709\u503c\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u5c06\u5176\u503c\u66f4\u65b0\u4e3a0\uff0c\u6700\u540e\u518d\u8c03\u7528\u7236\u7c7b\u7684save()\u65b9\u6cd5\u8fdb\u884c\u5176\u4ed6\u5b57\u6bb5\u7684\u66f4\u65b0\u3002<\/p>\n<pre>public&nbsp;function&nbsp;save(array&nbsp;$data&nbsp;=&nbsp;[],&nbsp;$where&nbsp;=&nbsp;[],&nbsp;$sequence&nbsp;=&nbsp;null)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(self::$zero_fields&nbsp;as&nbsp;$field)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!isset($data[$field]))&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data[$field]&nbsp;=&nbsp;0;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;parent::save($data,&nbsp;$where,&nbsp;$sequence);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u91cd\u5199save()\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9488\u5bf9\u6027\u5730\u5bf9\u9700\u8981\u66f4\u65b0\u7684\u5b57\u6bb5\u8fdb\u884c\u5904\u7406\uff0c\u5c06\u4e0d\u53d8\u7684\u5b57\u6bb5\u66f4\u65b0\u4e3a0\uff0c\u907f\u514d\u4e86\u8868\u5355\u63d0\u4ea4\u7684\u4e0d\u5b8c\u6574\u6570\u636e\u5bf9\u6570\u636e\u5e93\u9020\u6210\u7684\u5f71\u54cd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f5\u5982\u4f55\u5c06\u4e0d\u53d8\u7684\u6570\u636e\u66f4\u65b0\u4e3a0\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 5\u662f\u4e00\u79cd\u5f00\u6e90\u7684PHP\u6846\u67b6\uff0c\u80fd\u591f\u663e\u8457\u63d0\u9ad8Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u6548\u7387\u3002\u5176\u4e2d\uff0c\u6d89\u53ca\u5230\u6570\u636e\u7684\u67e5\u8be2\u3001\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u662f\u4f7f\u7528\u9891\u7387\u6700\u9ad8\u7684\u90e8\u5206\u4e4b\u4e00\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5728\u4f7f\u7528ThinkPHP 5\u6846\u67b6\u65f6\uff0c\u5982\u4f55\u5c06\u4e0d\u9700\u8981\u66f4\u65b0\u7684\u6570\u636e\u66f4\u65b0\u4e3a0\u3002 \u4f7f\u7528ORM\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5728ThinkPHP 5\u4e2d\u64cd\u4f5c\u6570\u636e\u975e\u5e38\u65b9\u4fbf\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u6a21\u578b\u5bf9\u8c61\u7684save()\u65b9\u6cd5\u6765\u66f4\u65b0\u6570\u636e\u3002\u5f53\u4f7f\u7528save()\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u8868\u5355\u4e2d\u67d0\u4e2a\u5b57\u6bb5\u6ca1\u6709\u88ab\u4fee\u6539\uff0c\u5219\u8be5\u5b57\u6bb5\u7684\u503c\u5c06\u4e0d\u4f1a\u88ab\u66f4\u65b0\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u62e5\u6709\u4e00\u5f20\u7528\u6237\u4fe1\u606f\u8868\u683c\uff0c\u5176\u4e2d\u6db5\u76d6\u4e86\u7528\u6237\u7684ID\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u6027\u522b\u548c\u5e74\u9f84\u7b49\u8be6\u7ec6\u4fe1\u606f\u3002\u7528\u6237\u4ec5\u4fee\u6539\u4e86\u5bc6\u7801\u548c\u5e74\u9f84\u5b57\u6bb5\uff0c\u7528\u6237\u540d\u548c\u6027\u522b\u5b57\u6bb5\u672a\u4f5c\u4efb\u4f55\u66f4\u6539\u3002\u6267\u884c\u66f4\u65b0\u64cd\u4f5c\u65f6\uff0csave\uff08\uff09\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u7528\u6237\u540d\u548c\u6027\u522b\u5b57\u6bb5\u7684\u503c\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u5c06\u4e0d\u53d8\u7684\u6570\u636e\u66f4\u65b0\u4e3a0\uff0c\u6211\u4eec\u8be5\u5982\u4f55\u505a\u5462\uff1f \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u91cd\u5199\u6a21\u578b\u5bf9\u8c61\u7684save()\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1.\u5728\u6a21\u578b\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u6570\u7ec4$zero_fields\uff0c\u7528\u4e8e\u5b58\u50a8\u9700\u8981\u88ab\u66f4\u65b0\u4e3a0\u7684\u5b57\u6bb5\u6570\u7ec4\u3002 protected&nbsp;static&nbsp;$zero_fields&nbsp;=&nbsp;[&#8216;field1&#8217;,&nbsp;&#8216;field2&#8217;,&nbsp;&#8230;]; \u767b\u5f55\u540e\u590d\u5236 2.\u5728save()\u65b9\u6cd5\u4e2d\uff0c\u5224\u65ad\u9700\u8981\u88ab\u66f4\u65b0\u4e3a0\u7684\u5b57\u6bb5\u662f\u5426\u5df2\u7ecf\u6709\u503c\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u5c06\u5176\u503c\u66f4\u65b0\u4e3a0\uff0c\u6700\u540e\u518d\u8c03\u7528\u7236\u7c7b\u7684save()\u65b9\u6cd5\u8fdb\u884c\u5176\u4ed6\u5b57\u6bb5\u7684\u66f4\u65b0\u3002 public&nbsp;function&nbsp;save(array&nbsp;$data&nbsp;=&nbsp;[],&nbsp;$where&nbsp;=&nbsp;[],&nbsp;$sequence&nbsp;=&nbsp;null) { &nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(self::$zero_fields&nbsp;as&nbsp;$field)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!isset($data[$field]))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data[$field]&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;parent::save($data,&nbsp;$where,&nbsp;$sequence); } \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7\u91cd\u5199save()\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9488\u5bf9\u6027\u5730\u5bf9\u9700\u8981\u66f4\u65b0\u7684\u5b57\u6bb5\u8fdb\u884c\u5904\u7406\uff0c\u5c06\u4e0d\u53d8\u7684\u5b57\u6bb5\u66f4\u65b0\u4e3a0\uff0c\u907f\u514d\u4e86\u8868\u5355\u63d0\u4ea4\u7684\u4e0d\u5b8c\u6574\u6570\u636e\u5bf9\u6570\u636e\u5e93\u9020\u6210\u7684\u5f71\u54cd\u3002 \u4ee5\u4e0a\u5c31\u662f5\u5982\u4f55\u5c06\u4e0d\u53d8\u7684\u6570\u636e\u66f4\u65b0\u4e3a0\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-23174","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23174","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=23174"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23174\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}