{"id":22910,"date":"2024-11-21T11:48:33","date_gmt":"2024-11-21T03:48:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22910\/"},"modified":"2024-11-21T11:48:33","modified_gmt":"2024-11-21T03:48:33","slug":"thinkphp%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22910\/","title":{"rendered":"thinkphp\u5982\u4f55\u4fee\u6539\u6570\u636e\u5e93"},"content":{"rendered":"<\/p>\n<p><strong>\u4e00\u3001ThinkPHP\u6570\u636e\u5e93\u64cd\u4f5c<\/strong><\/p>\n<p>\u5728ThinkPHP\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b83\u63d0\u4f9b\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\u6765\u8fdb\u884c\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002\u5e38\u7528\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\u6709\uff1a<\/p>\n<ol>\n<li>\n<p>Db\u7c7b<\/p>\n<\/li>\n<\/ol>\n<p>\u5728ThinkPHP\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Db\u7c7b\u6765\u8fdb\u884c\u5bf9\u6570\u636e\u5e93\u7684\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\u7b49\u64cd\u4f5c\u3002\u5176\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>&lt;?php use thinkDb;\n\n\/\/ \u67e5\u8be2\u6570\u636e\n$list = Db::table(&#039;user&#039;)-&gt;where('id',&nbsp;1)-&amp;gt;find();\n\n\/\/&nbsp;\u65b0\u589e\u6570\u636e\n$data['name']&nbsp;=&nbsp;'test';\n$data['age']&nbsp;=&nbsp;20;\nDb::table('user')-&amp;gt;insert($data);\n\n\/\/&nbsp;\u66f4\u65b0\u6570\u636e\n$where['id']&nbsp;=&nbsp;1;\n$data['name']&nbsp;=&nbsp;'test';\n$data['age']&nbsp;=&nbsp;30;\nDb::table('user')-&amp;gt;where($where)-&amp;gt;update($data);\n\n\/\/&nbsp;\u5220\u9664\u6570\u636e\n$where['id']&nbsp;=&nbsp;1;\nDb::table('user')-&amp;gt;where($where)-&amp;gt;delete();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\n<p>Model\u7c7b<\/p>\n<\/li>\n<\/ol>\n<p>\u5728ThinkPHP\u4e2d\uff0cModel\u7c7b\u7ee7\u627f\u81eaDb\u7c7b\uff0c\u56e0\u6b64Model\u7c7b\u53ef\u4ee5\u4f7f\u7528Db\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u66f4\u52a0\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u5176\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace appindexmodel;\n\nuse thinkModel;\n\nclass User extends Model\n{\n    \/\/ \u67e5\u8be2\u6570\u636e\n    public function getUserById($id)\n    {\n        return $this-&gt;where('id',&nbsp;$id)-&amp;gt;find();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u66f4\u65b0\u6570\u636e\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;updateUser($id,&nbsp;$name)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;save(['name'&nbsp;=&amp;gt;&nbsp;$name],&nbsp;['id'&nbsp;=&amp;gt;&nbsp;$id]);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u3001ThinkPHP\u5982\u4f55\u4fee\u6539\u6570\u636e\u5e93<\/strong><\/p>\n<p>\u5728\u4f7f\u7528ThinkPHP\u4fee\u6539\u6570\u636e\u5e93\u65f6\uff0c\u901a\u5e38\u4f1a\u7ecf\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u6a21\u578b<\/p>\n<\/li>\n<\/ol>\n<p>\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6a21\u578b\uff0c\u8be5\u6a21\u578b\u5bf9\u5e94\u7740\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u7684\u6570\u636e\u5e93\u8868\u3002\u7531\u4e8eThinkPHP\u91c7\u7528\u4e86MVC\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u56e0\u6b64\u6211\u4eec\u5728\u521b\u5efa\u6a21\u578b\u65f6\u9700\u8981\u7ee7\u627f\u81eaThinkPHP\u7684Model\u7c7b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efaUser\u6a21\u578b\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php namespace appindexmodel;\n\nuse thinkModel;\n\nclass User extends Model\n{\n    protected $table = &#039;user&#039;;\n    protected $pk = &#039;id&#039;;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u521b\u5efaUser\u6a21\u578b\u65f6\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u8be5\u6a21\u578b\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u8868\u662fuser\u8868\uff0c\u5e76\u4e14\u8be5\u8868\u7684\u4e3b\u952e\u662fid\u3002<\/p>\n<ol>\n<li>\n<p>\u5b9e\u4f8b\u5316\u6a21\u578b<\/p>\n<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5148\u524d\u521b\u5efa\u7684User\u6a21\u578b\u8fdb\u884c\u5b9e\u4f8b\u5316\uff0c\u5e76\u5229\u7528\u8be5\u6a21\u578b\u64cd\u4f5c\u6570\u636e\u5e93\u3002\u4e0b\u9762\u662f\u5b9e\u4f8b\u5316User\u6a21\u578b\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php $userModel = new ppindexmodelUser();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\n<p>\u4fee\u6539\u6570\u636e<\/p>\n<\/li>\n<\/ol>\n<p>\u4e00\u65e6\u521b\u5efa\u4e86User\u6a21\u578b\u7684\u5b9e\u4f8b\uff0c\u5c31\u53ef\u4ee5\u5229\u7528\u8be5\u6a21\u578b\u6240\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u4fee\u6539\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528User\u6a21\u578b\u4fee\u6539\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php $userModel = new ppindexmodelUser();\n\n\/\/ \u66f4\u65b0\u6570\u636e\n$where['id'] = 1;\n$data['name'] = 'test';\n$data['age'] = 30;\n$userModel-&gt;where($where)-&gt;update($data);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528$UserModel\u7684update()\u65b9\u6cd5\u5bf9User\u8868\u4e2did\u4e3a1\u7684\u6570\u636e\u8fdb\u884c\u4e86\u4fee\u6539\uff0c\u5c06\u8be5\u884c\u6570\u636e\u7684name\u5b57\u6bb5\u4fee\u6539\u4e3atest\uff0cage\u5b57\u6bb5\u4fee\u6539\u4e3a30\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4fee\u6539\u6570\u636e\u5e93\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>\u4e00\u3001ThinkPHP\u6570\u636e\u5e93\u64cd\u4f5c \u5728ThinkPHP\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b83\u63d0\u4f9b\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\u6765\u8fdb\u884c\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002\u5e38\u7528\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\u6709\uff1a Db\u7c7b \u5728ThinkPHP\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Db\u7c7b\u6765\u8fdb\u884c\u5bf9\u6570\u636e\u5e93\u7684\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\u7b49\u64cd\u4f5c\u3002\u5176\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b &lt;?php use thinkDb; \/\/ \u67e5\u8be2\u6570\u636e $list = Db::table(&#039;user&#039;)-&gt;where(&#8216;id&#8217;,&nbsp;1)-&amp;gt;find(); \/\/&nbsp;\u65b0\u589e\u6570\u636e $data[&#8216;name&#8217;]&nbsp;=&nbsp;&#8216;test&#8217;; $data[&#8216;age&#8217;]&nbsp;=&nbsp;20; Db::table(&#8216;user&#8217;)-&amp;gt;insert($data); \/\/&nbsp;\u66f4\u65b0\u6570\u636e $where[&#8216;id&#8217;]&nbsp;=&nbsp;1; $data[&#8216;name&#8217;]&nbsp;=&nbsp;&#8216;test&#8217;; $data[&#8216;age&#8217;]&nbsp;=&nbsp;30; Db::table(&#8216;user&#8217;)-&amp;gt;where($where)-&amp;gt;update($data); \/\/&nbsp;\u5220\u9664\u6570\u636e $where[&#8216;id&#8217;]&nbsp;=&nbsp;1; Db::table(&#8216;user&#8217;)-&amp;gt;where($where)-&amp;gt;delete(); \u767b\u5f55\u540e\u590d\u5236 Model\u7c7b \u5728ThinkPHP\u4e2d\uff0cModel\u7c7b\u7ee7\u627f\u81eaDb\u7c7b\uff0c\u56e0\u6b64Model\u7c7b\u53ef\u4ee5\u4f7f\u7528Db\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u66f4\u52a0\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u5176\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a &lt;?php namespace appindexmodel; use thinkModel; class User extends Model { \/\/ \u67e5\u8be2\u6570\u636e public function getUserById($id) { return $this-&gt;where(&#8216;id&#8217;,&nbsp;$id)-&amp;gt;find(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u66f4\u65b0\u6570\u636e &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;updateUser($id,&nbsp;$name) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;save([&#8216;name&#8217;&nbsp;=&amp;gt;&nbsp;$name],&nbsp;[&#8216;id&#8217;&nbsp;=&amp;gt;&nbsp;$id]); &nbsp;&nbsp;&nbsp;&nbsp;} } \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001ThinkPHP\u5982\u4f55\u4fee\u6539\u6570\u636e\u5e93 [&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-22910","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22910","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=22910"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22910\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}