{"id":22938,"date":"2024-11-21T14:55:58","date_gmt":"2024-11-21T06:55:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22938\/"},"modified":"2024-11-21T14:55:58","modified_gmt":"2024-11-21T06:55:58","slug":"thinkphp%e4%b8%ad%e7%9a%84m%e6%96%b9%e6%b3%95%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22938\/","title":{"rendered":"thinkphp\u4e2d\u7684m\u65b9\u6cd5\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<\/p>\n<p>\u4e2d\u7684m\u65b9\u6cd5\u662fModel\u65b9\u6cd5\u7684\u7f29\u5199\u3002\u901a\u5e38\u7528\u4e8e\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u6570\u636e\u6301\u4e45\u5316\u7684\u5b9e\u73b0\u662fModel\uff0c\u5b83\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u4e00\u90e8\u5206\u3002Model\u7c7b\u5728thinkphp\u6846\u67b6\u4e2d\u63d0\u4f9b\u4e00\u7ec4\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u589e\u5220\u6539\u67e5\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u4f7f\u7528thinkphp\u7684m\u65b9\u6cd5\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u64cd\u4f5cModel\u7c7b\uff0c\u8fdb\u884c\u6570\u636e\u5e93\u7684\u8bfb\u5199\u3002m\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<\/p>\n<pre>$model&nbsp;=&nbsp;M('table');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c\u2019table\u2019\u662f\u6570\u636e\u5e93\u4e2d\u7684\u8868\u540d\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6216\u8005\u53d8\u91cf\u3002<\/p>\n<p>M\u65b9\u6cd5\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u6570\u636e\u5e93\u8868\u540d\uff0c\u540e\u9762\u7684\u53c2\u6570\u4e3a\u6570\u636e\u5e93\u914d\u7f6e\u4fe1\u606f\uff0c\u5305\u62ec\u6570\u636e\u5e93\u7684\u5730\u5740\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u7b49\u3002\u5982\u679c\u4e0d\u8bbe\u7f6e\u540e\u9762\u7684\u53c2\u6570\uff0c\u5219\u4f1a\u81ea\u52a8\u52a0\u8f7d\u9879\u76ee\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4f7f\u7528m\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u521b\u5efaModel\u7c7b\u7684\u5b9e\u4f8b\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u5176\u4ed6\u975eModel\u7c7b\u7684\u5b9e\u4f8b\u3002\u901a\u8fc7\u4f7f\u7528m\u65b9\u6cd5\u5b9e\u4f8b\u5316\u7c7b\uff0c\u60a8\u53ef\u4ee5\u5bf9\u8be5\u7c7b\u6267\u884c\u76f8\u5e94\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u8c03\u7528Model\u7c7b\u6240\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>$user&nbsp;=&nbsp;M('user');\n$user-&amp;gt;where('id=1')-&amp;gt;find();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a$user\u7684Model\u7c7b\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528where()\u65b9\u6cd5\u548cfind()\u65b9\u6cd5\u67e5\u8be2\u4e86\u6570\u636e\u5e93\u8868\u4e2dID\u4e3a1\u7684\u8bb0\u5f55\uff0c\u7136\u540e\u5c06\u67e5\u8be2\u7ed3\u679c\u5b58\u50a8\u5728$user\u53d8\u91cf\u4e2d\u3002<\/p>\n<p>\u9664\u4e86\u67e5\u8be2\uff0cm\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u7684\u589e\u52a0\u3001\u4fee\u6539\u548c\u5220\u9664\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u589e\u52a0\u6570\u636e\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>$user&nbsp;=&nbsp;M('user');\n$user-&amp;gt;username&nbsp;=&nbsp;'test';\n$user-&amp;gt;password&nbsp;=&nbsp;md5('123456');\n$user-&amp;gt;add();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a$user\u7684Model\u7c7b\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528add()\u65b9\u6cd5\u5411\u6570\u636e\u5e93\u8868\u4e2d\u589e\u52a0\u4e86\u4e00\u6761\u8bb0\u5f55\uff0c\u5176\u4e2d\u5305\u62ecusername\u548cpassword\u4e24\u4e2a\u5b57\u6bb5\uff0c\u5bc6\u7801\u901a\u8fc7md5\u51fd\u6570\u52a0\u5bc6\u3002<\/p>\n<p>\u9664\u4e86\u4ee5\u4e0a\u793a\u4f8b\u4e2d\u7684\u5e38\u7528\u64cd\u4f5c\uff0cModel\u7c7b\u8fd8\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u5982join()\u3001order()\u3001limit()\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u7075\u6d3b\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2d\u7684m\u65b9\u6cd5\u600e\u4e48\u4f7f\u7528\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>\u4e2d\u7684m\u65b9\u6cd5\u662fModel\u65b9\u6cd5\u7684\u7f29\u5199\u3002\u901a\u5e38\u7528\u4e8e\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u6570\u636e\u6301\u4e45\u5316\u7684\u5b9e\u73b0\u662fModel\uff0c\u5b83\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u4e00\u90e8\u5206\u3002Model\u7c7b\u5728thinkphp\u6846\u67b6\u4e2d\u63d0\u4f9b\u4e00\u7ec4\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u589e\u5220\u6539\u67e5\u7b49\u64cd\u4f5c\u3002 \u4f7f\u7528thinkphp\u7684m\u65b9\u6cd5\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u64cd\u4f5cModel\u7c7b\uff0c\u8fdb\u884c\u6570\u636e\u5e93\u7684\u8bfb\u5199\u3002m\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a $model&nbsp;=&nbsp;M(&#8216;table&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c\u2019table\u2019\u662f\u6570\u636e\u5e93\u4e2d\u7684\u8868\u540d\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6216\u8005\u53d8\u91cf\u3002 M\u65b9\u6cd5\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u6570\u636e\u5e93\u8868\u540d\uff0c\u540e\u9762\u7684\u53c2\u6570\u4e3a\u6570\u636e\u5e93\u914d\u7f6e\u4fe1\u606f\uff0c\u5305\u62ec\u6570\u636e\u5e93\u7684\u5730\u5740\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u7b49\u3002\u5982\u679c\u4e0d\u8bbe\u7f6e\u540e\u9762\u7684\u53c2\u6570\uff0c\u5219\u4f1a\u81ea\u52a8\u52a0\u8f7d\u9879\u76ee\u914d\u7f6e\u6587\u4ef6\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f7f\u7528m\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u521b\u5efaModel\u7c7b\u7684\u5b9e\u4f8b\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u5176\u4ed6\u975eModel\u7c7b\u7684\u5b9e\u4f8b\u3002\u901a\u8fc7\u4f7f\u7528m\u65b9\u6cd5\u5b9e\u4f8b\u5316\u7c7b\uff0c\u60a8\u53ef\u4ee5\u5bf9\u8be5\u7c7b\u6267\u884c\u76f8\u5e94\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u8c03\u7528Model\u7c7b\u6240\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a $user&nbsp;=&nbsp;M(&#8216;user&#8217;); $user-&amp;gt;where(&#8216;id=1&#8217;)-&amp;gt;find(); \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a$user\u7684Model\u7c7b\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528where()\u65b9\u6cd5\u548cfind()\u65b9\u6cd5\u67e5\u8be2\u4e86\u6570\u636e\u5e93\u8868\u4e2dID\u4e3a1\u7684\u8bb0\u5f55\uff0c\u7136\u540e\u5c06\u67e5\u8be2\u7ed3\u679c\u5b58\u50a8\u5728$user\u53d8\u91cf\u4e2d\u3002 \u9664\u4e86\u67e5\u8be2\uff0cm\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u7684\u589e\u52a0\u3001\u4fee\u6539\u548c\u5220\u9664\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u589e\u52a0\u6570\u636e\u7684\u793a\u4f8b\u4ee3\u7801\uff1a $user&nbsp;=&nbsp;M(&#8216;user&#8217;); $user-&amp;gt;username&nbsp;=&nbsp;&#8216;test&#8217;; $user-&amp;gt;password&nbsp;=&nbsp;md5(&#8216;123456&#8217;); $user-&amp;gt;add(); \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a$user\u7684Model\u7c7b\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528add()\u65b9\u6cd5\u5411\u6570\u636e\u5e93\u8868\u4e2d\u589e\u52a0\u4e86\u4e00\u6761\u8bb0\u5f55\uff0c\u5176\u4e2d\u5305\u62ecusername\u548cpassword\u4e24\u4e2a\u5b57\u6bb5\uff0c\u5bc6\u7801\u901a\u8fc7md5\u51fd\u6570\u52a0\u5bc6\u3002 \u9664\u4e86\u4ee5\u4e0a\u793a\u4f8b\u4e2d\u7684\u5e38\u7528\u64cd\u4f5c\uff0cModel\u7c7b\u8fd8\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u5982join()\u3001order()\u3001limit()\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u7075\u6d3b\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002 \u4ee5\u4e0a\u5c31\u662f\u4e2d\u7684m\u65b9\u6cd5\u600e\u4e48\u4f7f\u7528\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-22938","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22938","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=22938"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22938\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}