{"id":23225,"date":"2024-11-21T16:38:46","date_gmt":"2024-11-21T08:38:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23225\/"},"modified":"2024-11-21T16:38:46","modified_gmt":"2024-11-21T08:38:46","slug":"thinkphp6%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8phinx%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%81%e7%a7%bb%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23225\/","title":{"rendered":"ThinkPHP6\u4e2d\u5982\u4f55\u4f7f\u7528Phinx\u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb\uff1f"},"content":{"rendered":"<p>\u968f\u7740web\u5e94\u7528\u7a0b\u5e8f\u7684\u8fc5\u731b\u53d1\u5c55\uff0c\u6570\u636e\u5e93\u4f5c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\uff0c\u4e5f\u88ab\u8d4b\u4e88\u4e86\u66f4\u9ad8\u7684\u91cd\u8981\u6027\u3002\u5bf9\u4e8eweb\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6570\u636e\u5e93\u6570\u636e\u7684\u5b89\u5168\u6027\u3001\u53ef\u9760\u6027\u548c\u7a33\u5b9a\u6027\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u6570\u636e\u8868\u7684\u8bbe\u8ba1\u548c\u7ba1\u7406\u4e5f\u662f\u5f00\u53d1\u5de5\u4f5c\u7684\u5173\u952e\u90e8\u5206\uff0c\u56e0\u6b64\u6570\u636e\u8fc1\u79fb\u6210\u4e3a\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u91cd\u8981\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<p>Phinx\u662f\u4e00\u4e2aPHP\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5bf9\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb\u548c\u7ba1\u7406\u3002\u800cThinkPHP6\u4f5c\u4e3a\u4e00\u4e2a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u4e5f\u63d0\u4f9b\u4e86Phinx\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7b80\u5316\u6211\u4eec\u7684\u5f00\u53d1\u5de5\u4f5c\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecdThinkPHP6\u6846\u67b6\u4e2d\u4f7f\u7528Phinx\u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb\u7684\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>\u5b89\u88c5Phinx<\/li>\n<\/ol>\n<p>\u5148\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u4f7f\u7528Composer\u5b89\u88c5Phinx\uff1a<\/p>\n<pre>composer require robmorgan\/phinx<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u914d\u7f6ePhinx<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u4e2d\uff0cPhinx\u7684\u914d\u7f6e\u5728config\/phinx.php\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u751f\u6210\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>vendor\/bin\/phinx init<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u5b8c\u6bd5\u540e\uff0cconfig\u76ee\u5f55\u4e0b\u4f1a\u751f\u6210phinx.php\u6587\u4ef6\u3002\u6211\u4eec\u9700\u8981\u5728\u5176\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u4fe1\u606f\u548c\u76ee\u5f55\u7ed3\u6784\u7b49\u4fe1\u606f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\uff1a<\/p>\n<pre>return [\n    'paths' =&amp;gt; [\n        'migrations' =&amp;gt; '%%PHINX_CONFIG_DIR%%\/db\/migrations',\n        'seeds' =&amp;gt; '%%PHINX_CONFIG_DIR%%\/db\/seeds'\n    ],\n    'environments' =&amp;gt; [\n        'default_database' =&amp;gt; 'development',\n        'development' =&amp;gt; [\n            'adapter' =&amp;gt; 'mysql',\n            'host' =&amp;gt; '127.0.0.1',\n            'name' =&amp;gt; 'thinkphp6',\n            'user' =&amp;gt; 'root',\n            'pass' =&amp;gt; '123456',\n            'charset' =&amp;gt; 'utf8',\n        ]\n    ]\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u521b\u5efa\u6570\u636e\u5e93\u8fc1\u79fb<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u4e2d\uff0cPhinx\u7684\u6570\u636e\u5e93\u8fc1\u79fb\u5b58\u50a8\u5728database\/migrations\u76ee\u5f55\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fc1\u79fb\uff1a<\/p>\n<pre>vendor\/bin\/phinx create MyNewMigration<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u8be5\u547d\u4ee4\u540e\u4f1a\u5728database\/migrations\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fc1\u79fb\u6587\u4ef6\u3002<\/p>\n<p>\u5728Phinx\u4e2d\uff0c\u8fc1\u79fb\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a\u8fc1\u79fb\u548c\u56de\u6eda\u3002\u8fc1\u79fb\u5305\u542b\u5bf9\u6570\u636e\u5e93\u7684\u66f4\u6539\uff0c\u56de\u6eda\u6267\u884c\u4e0e\u8fc1\u79fb\u76f8\u53cd\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2ausers\u8868\uff1a<\/p>\n<pre>use PhinxMigrationAbstractMigration;\n\nclass CreateUsersTable extends AbstractMigration\n{\n    \/**\n     * Change Method.\n     *\n     * More information on this method is available here:\n     * http:\/\/docs.phinx.org\/en\/latest\/migrations.html#the-change-method\n     *\/\n    public function change()\n    {\n        $table = $this-&amp;gt;table('users');\n        $table-&amp;gt;addColumn('username', 'string', ['limit' =&amp;gt; 50])\n              -&amp;gt;addColumn('email', 'string', ['limit' =&amp;gt; 100])\n              -&amp;gt;addColumn('password', 'string', ['limit' =&amp;gt; 255])\n              -&amp;gt;addColumn('created_at', 'datetime')\n              -&amp;gt;addColumn('updated_at', 'datetime')\n              -&amp;gt;create();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728change()\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528$table\u53d8\u91cf\u6784\u5efa\u8868\u7ed3\u6784\uff0c\u5e76\u4f7f\u7528addColumn()\u65b9\u6cd5\u4e3a\u4e0d\u540c\u7684\u5217\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u548c\u9650\u5236\u3002\u6700\u540e\u8c03\u7528create()\u65b9\u6cd5\uff0c\u5c06\u8868\u7ed3\u6784\u521b\u5efa\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<ol>\n<li>\u6267\u884c\u6570\u636e\u5e93\u8fc1\u79fb<\/li>\n<\/ol>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6267\u884c\u8fc1\u79fb\uff1a<\/p>\n<pre>vendor\/bin\/phinx migrate<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Phinx\u4f1a\u6309\u7167\u521b\u5efa\u8fc1\u79fb\u7684\u5148\u540e\u987a\u5e8f\u6267\u884c\u66f4\u6539\u3002\u5982\u679c\u6240\u6709\u8fc1\u79fb\u6267\u884c\u6210\u529f\uff0cPhinx\u5c06\u4f1a\u5728\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aphinxlog\u7684\u8868\u4ee5\u8bb0\u5f55\u8fc1\u79fb\u5386\u53f2\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u6267\u884c\u8fc1\u79fb\u5c06\u4f1a\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u66f4\u6539\uff0c\u8bf7\u786e\u4fdd\u5907\u4efd\u6570\u636e\uff0c\u4ee5\u9632\u6570\u636e\u4e22\u5931\u6216\u65e0\u6cd5\u6062\u590d\u3002<\/p>\n<ol>\n<li>\u56de\u6eda\u6570\u636e\u5e93\u8fc1\u79fb<\/li>\n<\/ol>\n<p>\u5982\u679c\u51fa\u73b0\u9519\u8bef\u6216\u9700\u8981\u56de\u9000\u66f4\u6539\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u56de\u9000\u4e0a\u4e00\u6b21\u8fc1\u79fb\uff1a<\/p>\n<pre>vendor\/bin\/phinx rollback<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Phinx\u4f1a\u56de\u6eda\u4e0a\u4e00\u6b21\u6210\u529f\u6267\u884c\u7684\u8fc1\u79fb\uff08\u5982\u679c\u5b58\u5728\uff09\u3002Phinx\u4f1a\u5728\u56de\u6eda\u8fc1\u79fb\u540e\u5c06\u4fe1\u606f\u5199\u5165\u5230phinxlog\u8868\u4e2d\u3002<\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528Phinx\u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb\u7684\u8fc7\u7a0b\u3002\u4f7f\u7528Phinx\u53ef\u4ee5\u51cf\u5c11\u624b\u5199SQL\u7684\u590d\u6742\u6027\u548c\u98ce\u9669\uff0c\u4f7f\u6570\u636e\u5e93\u8fc1\u79fb\u8fc7\u7a0b\u66f4\u52a0\u7b80\u4fbf\u548c\u53ef\u63a7\u3002<\/p>\n<p>\u5728\u8fc1\u79fb\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Phinx\u7684\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u6570\u636e\u586b\u5145\u3001\u6570\u636e\u6821\u9a8c\u548c\u652f\u6301\u591a\u6570\u636e\u5e93\u3002\u8fd9\u4e9b\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u6027\u80fd\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u5efa\u8bae\u5c06\u6570\u636e\u5e93\u8fc1\u79fb\u548c\u7ba1\u7406\u4f5c\u4e3a\u56e2\u961f\u5f00\u53d1\u7684\u4e00\u90e8\u5206\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7ed3\u6784\u7684\u4e00\u81f4\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6\u4e2d\u5982\u4f55\u4f7f\u7528Phinx\u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb\uff1f\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>\u968f\u7740web\u5e94\u7528\u7a0b\u5e8f\u7684\u8fc5\u731b\u53d1\u5c55\uff0c\u6570\u636e\u5e93\u4f5c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\uff0c\u4e5f\u88ab\u8d4b\u4e88\u4e86\u66f4\u9ad8\u7684\u91cd\u8981\u6027\u3002\u5bf9\u4e8eweb\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6570\u636e\u5e93\u6570\u636e\u7684\u5b89\u5168\u6027\u3001\u53ef\u9760\u6027\u548c\u7a33\u5b9a\u6027\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u6570\u636e\u8868\u7684\u8bbe\u8ba1\u548c\u7ba1\u7406\u4e5f\u662f\u5f00\u53d1\u5de5\u4f5c\u7684\u5173\u952e\u90e8\u5206\uff0c\u56e0\u6b64\u6570\u636e\u8fc1\u79fb\u6210\u4e3a\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u91cd\u8981\u65b9\u6cd5\u4e4b\u4e00\u3002 Phinx\u662f\u4e00\u4e2aPHP\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5bf9\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb\u548c\u7ba1\u7406\u3002\u800cThinkPHP6\u4f5c\u4e3a\u4e00\u4e2a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u4e5f\u63d0\u4f9b\u4e86Phinx\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7b80\u5316\u6211\u4eec\u7684\u5f00\u53d1\u5de5\u4f5c\u3002 \u672c\u6587\u5c06\u4ecb\u7ecdThinkPHP6\u6846\u67b6\u4e2d\u4f7f\u7528Phinx\u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb\u7684\u65b9\u6cd5\u3002 \u5b89\u88c5Phinx \u5148\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u4f7f\u7528Composer\u5b89\u88c5Phinx\uff1a composer require robmorgan\/phinx \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6ePhinx \u5728ThinkPHP6\u4e2d\uff0cPhinx\u7684\u914d\u7f6e\u5728config\/phinx.php\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u751f\u6210\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b vendor\/bin\/phinx init \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c\u5b8c\u6bd5\u540e\uff0cconfig\u76ee\u5f55\u4e0b\u4f1a\u751f\u6210phinx.php\u6587\u4ef6\u3002\u6211\u4eec\u9700\u8981\u5728\u5176\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u4fe1\u606f\u548c\u76ee\u5f55\u7ed3\u6784\u7b49\u4fe1\u606f\u3002 \u4f8b\u5982\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\uff1a return [ &#8216;paths&#8217; =&amp;gt; [ &#8216;migrations&#8217; =&amp;gt; &#8216;%%PHINX_CONFIG_DIR%%\/db\/migrations&#8217;, &#8216;seeds&#8217; =&amp;gt; &#8216;%%PHINX_CONFIG_DIR%%\/db\/seeds&#8217; ], &#8216;environments&#8217; =&amp;gt; [ &#8216;default_database&#8217; =&amp;gt; &#8216;development&#8217;, &#8216;development&#8217; =&amp;gt; [ &#8216;adapter&#8217; =&amp;gt; &#8216;mysql&#8217;, &#8216;host&#8217; =&amp;gt; &#8216;127.0.0.1&#8217;, &#8216;name&#8217; =&amp;gt; &#8216;thinkphp6&#8217;, &#8216;user&#8217; =&amp;gt; &#8216;root&#8217;, &#8216;pass&#8217; =&amp;gt; &#8216;123456&#8217;, &#8216;charset&#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-23225","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23225","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=23225"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23225\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}