{"id":22948,"date":"2024-11-21T13:27:31","date_gmt":"2024-11-21T05:27:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22948\/"},"modified":"2024-11-21T13:27:31","modified_gmt":"2024-11-21T05:27:31","slug":"thinkphp%e5%92%8cm%e6%96%b9%e6%b3%95%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22948\/","title":{"rendered":"thinkphp\u548cm\u65b9\u6cd5\u7684\u533a\u522b"},"content":{"rendered":"<p>thinkphp\u548cm\u65b9\u6cd5\u7684\u533a\u522b<\/p>\n<p>ThinkPHP\u662f\u4e00\u6b3e\u8f7b\u91cf\u7ea7\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u65b9\u4fbf\u5feb\u901f\u5730\u5f00\u53d1\u9ad8\u8d28\u91cf\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u5728ThinkPHP\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u8981\u7528\u5230\u6a21\u578b\u5c42\u7684M\u65b9\u6cd5\uff0c\u90a3\u4e48M\u65b9\u6cd5\u548cThinkPHP\u7684\u54ea\u4e9b\u529f\u80fd\u6709\u533a\u522b\u5462\uff1f\u4e0b\u9762\u5c31\u4e3a\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u3002<\/p>\n<p>\u4e00\u3001ThinkPHP<\/p>\n<p>ThinkPHP\u662f\u4e00\u6b3e\u4f18\u79c0\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u5176\u4e3b\u8981\u7279\u70b9\u662f\u7b80\u6d01\u3001\u9ad8\u6548\u3001\u5b89\u5168\u3001\u6613\u7528\u3001\u7075\u6d3b\u7b49\u3002\u8be5\u6846\u67b6\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684 MVC \u5f00\u53d1\u6a21\u5f0f\uff0c\u53ef\u5b9e\u73b0\u5feb\u901f\u7684\u5f00\u53d1\uff0c\u5177\u6709\u4fbf\u4e8e\u6269\u5c55\u548c\u914d\u7f6e\u7684\u7279\u6b8a\u8bbe\u8ba1\uff0c\u4f18\u79c0\u7684\u6587\u6863\u548c\u652f\u6301\u793e\u533a\uff0c\u4ee5\u53ca\u5f3a\u5927\u7684\u7f13\u5b58\u673a\u5236\u548c\u6570\u636e\u9a8c\u8bc1\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u4e8c\u3001M\u65b9\u6cd5<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>M\u65b9\u6cd5\u662fThinkPHP\u4e2d\u7684\u4e00\u79cd\u6a21\u578b\u5c42\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u64cd\u4f5c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>$model-&amp;gt;M($sql, $bind = [], $master = false, $class = false)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c$sql\u4e3a\u8981\u6267\u884c\u7684SQL\u8bed\u53e5\uff0c$bind\u4e3a\u7ed1\u5b9a\u7684\u53c2\u6570\uff0c\u5982\u679c\u6ca1\u6709\u5219\u4e3a[]\uff0c$master\u4e3a\u662f\u5426\u4e3a\u4e3b\u5e93\u64cd\u4f5c\uff0c$class\u4e3a\u4f7f\u7528\u7684\u5b9e\u4f8b\u5bf9\u8c61\u7c7b\u540d\u79f0\u3002<\/p>\n<p>\u4e09\u3001M\u65b9\u6cd5\u548cThinkPHP\u7684\u533a\u522b<\/p>\n<ol>\n<li>\u5f00\u53d1\u6846\u67b6\u548c\u6a21\u578b\u5c42\u64cd\u4f5c<\/li>\n<\/ol>\n<p>ThinkPHP\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u5305\u542b\u4e86MVC\u67b6\u6784\u4e2d\u7684\u6a21\u578b\u5c42\u3001\u89c6\u56fe\u5c42\u548c\u63a7\u5236\u5c42\u7b49\u3002\u800cM\u65b9\u6cd5\u53ea\u662f\u6a21\u578b\u5c42\u4e2d\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5b83\u662f\u5bf9\u6570\u636e\u5e93\u6267\u884cSQL\u8bed\u53e5\u7684\u4e00\u79cd\u5c01\u88c5\uff0c\u548c\u6846\u67b6\u7684\u529f\u80fd\u6709\u6240\u533a\u522b\u3002<\/p>\n<ol>\n<li>\u6570\u636e\u5e93\u8fde\u63a5\u65b9\u5f0f<\/li>\n<\/ol>\n<p>ThinkPHP\u91c7\u7528\u4e86\u6570\u636e\u5e93\u62bd\u8c61\u5c42\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u8fde\u63a5\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528PDO\u3001MySQLi\u548c\u539f\u751f\u7684MySQL\u7b49\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u8fdb\u884c\u914d\u7f6e\u548c\u8c03\u6574\u3002\u800cM\u65b9\u6cd5\u5219\u65e0\u6cd5\u9009\u62e9\u4f7f\u7528\u7684\u6570\u636e\u5e93\u8fde\u63a5\u65b9\u5f0f\uff0c\u4f7f\u7528\u9ed8\u8ba4\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u7684\u6570\u636e\u5e93\u53ef\u80fd\u65e0\u6cd5\u652f\u6301\u3002<\/p>\n<ol>\n<li>\u53d8\u91cf\u7ed1\u5b9a<\/li>\n<\/ol>\n<p>ThinkPHP\u4e2d\u7684SQL\u8bed\u53e5\u7ed1\u5b9a\u53d8\u91cf\u80fd\u591f\u6709\u6548\u5730\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002\u800cM\u65b9\u6cd5\u5219\u4e0d\u80fd\u4f7f\u7528\u53d8\u91cf\u7ed1\u5b9a\uff0c\u7ed1\u5b9a\u7684\u53c2\u6570\u5fc5\u987b\u76f4\u63a5\u62fc\u63a5\u5230SQL\u8bed\u53e5\u4e2d\uff0c\u5bb9\u6613\u9020\u6210\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<ol>\n<li>\u6267\u884cSQL\u8bed\u53e5\u7684\u65b9\u5f0f<\/li>\n<\/ol>\n<p>ThinkPHP\u91c7\u7528PDO\u6216mysqli\u9a71\u52a8\u6267\u884cSQL\u8bed\u53e5\uff0c\u5bf9\u4e8e\u76f8\u5173\u914d\u7f6e\u8fdb\u884c\u8bbe\u7f6e\u540e\u80fd\u591f\u8fbe\u5230\u4e00\u5b9a\u7684\u6027\u80fd\u4f18\u5316\u548c\u5b89\u5168\u6027\u4fdd\u969c\uff0c\u9002\u7528\u4e8e\u4e2d\u5927\u578b\u5e94\u7528\u3002\u800cM\u65b9\u6cd5\u4f7f\u7528\u539f\u751f\u7684MySQL\u6267\u884cSQL\u8bed\u53e5\uff0c\u5bf9\u4e8e\u5927\u91cf\u6570\u636e\u7684\u5904\u7406\u548c\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u5b58\u5728\u4e00\u5b9a\u7684\u74f6\u9888\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cM\u65b9\u6cd5\u662fThinkPHP\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u6cd5\uff0c\u7b80\u5355\u3001\u65b9\u4fbf\uff0c\u5bf9\u6570\u636e\u7684\u589e\u5220\u6539\u67e5\u7b49\u57fa\u7840\u64cd\u4f5c\u5f88\u6709\u5e2e\u52a9\u3002\u4f46\u662f\uff0c\u5728\u5f00\u53d1\u66f4\u52a0\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u8fd8\u9700\u8981\u7efc\u5408\u8003\u8651\u5230\u6846\u67b6\u7684\u5b8c\u6574\u529f\u80fd\u548c\u5b89\u5168\u6027\u7b49\u65b9\u9762\uff0c\u505a\u597d\u914d\u7f6e\u548c\u8c03\u6574\uff0c\u5b9e\u73b0\u9ad8\u6548\u4e14\u5b89\u5168\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u548cm\u65b9\u6cd5\u7684\u533a\u522b\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\u548cm\u65b9\u6cd5\u7684\u533a\u522b ThinkPHP\u662f\u4e00\u6b3e\u8f7b\u91cf\u7ea7\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u65b9\u4fbf\u5feb\u901f\u5730\u5f00\u53d1\u9ad8\u8d28\u91cf\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u5728ThinkPHP\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u8981\u7528\u5230\u6a21\u578b\u5c42\u7684M\u65b9\u6cd5\uff0c\u90a3\u4e48M\u65b9\u6cd5\u548cThinkPHP\u7684\u54ea\u4e9b\u529f\u80fd\u6709\u533a\u522b\u5462\uff1f\u4e0b\u9762\u5c31\u4e3a\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u3002 \u4e00\u3001ThinkPHP ThinkPHP\u662f\u4e00\u6b3e\u4f18\u79c0\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u5176\u4e3b\u8981\u7279\u70b9\u662f\u7b80\u6d01\u3001\u9ad8\u6548\u3001\u5b89\u5168\u3001\u6613\u7528\u3001\u7075\u6d3b\u7b49\u3002\u8be5\u6846\u67b6\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684 MVC \u5f00\u53d1\u6a21\u5f0f\uff0c\u53ef\u5b9e\u73b0\u5feb\u901f\u7684\u5f00\u53d1\uff0c\u5177\u6709\u4fbf\u4e8e\u6269\u5c55\u548c\u914d\u7f6e\u7684\u7279\u6b8a\u8bbe\u8ba1\uff0c\u4f18\u79c0\u7684\u6587\u6863\u548c\u652f\u6301\u793e\u533a\uff0c\u4ee5\u53ca\u5f3a\u5927\u7684\u7f13\u5b58\u673a\u5236\u548c\u6570\u636e\u9a8c\u8bc1\u7b49\u529f\u80fd\u3002 \u4e8c\u3001M\u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b M\u65b9\u6cd5\u662fThinkPHP\u4e2d\u7684\u4e00\u79cd\u6a21\u578b\u5c42\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u64cd\u4f5c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u5176\u8bed\u6cd5\u4e3a\uff1a $model-&amp;gt;M($sql, $bind = [], $master = false, $class = false) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c$sql\u4e3a\u8981\u6267\u884c\u7684SQL\u8bed\u53e5\uff0c$bind\u4e3a\u7ed1\u5b9a\u7684\u53c2\u6570\uff0c\u5982\u679c\u6ca1\u6709\u5219\u4e3a[]\uff0c$master\u4e3a\u662f\u5426\u4e3a\u4e3b\u5e93\u64cd\u4f5c\uff0c$class\u4e3a\u4f7f\u7528\u7684\u5b9e\u4f8b\u5bf9\u8c61\u7c7b\u540d\u79f0\u3002 \u4e09\u3001M\u65b9\u6cd5\u548cThinkPHP\u7684\u533a\u522b \u5f00\u53d1\u6846\u67b6\u548c\u6a21\u578b\u5c42\u64cd\u4f5c ThinkPHP\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u5305\u542b\u4e86MVC\u67b6\u6784\u4e2d\u7684\u6a21\u578b\u5c42\u3001\u89c6\u56fe\u5c42\u548c\u63a7\u5236\u5c42\u7b49\u3002\u800cM\u65b9\u6cd5\u53ea\u662f\u6a21\u578b\u5c42\u4e2d\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5b83\u662f\u5bf9\u6570\u636e\u5e93\u6267\u884cSQL\u8bed\u53e5\u7684\u4e00\u79cd\u5c01\u88c5\uff0c\u548c\u6846\u67b6\u7684\u529f\u80fd\u6709\u6240\u533a\u522b\u3002 \u6570\u636e\u5e93\u8fde\u63a5\u65b9\u5f0f ThinkPHP\u91c7\u7528\u4e86\u6570\u636e\u5e93\u62bd\u8c61\u5c42\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u8fde\u63a5\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528PDO\u3001MySQLi\u548c\u539f\u751f\u7684MySQL\u7b49\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u8fdb\u884c\u914d\u7f6e\u548c\u8c03\u6574\u3002\u800cM\u65b9\u6cd5\u5219\u65e0\u6cd5\u9009\u62e9\u4f7f\u7528\u7684\u6570\u636e\u5e93\u8fde\u63a5\u65b9\u5f0f\uff0c\u4f7f\u7528\u9ed8\u8ba4\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u7684\u6570\u636e\u5e93\u53ef\u80fd\u65e0\u6cd5\u652f\u6301\u3002 \u53d8\u91cf\u7ed1\u5b9a ThinkPHP\u4e2d\u7684SQL\u8bed\u53e5\u7ed1\u5b9a\u53d8\u91cf\u80fd\u591f\u6709\u6548\u5730\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002\u800cM\u65b9\u6cd5\u5219\u4e0d\u80fd\u4f7f\u7528\u53d8\u91cf\u7ed1\u5b9a\uff0c\u7ed1\u5b9a\u7684\u53c2\u6570\u5fc5\u987b\u76f4\u63a5\u62fc\u63a5\u5230SQL\u8bed\u53e5\u4e2d\uff0c\u5bb9\u6613\u9020\u6210\u5b89\u5168\u95ee\u9898\u3002 \u6267\u884cSQL\u8bed\u53e5\u7684\u65b9\u5f0f ThinkPHP\u91c7\u7528PDO\u6216mysqli\u9a71\u52a8\u6267\u884cSQL\u8bed\u53e5\uff0c\u5bf9\u4e8e\u76f8\u5173\u914d\u7f6e\u8fdb\u884c\u8bbe\u7f6e\u540e\u80fd\u591f\u8fbe\u5230\u4e00\u5b9a\u7684\u6027\u80fd\u4f18\u5316\u548c\u5b89\u5168\u6027\u4fdd\u969c\uff0c\u9002\u7528\u4e8e\u4e2d\u5927\u578b\u5e94\u7528\u3002\u800cM\u65b9\u6cd5\u4f7f\u7528\u539f\u751f\u7684MySQL\u6267\u884cSQL\u8bed\u53e5\uff0c\u5bf9\u4e8e\u5927\u91cf\u6570\u636e\u7684\u5904\u7406\u548c\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u5b58\u5728\u4e00\u5b9a\u7684\u74f6\u9888\u3002 \u7efc\u4e0a\u6240\u8ff0\uff0cM\u65b9\u6cd5\u662fThinkPHP\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u6cd5\uff0c\u7b80\u5355\u3001\u65b9\u4fbf\uff0c\u5bf9\u6570\u636e\u7684\u589e\u5220\u6539\u67e5\u7b49\u57fa\u7840\u64cd\u4f5c\u5f88\u6709\u5e2e\u52a9\u3002\u4f46\u662f\uff0c\u5728\u5f00\u53d1\u66f4\u52a0\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u8fd8\u9700\u8981\u7efc\u5408\u8003\u8651\u5230\u6846\u67b6\u7684\u5b8c\u6574\u529f\u80fd\u548c\u5b89\u5168\u6027\u7b49\u65b9\u9762\uff0c\u505a\u597d\u914d\u7f6e\u548c\u8c03\u6574\uff0c\u5b9e\u73b0\u9ad8\u6548\u4e14\u5b89\u5168\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u3002 \u4ee5\u4e0a\u5c31\u662fthinkphp\u548cm\u65b9\u6cd5\u7684\u533a\u522b\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-22948","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22948","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=22948"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22948\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}