{"id":24777,"date":"2024-11-21T13:47:28","date_gmt":"2024-11-21T05:47:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24777\/"},"modified":"2024-11-21T13:47:28","modified_gmt":"2024-11-21T05:47:28","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8c%e5%8a%a8%e6%80%81%e5%88%87%e6%8d%a2%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24777\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169828295532993.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93<\/p>\n<p>\u5f15\u8a00:<br \/>Hyperf\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 PHP \u6846\u67b6\uff0c\u5b83\u5728 Laravel \u7ec4\u4ef6\u57fa\u7840\u4e0a\u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u6027\u80fd\u548c\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u3002\u5176\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7279\u6027\u5c31\u662f\u5b83\u5bf9\u6570\u636e\u5e93\u7684\u652f\u6301\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u5207\u6362\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7684\u9700\u6c42\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Hyerpf \u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<br \/>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u6309\u7167 Hyerpf \u6846\u67b6\u7684\u8981\u6c42\uff0c\u6b63\u786e\u5730\u5b89\u88c5\u548c\u914d\u7f6e\u4e86Hyperf\u6846\u67b6\u3002<\/p>\n<p>\u4e8c\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<br \/>\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u4f4d\u4e8econfig\/autoload\/databases.php\u3002\u8be5\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e86\u6570\u7ec4\u6765\u5b58\u50a8\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u4e3a\u6bcf\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e\u4e0d\u540c\u7684\u540d\u79f0\u548c\u53c2\u6570\uff0c\u4ee5\u5b9e\u73b0\u52a8\u6001\u5207\u6362\u3002<\/p>\n<p>\u793a\u4f8b\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre>return [\n    'default' =&amp;gt; [\n        'driver' =&amp;gt; HyperfDatabaseDriverPgsqlDriver::class,\n        'host' =&amp;gt; env('DB_HOST', '127.0.0.1'),\n        'database' =&amp;gt; env('DB_DATABASE', 'hyperf'),\n        'username' =&amp;gt; env('DB_USERNAME', 'root'),\n        'password' =&amp;gt; env('DB_PASSWORD', ''),\n        'port' =&amp;gt; env('DB_PORT', 5432),\n        ...\n    ],\n    'database2' =&amp;gt; [\n        'driver' =&amp;gt; HyperfDatabaseDriverPgsqlDriver::class,\n        'host' =&amp;gt; env('DB_HOST2', '127.0.0.1'),\n        'database' =&amp;gt; env('DB_DATABASE2', 'hyperf'),\n        'username' =&amp;gt; env('DB_USERNAME2', 'root'),\n        'password' =&amp;gt; env('DB_PASSWORD2', ''),\n        'port' =&amp;gt; env('DB_PORT2', 5432),\n        ...\n    ],\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86default\u548cdatabase2\u4e24\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5206\u522b\u5bf9\u5e94\u4e0d\u540c\u7684\u6570\u636e\u5e93\u3002\u5176\u4e2d\u7684env\u51fd\u6570\u7528\u4e8e\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u83b7\u53d6\u914d\u7f6e\u503c\uff0c\u4ee5\u4fbf\u652f\u6301\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u52a8\u6001\u5207\u6362\u3002<\/p>\n<p>\u4e09\u3001\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93<br \/>\u5728 Hyerpf \u6846\u67b6\u4e2d\uff0c\u6570\u636e\u5e93\u7684\u8fde\u63a5\u662f\u901a\u8fc7\u5bb9\u5668\u6765\u7ba1\u7406\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bb9\u5668\u7684make\u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u5b9e\u4f8b\uff0c\u5e76\u5728\u9700\u8981\u5207\u6362\u6570\u636e\u5e93\u65f6\u91cd\u65b0\u7ed1\u5b9a\u8fde\u63a5\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>use HyperfDbConnectionDb;\nuse HyperfUtilsApplicationContext;\n\nclass ExampleService\n{\n    public function query($database)\n    {\n        $container = ApplicationContext::getContainer();\n        $connection = $container-&amp;gt;make(Db::class)-&amp;gt;getConnection();\n        $databaseConfig = config('databases.' . $database);\n\n        $connection-&amp;gt;disconnect();\n        $connection-&amp;gt;getConfig()-&amp;gt;set($databaseConfig);\n        $connection-&amp;gt;connect();\n\n        return $connection-&amp;gt;select(\"SELECT * FROM example_table\");\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u7247\u6bb5\u662f\u4e00\u4e2a\u793a\u4f8b\u670d\u52a1\u7c7b\uff0c\u5728query\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4ece\u5bb9\u5668\u4e2d\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u5b9e\u4f8b\u3002\u7136\u540e\uff0c\u901a\u8fc7config\u51fd\u6570\u83b7\u53d6\u5230\u5bf9\u5e94\u6570\u636e\u5e93\u7684\u914d\u7f6e\u4fe1\u606f\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5148\u65ad\u5f00\u5f53\u524d\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u7136\u540e\u518d\u6839\u636e\u65b0\u7684\u914d\u7f6e\u4fe1\u606f\u91cd\u65b0\u8fde\u63a5\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u64cd\u4f5c\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93\u7684\u529f\u80fd\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u53ea\u9700\u8981\u4f20\u5165\u4e0d\u540c\u7684\u6570\u636e\u5e93\u914d\u7f6e\u540d\u79f0\u5373\u53ef\u3002<\/p>\n<p>\u7ed3\u8bba:<br \/>\u4f7f\u7528 Hyperf \u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ea\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u591a\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5e76\u5728\u9700\u8981\u52a8\u6001\u5207\u6362\u7684\u5730\u65b9\uff0c\u91cd\u65b0\u7ed1\u5b9a\u6570\u636e\u5e93\u8fde\u63a5\u5373\u53ef\u3002\u901a\u8fc7\u4ee5\u4e0a\u7b80\u5355\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728 Hyerpf \u6846\u67b6\u4e2d\u5b9e\u73b0\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93\u7684\u529f\u80fd\u3002\u8fd9\u4e3a\u6211\u4eec\u7684\u5f00\u53d1\u5de5\u4f5c\u5e26\u6765\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u548c\u4fbf\u5229\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\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>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93 \u5f15\u8a00:Hyperf\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 PHP \u6846\u67b6\uff0c\u5b83\u5728 Laravel \u7ec4\u4ef6\u57fa\u7840\u4e0a\u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u6027\u80fd\u548c\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u3002\u5176\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7279\u6027\u5c31\u662f\u5b83\u5bf9\u6570\u636e\u5e93\u7684\u652f\u6301\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u5207\u6362\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7684\u9700\u6c42\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Hyerpf \u6846\u67b6\u8fdb\u884c\u52a8\u6001\u5207\u6362\u6570\u636e\u5e93\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u51c6\u5907\u5de5\u4f5c\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u6309\u7167 Hyerpf \u6846\u67b6\u7684\u8981\u6c42\uff0c\u6b63\u786e\u5730\u5b89\u88c5\u548c\u914d\u7f6e\u4e86Hyperf\u6846\u67b6\u3002 \u4e8c\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u4f4d\u4e8econfig\/autoload\/databases.php\u3002\u8be5\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e86\u6570\u7ec4\u6765\u5b58\u50a8\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u4e3a\u6bcf\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e\u4e0d\u540c\u7684\u540d\u79f0\u548c\u53c2\u6570\uff0c\u4ee5\u5b9e\u73b0\u52a8\u6001\u5207\u6362\u3002 \u793a\u4f8b\u914d\u7f6e\u5982\u4e0b\uff1a return [ &#8216;default&#8217; =&amp;gt; [ &#8216;driver&#8217; =&amp;gt; HyperfDatabaseDriverPgsqlDriver::class, &#8216;host&#8217; =&amp;gt; env(&#8216;DB_HOST&#8217;, &#8216;127.0.0.1&#8217;), &#8216;database&#8217; =&amp;gt; env(&#8216;DB_DATABASE&#8217;, &#8216;hyperf&#8217;), &#8216;username&#8217; =&amp;gt; env(&#8216;DB_USERNAME&#8217;, &#8216;root&#8217;), &#8216;password&#8217; =&amp;gt; env(&#8216;DB_PASSWORD&#8217;, &#8221;), &#8216;port&#8217; =&amp;gt; env(&#8216;DB_PORT&#8217;, 5432), &#8230; ], &#8216;database2&#8217; =&amp;gt; [ &#8216;driver&#8217; =&amp;gt; HyperfDatabaseDriverPgsqlDriver::class, &#8216;host&#8217; =&amp;gt; env(&#8216;DB_HOST2&#8217;, &#8216;127.0.0.1&#8217;), &#8216;database&#8217; =&amp;gt; [&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-24777","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24777","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=24777"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24777\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}