ThinkPHP6ÖÐÔõÑùʹÓÃPhinx¾ÙÐÐÊý¾Ý¿âǨá㣿
Ëæ×ÅwebÓ¦ÓóÌÐòµÄѸÃÍÉú³¤£¬Êý¾Ý¿â×÷ΪӦÓóÌÐòµÄÒ»²¿·Ö£¬Ò²±»¸¶ÓëÁ˸ü¸ßµÄÖ÷ÒªÐÔ¡£¹ØÓÚweb¿ª·¢ÕßÀ´Ëµ£¬Êý¾Ý¿âÊý¾ÝµÄÇå¾²ÐÔ¡¢¿É¿¿ÐÔºÍÎȹÌÐÔ¶¼ÊÇÖÁ¹ØÖ÷ÒªµÄ¡£Êý¾Ý±íµÄÉè¼ÆºÍÖÎÀíÒ²ÊÇ¿ª·¢ÊÂÇéµÄÒªº¦²¿·Ö£¬Òò´ËÊý¾ÝǨáã³ÉΪÊý¾Ý¿â²Ù×÷µÄÖ÷ÒªÒªÁìÖ®Ò»¡£
PhinxÊÇÒ»¸öPHP¿â£¬Ëü¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¶ÔÓ¦ÓóÌÐò¾ÙÐÐÊý¾Ý¿âǨáãºÍÖÎÀí¡£¶øThinkPHP6×÷Ϊһ¸öÊ¢ÐеÄPHP¿ò¼Ü£¬Ò²ÌṩÁËPhinxµÄʹÓÃÒªÁ죬¿ÉÒÔ¼ò»¯ÎÒÃǵĿª·¢ÊÂÇé¡£
±¾ÎĽ«ÏÈÈÝThinkPHP6¿ò¼ÜÖÐʹÓÃPhinx¾ÙÐÐÊý¾Ý¿âǨáãµÄÒªÁì¡£
×°ÖÃPhinx
ÏÈÔÚÏîĿĿ¼ÏÂʹÓÃComposer×°ÖÃPhinx£º
composer require robmorgan/phinx
µÇ¼ºó¸´ÖÆ
ÉèÖÃPhinx
ÔÚThinkPHP6ÖУ¬PhinxµÄÉèÖÃÔÚconfig/phinx.phpÎļþÖС£¿ÉÒÔͨ¹ýÏÂÁîÐй¤¾ßÌìÉúĬÈÏÉèÖÃÎļþ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
vendor/bin/phinx init
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÍê±Ïºó£¬configĿ¼Ï»áÌìÉúphinx.phpÎļþ¡£ÎÒÃÇÐèÒªÔÚÆäÖÐÉèÖÃÊý¾Ý¿âµÄÅþÁ¬ÐÅÏ¢ºÍĿ¼½á¹¹µÈÐÅÏ¢¡£
ÀýÈ磬ÉèÖÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢£º
return [ 'paths' => [ 'migrations' => '%%PHINX_CONFIG_DIR%%/db/migrations', 'seeds' => '%%PHINX_CONFIG_DIR%%/db/seeds' ], 'environments' => [ 'default_database' => 'development', 'development' => [ 'adapter' => 'mysql', 'host' => '127.0.0.1', 'name' => 'thinkphp6', 'user' => 'root', 'pass' => '123456', 'charset' => 'utf8', ] ] ];
µÇ¼ºó¸´ÖÆ
½¨ÉèÊý¾Ý¿âǨáã
ÔÚThinkPHP6ÖУ¬PhinxµÄÊý¾Ý¿âǨáã´æ´¢ÔÚdatabase/migrationsĿ¼ÖС£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉèÒ»¸öеÄǨá㣺
vendor/bin/phinx create MyNewMigration
µÇ¼ºó¸´ÖÆ
Ö´ÐиÃÏÂÁîºó»áÔÚdatabase/migrationsĿ¼Ï½¨ÉèÒ»¸öеÄǨáãÎļþ¡£
ÔÚPhinxÖУ¬Ç¨áã·ÖΪÁ½ÖÖÀàÐÍ£ºÇ¨áãºÍ»Ø¹ö¡£Ç¨áã°üÀ¨¶ÔÊý¾Ý¿âµÄ¸ü¸Ä£¬»Ø¹öÖ´ÐÐÓëǨáãÏà·´µÄ²Ù×÷¡£
ºÃ±È£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öusers±í£º
use PhinxMigrationAbstractMigration; class CreateUsersTable extends AbstractMigration { /** * Change Method. * * More information on this method is available here: * http://docs.phinx.org/en/latest/migrations.html#the-change-method */ public function change() { $table = $this->table('users'); $table->addColumn('username', 'string', ['limit' => 50]) ->addColumn('email', 'string', ['limit' => 100]) ->addColumn('password', 'string', ['limit' => 255]) ->addColumn('created_at', 'datetime') ->addColumn('updated_at', 'datetime') ->create(); } }
µÇ¼ºó¸´ÖÆ
ÔÚchange()ÒªÁìÖУ¬Ê¹ÓÃ$table±äÁ¿¹¹½¨±í½á¹¹£¬²¢Ê¹ÓÃaddColumn()ÒªÁìΪ²î±ðµÄÁÐÖ¸¶¨Ãü¾ÝÀàÐͺÍÏÞÖÆ¡£×îºóŲÓÃcreate()ÒªÁ죬½«±í½á¹¹½¨Éèµ½Êý¾Ý¿âÖС£
Ö´ÐÐÊý¾Ý¿âǨáã
ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÖ´ÐÐǨá㣺
vendor/bin/phinx migrate
µÇ¼ºó¸´ÖÆ
Phinx»áƾ֤½¨ÉèǨáãµÄÏȺó˳ÐòÖ´Ðиü¸Ä¡£ÈôÊÇËùÓÐǨáãÖ´ÐÐÀֳɣ¬Phinx½«»áÔÚÊý¾Ý¿âÖн¨ÉèÒ»¸öÃûΪphinxlogµÄ±íÒԼͼǨáãÀúÊ·¡£
×¢ÖØ£ºÖ´ÐÐǨá㽫»á¶ÔÊý¾Ý¿â¾ÙÐиü¸Ä£¬ÇëÈ·±£±¸·ÝÊý¾Ý£¬ÒÔ·ÀÊý¾Ýɥʧ»òÎÞ·¨»Ö¸´¡£
»Ø¹öÊý¾Ý¿âǨáã
ÈôÊÇ·ºÆð¹ýʧ»òÐèÒª»ØÍ˸ü¸Ä£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî»ØÍËÉÏÒ»´ÎǨá㣺
vendor/bin/phinx rollback
µÇ¼ºó¸´ÖÆ
Phinx»á»Ø¹öÉÏÒ»´ÎÀÖ³ÉÖ´ÐеÄǨá㣨ÈôÊDZ£´æ£©¡£Phinx»áÔڻعöǨáãºó½«ÐÅϢдÈëµ½phinxlog±íÖС£
×ܽá
ÒÔÉϾÍÊÇÔÚThinkPHP6ÖÐʹÓÃPhinx¾ÙÐÐÊý¾Ý¿âǨáãµÄÀú³Ì¡£Ê¹ÓÃPhinx¿ÉÒÔïÔÌÊÖдSQLµÄÖØ´óÐÔºÍΣº¦£¬Ê¹Êý¾Ý¿âǨáãÀú³ÌÔ½·¢ÇáÓ¯ºÍ¿É¿Ø¡£
ÔÚǨáãÀú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃPhinxµÄ¸ß¼¶¹¦Ð§£¬ÀýÈçÊý¾ÝÌî³ä¡¢Êý¾ÝУÑéºÍÖ§³Ö´ó¶¼¾Ý¿â¡£ÕâЩ¹¦Ð§¿ÉÒÔ´ó´óÌá¸ßÓ¦ÓóÌÐòµÄ¿Éά»¤ÐÔºÍÐÔÄÜ¡£
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃǽ¨Ò齫Êý¾Ý¿âǨáãºÍÖÎÀí×÷ΪÍŶӿª·¢µÄÒ»²¿·Ö£¬ÒÔÈ·±£Êý¾Ý½á¹¹µÄÒ»ÖÂÐԺͿɿ¿ÐÔ¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑùʹÓÃPhinx¾ÙÐÐÊý¾Ý¿âǨá㣿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡