{"id":23156,"date":"2024-11-21T09:24:13","date_gmt":"2024-11-21T01:24:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23156\/"},"modified":"2024-11-21T09:24:13","modified_gmt":"2024-11-21T01:24:13","slug":"%e6%80%8e%e4%b9%88%e7%94%a8thinkphp%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e8%b4%ad%e7%89%a9%e8%bd%a6%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23156\/","title":{"rendered":"\u600e\u4e48\u7528ThinkPHP\u5b9e\u73b0\u4e00\u4e2a\u8d2d\u7269\u8f66\u529f\u80fd"},"content":{"rendered":"<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u6765\u5b58\u50a8\u6211\u4eec\u7684\u5546\u54c1\u548c\u8ba2\u5355\u4fe1\u606f\u3002\u590d\u5236\u5e76\u7c98\u8d34\u4ee5\u4e0bSQL\u4ee3\u7801\u5230phpMyAdmin\u6216\u5176\u4ed6MySQL\u5ba2\u6237\u7aef\u4e2d\uff0c\u5373\u53ef\u521b\u5efa\u6570\u636e\u5e93\uff1a<\/p>\n<p>CREATE DATABASE cart DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;<\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e24\u4e2a\u8868\u6765\u5b58\u50a8\u5546\u54c1\u548c\u8ba2\u5355\u4fe1\u606f\u3002\u4e0b\u8ff0SQL\u8bed\u53e5\u662f\u521b\u5efa\u201cproducts\u201d\u548c\u201corders\u201d\u4e24\u4e2a\u8868\u7684\uff1a CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(50), price DECIMAL(10,2) ); CREATE TABLE orders ( order_id INT PRIMARY KEY, product_id INT, order_date DATE, amount INT, FOREIGN KEY (product_id) REFERENCES products(product_id) );<\/p>\n<pre>CREATE&nbsp;TABLE&nbsp;products&nbsp;(\n&nbsp;id&nbsp;int(11)&nbsp;NOT&nbsp;NULL&nbsp;AUTO_INCREMENT,\n&nbsp;name&nbsp;varchar(255)&nbsp;NOT&nbsp;NULL,\n&nbsp;description&nbsp;text&nbsp;NOT&nbsp;NULL,\n&nbsp;price&nbsp;float&nbsp;NOT&nbsp;NULL,\n&nbsp;PRIMARY&nbsp;KEY&nbsp;(id)\n)&nbsp;ENGINE=InnoDB&nbsp;DEFAULT&nbsp;CHARSET=utf8;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>CREATE&nbsp;TABLE&nbsp;orders&nbsp;(\n&nbsp;id&nbsp;int(11)&nbsp;NOT&nbsp;NULL&nbsp;AUTO_INCREMENT,\n&nbsp;user_id&nbsp;int(11)&nbsp;NOT&nbsp;NULL,\n&nbsp;product_id&nbsp;int(11)&nbsp;NOT&nbsp;NULL,\n&nbsp;quantity&nbsp;int(11)&nbsp;NOT&nbsp;NULL,\n&nbsp;created_at&nbsp;timestamp&nbsp;NOT&nbsp;NULL&nbsp;DEFAULT&nbsp;CURRENT_TIMESTAMP,\n&nbsp;PRIMARY&nbsp;KEY&nbsp;(id)\n)&nbsp;ENGINE=InnoDB&nbsp;DEFAULT&nbsp;CHARSET=utf8;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528Composer\u5b89\u88c5ThinkPHP\u6846\u67b6\uff1a<\/p>\n<p>composer create-project topthink\/think tp5 &nbsp;&#8211;prefer-dist<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u63a5\u7740\uff0c\u628a\u4e0b\u9762\u7684\u4ee3\u7801\u590d\u5236\u5e76\u7c98\u8d34\u5230tp5\/application\/common.php\u6587\u4ef6\u91cc\u3002\u5168\u5c40\u5e2e\u52a9\u51fd\u6570\u201cgetCart\u201d\u5c06\u88ab\u521b\u5efa\uff0c\u4ee5\u83b7\u53d6\u7528\u6237\u8d2d\u7269\u8f66\u4fe1\u606f<\/p>\n<pre>&lt;?php use appindexmodelCart;\nfunction getCart()\n{\n$user_id = 1; \/\/ \u6b64\u5904\u9ed8\u8ba4\u7528\u6237ID\u4e3a1\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u5e94\u8be5\u4ece\u4f1a\u8bdd\u4e2d\u83b7\u53d6\u7528\u6237ID\n$cart = Cart::where(&#039;user_id&#039;, $user_id)-&gt;select();\nreturn&nbsp;$cart;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cCart\u201d\u7684\u6a21\u578b\u6765\u7ba1\u7406\u7528\u6237\u8d2d\u7269\u8f66\u4e2d\u7684\u9879\u76ee\u3002<\/p>\n<pre>&lt;?php namespace appindexmodel;\nuse thinkModel;\nclass Cart extends Model\n{\nprotected $table = &#039;orders&#039;;\n\nstatic function add($product_id, $quantity)\n{\n    $user_id = 1; \/\/ \u6b64\u5904\u9ed8\u8ba4\u7528\u6237ID\u4e3a1\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u5e94\u8be5\u4ece\u4f1a\u8bdd\u4e2d\u83b7\u53d6\u7528\u6237ID\n    $order = new Cart();\n    $order-&gt;user_id&nbsp;=&nbsp;$user_id;\n&nbsp;&nbsp;&nbsp;&nbsp;$order-&amp;gt;product_id&nbsp;=&nbsp;$product_id;\n&nbsp;&nbsp;&nbsp;&nbsp;$order-&amp;gt;quantity&nbsp;=&nbsp;$quantity;\n&nbsp;&nbsp;&nbsp;&nbsp;$order-&amp;gt;save();\n}\n\nstatic&nbsp;function&nbsp;remove($id)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;Cart::destroy($id);\n}\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u73b0\u5728\u80fd\u591f\u901a\u8fc7\u4f7f\u7528\u201cCart\u201d\u6a21\u578b\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u6216\u5220\u9664\u8d2d\u7269\u8f66\u4e2d\u7684\u5546\u54c1\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5546\u54c1\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\uff1a<\/p>\n<pre>Cart::add($product_id,&nbsp;$quantity);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u800c\u5c06\u5546\u54c1\u4ece\u8d2d\u7269\u8f66\u4e2d\u5220\u9664\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>Cart::remove($id);<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cCart\u201d\u7684\u63a7\u5236\u5668\uff0c\u5e76\u6dfb\u52a0\u4e24\u4e2a\u65b9\u6cd5\uff1a\u4e00\u4e2a\u7528\u4e8e\u663e\u793a\u8d2d\u7269\u8f66\u5185\u5bb9\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u5c06\u5546\u54c1\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u3002<\/p>\n<pre>&lt;?php namespace appindexcontroller;\nuse appindexmodelCart;\nclass CartController extends BaseController\n{\npublic function index()\n{\n    $cart = getCart();\n    $this-&gt;assign('cart',&nbsp;$cart);\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;fetch();\n}\n\npublic&nbsp;function&nbsp;add()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;$product_id&nbsp;=&nbsp;input('post.product_id');\n&nbsp;&nbsp;&nbsp;&nbsp;$quantity&nbsp;=&nbsp;input('post.quantity');\n\n&nbsp;&nbsp;&nbsp;&nbsp;Cart::add($product_id,&nbsp;$quantity);\n\n&nbsp;&nbsp;&nbsp;&nbsp;$this-&amp;gt;success('\u6dfb\u52a0\u6210\u529f',&nbsp;url('index'));\n}\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b8c\u6210\u4e0a\u8ff0\u6b65\u9aa4\u540e\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u8d2d\u7269\u8f66\u5e94\u7528\u7a0b\u5e8f\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eeCartController\u7684index\u65b9\u6cd5\u6765\u663e\u793a\u8d2d\u7269\u8f66\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7\u8bbf\u95eeCartController\u7684add\u65b9\u6cd5\u6765\u5c06\u5546\u54c1\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7528ThinkPHP\u5b9e\u73b0\u4e00\u4e2a\u8d2d\u7269\u8f66\u529f\u80fd\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>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u6765\u5b58\u50a8\u6211\u4eec\u7684\u5546\u54c1\u548c\u8ba2\u5355\u4fe1\u606f\u3002\u590d\u5236\u5e76\u7c98\u8d34\u4ee5\u4e0bSQL\u4ee3\u7801\u5230phpMyAdmin\u6216\u5176\u4ed6MySQL\u5ba2\u6237\u7aef\u4e2d\uff0c\u5373\u53ef\u521b\u5efa\u6570\u636e\u5e93\uff1a CREATE DATABASE cart DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; \u7136\u540e\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e24\u4e2a\u8868\u6765\u5b58\u50a8\u5546\u54c1\u548c\u8ba2\u5355\u4fe1\u606f\u3002\u4e0b\u8ff0SQL\u8bed\u53e5\u662f\u521b\u5efa\u201cproducts\u201d\u548c\u201corders\u201d\u4e24\u4e2a\u8868\u7684\uff1a CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(50), price DECIMAL(10,2) ); CREATE TABLE orders ( order_id INT PRIMARY KEY, product_id INT, order_date DATE, amount INT, FOREIGN KEY (product_id) REFERENCES products(product_id) ); CREATE&nbsp;TABLE&nbsp;products&nbsp;( &nbsp;id&nbsp;int(11)&nbsp;NOT&nbsp;NULL&nbsp;AUTO_INCREMENT, &nbsp;name&nbsp;varchar(255)&nbsp;NOT&nbsp;NULL, &nbsp;description&nbsp;text&nbsp;NOT&nbsp;NULL, &nbsp;price&nbsp;float&nbsp;NOT&nbsp;NULL, &nbsp;PRIMARY&nbsp;KEY&nbsp;(id) )&nbsp;ENGINE=InnoDB&nbsp;DEFAULT&nbsp;CHARSET=utf8; \u767b\u5f55\u540e\u590d\u5236 CREATE&nbsp;TABLE&nbsp;orders&nbsp;( &nbsp;id&nbsp;int(11)&nbsp;NOT&nbsp;NULL&nbsp;AUTO_INCREMENT, &nbsp;user_id&nbsp;int(11)&nbsp;NOT&nbsp;NULL, [&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-23156","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23156","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=23156"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23156\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}