{"id":168761,"date":"2024-11-06T15:56:21","date_gmt":"2024-11-06T07:56:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168761\/"},"modified":"2024-11-06T15:56:22","modified_gmt":"2024-11-06T07:56:22","slug":"tp3-%e5%af%a6%e7%8f%be%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e6%96%b9%e6%b3%95-tp3-%e5%a6%82%e4%bd%95%e9%80%a3%e6%8e%a5%e5%a4%9a%e5%80%8b%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168761\/","title":{"rendered":"TP3 \u5be6\u73fe\u591a\u6578\u64da\u5eab\u9023\u63a5\u7684\u65b9\u6cd5 (tp3 \u5982\u4f55\u9023\u63a5\u591a\u500b\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"tp3-%e5%af%a6%e7%8f%be%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e6%96%b9%e6%b3%95-tp3-%e5%a6%82%e4%bd%95%e9%80%a3%e6%8e%a5%e5%a4%9a%e5%80%8b%e6%95%b8%e6%93%9a%e5%ba%ab-KddnvMEzSO\">TP3 \u5be6\u73fe\u591a\u6578\u64da\u5eab\u9023\u63a5\u7684\u65b9\u6cd5 (tp3 \u5982\u4f55\u9023\u63a5\u591a\u500b\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u73fe\u4ee3\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u4f7f\u7528\u591a\u500b\u6578\u64da\u5eab\u4f86\u7ba1\u7406\u4e0d\u540c\u7684\u6578\u64da\u662f\u76f8\u7576\u666e\u904d\u7684\u9700\u6c42\u3002\u7279\u5225\u662f\u5728\u4f7f\u7528 ThinkPHP 3\uff08TP3\uff09\u6846\u67b6\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9700\u8981\u9023\u63a5\u591a\u500b\u6578\u64da\u5eab\u4ee5\u652f\u6301\u4e0d\u540c\u7684\u696d\u52d9\u908f\u8f2f\u3002\u672c\u6587\u5c07\u63a2\u8a0e TP3 \u5982\u4f55\u5be6\u73fe\u591a\u6578\u64da\u5eab\u9023\u63a5\u7684\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u5177\u9ad4\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"tp3-%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae-KddnvMEzSO\">TP3 \u7684\u6578\u64da\u5eab\u914d\u7f6e<\/h2>\n<p>\u5728 TP3 \u4e2d\uff0c\u6578\u64da\u5eab\u7684\u914d\u7f6e\u901a\u5e38\u662f\u5728\u61c9\u7528\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u9032\u884c\u7684\u3002\u9019\u4e9b\u914d\u7f6e\u6587\u4ef6\u4f4d\u65bc <code>application\/config\/database.php<\/code>\u3002\u5728\u9019\u500b\u6587\u4ef6\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5b9a\u7fa9\u591a\u500b\u6578\u64da\u5eab\u9023\u63a5\u7684\u53c3\u6578\u3002<\/p>\n<h3 id=\"%e9%85%8d%e7%bd%ae%e5%a4%9a%e5%80%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-KddnvMEzSO\">\u914d\u7f6e\u591a\u500b\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728 <code>database.php<\/code> \u4e2d\u914d\u7f6e\u591a\u500b\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/p>\n<pre><code>\nreturn array(\n    'default' =&gt; array(\n        'type'     =&gt; 'mysql',\n        'hostname' =&gt; 'localhost',\n        'database' =&gt; 'db1',\n        'username' =&gt; 'user1',\n        'password' =&gt; 'password1',\n        'hostport' =&gt; '',\n        'charset'  =&gt; 'utf8',\n        'prefix'   =&gt; '',\n    ),\n    'secondary' =&gt; array(\n        'type'     =&gt; 'mysql',\n        'hostname' =&gt; 'localhost',\n        'database' =&gt; 'db2',\n        'username' =&gt; 'user2',\n        'password' =&gt; 'password2',\n        'hostport' =&gt; '',\n        'charset'  =&gt; 'utf8',\n        'prefix'   =&gt; '',\n    ),\n);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u914d\u7f6e\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u5169\u500b\u6578\u64da\u5eab\u9023\u63a5\uff1a\u4e00\u500b\u662f\u9ed8\u8a8d\u7684 <code>default<\/code> \u9023\u63a5\uff0c\u53e6\u4e00\u500b\u662f <code>secondary<\/code> \u9023\u63a5\u3002\u6bcf\u500b\u9023\u63a5\u90fd\u5305\u542b\u4e86\u5fc5\u8981\u7684\u53c3\u6578\uff0c\u5982\u4e3b\u6a5f\u540d\u3001\u6578\u64da\u5eab\u540d\u3001\u7528\u6236\u540d\u548c\u5bc6\u78bc\u7b49\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e5%a4%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-KddnvMEzSO\">\u4f7f\u7528\u591a\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728\u914d\u7f6e\u597d\u591a\u500b\u6578\u64da\u5eab\u9023\u63a5\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5728 TP3 \u7684\u6a21\u578b\u4e2d\u4f7f\u7528\u9019\u4e9b\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u6a21\u578b\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u6578\u64da\u5eab\u9023\u63a5\u7684\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"%e5%9c%a8%e6%a8%a1%e5%9e%8b%e4%b8%ad%e4%bd%bf%e7%94%a8%e4%b8%8d%e5%90%8c%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab-KddnvMEzSO\">\u5728\u6a21\u578b\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u6578\u64da\u5eab<\/h3>\n<pre><code>\nclass UserModel extends Model {\n    protected $connection = 'default'; \/\/ \u4f7f\u7528\u9ed8\u8a8d\u6578\u64da\u5eab\n\n    public function getUser($id) {\n        return $this-&gt;where('id', $id)-&gt;find();\n    }\n}\n\nclass OrderModel extends Model {\n    protected $connection = 'secondary'; \/\/ \u4f7f\u7528\u7b2c\u4e8c\u500b\u6578\u64da\u5eab\n\n    public function getOrder($id) {\n        return $this-&gt;where('id', $id)-&gt;find();\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u5169\u500b\u6a21\u578b\uff1a<code>UserModel<\/code> \u548c <code>OrderModel<\/code>\u3002<code>UserModel<\/code> \u4f7f\u7528\u9ed8\u8a8d\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u800c <code>OrderModel<\/code> \u5247\u4f7f\u7528\u7b2c\u4e8c\u500b\u6578\u64da\u5eab\u9023\u63a5\u3002\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5728\u540c\u4e00\u500b\u61c9\u7528\u4e2d\u9748\u6d3b\u5730\u64cd\u4f5c\u4e0d\u540c\u7684\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-KddnvMEzSO\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u78ba\u4fdd\u6bcf\u500b\u6578\u64da\u5eab\u7684\u914d\u7f6e\u6b63\u78ba\u7121\u8aa4\uff0c\u7279\u5225\u662f\u7528\u6236\u540d\u548c\u5bc6\u78bc\u3002<\/li>\n<li>\u5728\u4f7f\u7528\u591a\u6578\u64da\u5eab\u6642\uff0c\u6ce8\u610f\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002<\/li>\n<li>\u6839\u64da\u696d\u52d9\u9700\u6c42\u5408\u7406\u5283\u5206\u6578\u64da\u5eab\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8907\u96dc\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-KddnvMEzSO\">\u7e3d\u7d50<\/h2>\n<p>\u5728 TP3 \u4e2d\u5be6\u73fe\u591a\u6578\u64da\u5eab\u9023\u63a5\u662f\u4e00\u500b\u76f8\u5c0d\u7c21\u55ae\u7684\u904e\u7a0b\uff0c\u53ea\u9700\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u7fa9\u591a\u500b\u6578\u64da\u5eab\u9023\u63a5\uff0c\u7136\u5f8c\u5728\u6a21\u578b\u4e2d\u6307\u5b9a\u4f7f\u7528\u7684\u9023\u63a5\u5373\u53ef\u3002\u9019\u7a2e\u9748\u6d3b\u6027\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u6839\u64da\u4e0d\u540c\u7684\u696d\u52d9\u9700\u6c42\u4f86\u7ba1\u7406\u6578\u64da\u3002\u82e5\u60a8\u9700\u8981\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\uff0c\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 TP3 \u5be6\u73fe\u591a\u6578\u64da\u5eab\u9023\u63a5\u7684\u65b9\u6cd5\uff0c\u5b78\u7fd2\u5982\u4f55\u5728 TP3 \u4e2d\u8f15\u9b06\u914d\u7f6e\u548c\u7ba1\u7406\u591a\u500b\u6578\u64da\u5eab\u9023\u63a5\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-168761","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=168761"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168761\/revisions"}],"predecessor-version":[{"id":168762,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168761\/revisions\/168762"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}