ThinkPHP6ÖÐÔõÑùʵÏÖÊý¾Ý¿âǨá㣿
Ëæ×ÅÓ¦ÓóÌÐòÒ»Ö±ÑݽøºÍÐèÇóµÄһֱת±ä£¬ÎÒÃÇÔÚ¿ª·¢Àú³ÌÖо³£ÐèÒª¶ÔÊý¾Ý¿â¾ÙÐÐÐ޸ġ¢Ç¨áãºÍ¸üС£¿ÉÊÇÔÚ¸üÐÂÊý¾Ý¿âµÄÀú³ÌÖУ¬ÈôÊÇδ¾ÈÏÕæ˼Á¿ºÍά»¤£¬¾Í¿ÉÄ᷺ܻÆðÊý¾Ý³åÍ»¡¢Êý¾ÝɥʧµÈһϵÁÐÎÊÌ⡣ΪÁËÓÐÓõؽâ¾öÕâЩÎÊÌ⣬ÎÒÃÇÐèҪʹÓÃÒ»ÖÖרҵµÄÊý¾Ý¿âǨá㹤¾ßÀ´Íê³ÉÕâЩ²Ù×÷¡£
ThinkPHP6ÊÇÒ»¿îÓÃÓÚ¹¹½¨WebÓ¦ÓóÌÐòµÄÊ¢ÐÐPHP¿ò¼Ü£¬ËüÌṩÁËÐí¶àÓÐÓõĹ¦Ð§ºÍ¹¤¾ß£¬ÆäÖаüÀ¨Êý¾Ý¿âǨáã¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚThinkPHP6ÖÐʹÓÃÊý¾Ý¿âǨáãµÄ¹¦Ð§¡£
Ò»¡¢Ê²Ã´ÊÇÊý¾Ý¿âǨá㣿
Êý¾Ý¿âǨáãÊÇÖ¸ÔÚÓ¦ÓóÌÐòµÄ¿ª·¢ºÍÔËάÀú³ÌÖУ¬¶ÔÊý¾Ý¿â½á¹¹¾ÙÐиüкÍÐ޸ĵÄÀú³Ì¡£Ò²¾ÍÊÇ˵£¬Í¨¹ýÊý¾Ý¿âǨá㣬ÎÒÃÇ¿ÉÒÔ¾ÙÐÐÊý¾Ý¿â±íµÄÔöÌí¡¢É¾³ý¡¢ÖØÃüÃûºÍ×ֶεÄÌí¼Ó¡¢Ð޸ġ¢É¾³ýµÈ²Ù×÷¡£
ÔÚThinkPHP6ÖУ¬Êý¾Ý¿âǨá㹤¾ßÖ÷Ҫͨ¹ýÁ½¸ö¿´·¨À´ÊµÏÖ£ºÇ¨áãÀàºÍÌî³äÀà¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢ÔõÑùʹÓÃÊý¾Ý¿âǨá㣿
½¨ÉèǨáãÀà
ÔÚThinkPHP6ÖÐʹÓÃÊý¾Ý¿âǨá㣬Ê×ÏÈÐèÒª½¨ÉèÒ»¸öǨáãÀࡣǨáãÀàÊÇÒ»¸ö°üÀ¨Á½¸öÒªÁìµÄPHPÀ࣬ÕâÁ½¸öÒªÁì»®·ÖÊÇupÒªÁìºÍdownÒªÁì¡£
upÒªÁìÓÃÓÚÖ´ÐÐÊý¾Ý¿âǨáã²Ù×÷£¬ÀýÈ罨Éè¡¢Ð޸ġ¢É¾³ýÊý¾Ý¿â±í»ò×ֶεȡ£down·½¹æÔòÓÃÓڻعöǨáã²Ù×÷£¬¼´×÷·ÏupÒªÁìÖ´ÐеIJÙ×÷¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀýǨáãÀ࣬ÓÃÓÚ½¨ÉèÒ»¸öÃûΪusersµÄÊý¾Ý¿â±í£º
<?php use thinkmigrationdbColumn; use thinkmigrationMigrator; class CreateUsersTable extends Migrator { public function up() { $table = $this->table('users', ['engine' => 'InnoDB', 'id' => false, 'primary_key' => 'id']); $table->addColumn('id', 'integer', ['signed' => false, 'identity' => true]) ->addColumn('name', 'string', ['limit' => 32]) ->addColumn('email', 'string', ['limit' => 128]) ->addColumn('password', 'string', ['limit' => 60]) ->addColumn('created_at', 'datetime') ->addColumn('updated_at', 'datetime') ->create(); } public function down() { $this->table('users')->drop(); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄǨáãÀàÖУ¬ÎÒÃÇÏÈÔÚupÒªÁìÖн¨ÉèÁËÒ»¸öÃûΪusersµÄÊý¾Ý¿â±í£¬²¢Ìí¼ÓÁËһЩ×ֶΡ£È»ºóÔÚdownÒªÁìÖУ¬ÎÒÃÇɾ³ýÁËÕâ¸ö±í¡£
Ö´ÐÐÊý¾Ý¿âǨáã
½¨ÉèºÃǨáãÀàÖ®ºó£¬ÎÒÃÇÐèҪʹÓÃphp think migrateÏÂÁîÀ´Ö´ÐÐǨáã²Ù×÷¡£ÈôÊÇÄ¿½ñ»¹Ã»ÓоÙÐйýÈκÎǨá㣬ÎÒÃÇÐèÒªÏÈʹÓÃphp think migrate:installÏÂÁîÀ´³õʼ»¯Êý¾Ý¿âǨáãµÄÏà¹Ø±í¡£
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔʹÓÃphp think migrate:statusÏÂÁîÀ´Éó²éÄ¿½ñËùÓеÄǨáã״̬¡£´Ëʱ£¬ÎÒÃÇ¿ÉÒÔ¿´µ½ÎÒÃǽ¨ÉèµÄǨáãÀ໹δִÐС£
È»ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃphp think migrate:runÏÂÁîÀ´Ö´ÐÐǨáã²Ù×÷¡£´Ëʱ£¬ÎÒÃÇ¿ÉÒÔ¿´µ½ÏµÍ³»áÖ´ÐÐÎÒÃÇÊʲŽ¨ÉèµÄǨáãÀ࣬²¢Êä³öһЩµ÷ÊÔÐÅÏ¢¡£
Ö´ÐÐÍê±Ïºó£¬ÎÒÃÇ¿ÉÒÔÔÙ´ÎʹÓÃphp think migrate:statusÏÂÁîÀ´Éó²éÄ¿½ñËùÓеÄǨáã״̬¡£´Ëʱ£¬ÎÒÃÇÓ¦¸Ã¿ÉÒÔ¿´µ½ÎÒÃǽ¨ÉèµÄǨáãÀàÒѾִÐÐÀֳɣ¬Êý¾Ý¿âÖÐÒѾ½¨ÉèÁËÃûΪusersµÄ±í¡£
ÈôÊÇÎÒÃÇÐèÒª»Ø¹öÊʲŽ¨ÉèµÄǨá㣬¿ÉÒÔʹÓÃphp think migrate:rollbackÏÂÁî¡£Õâ¸öÏÂÁî»á»Ø¹ö×îºóÒ»´ÎÖ´ÐеÄǨáã²Ù×÷¡£
ÁíÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃphp think migrate:resetÏÂÁîÍù·µ¹öËùÓÐÒѾִÐеÄǨáã²Ù×÷¡£
Èý¡¢ÔõÑùʹÓÃÌî³äÀࣿ
³ýÁËǨáãÀ࣬ThinkPHP6»¹ÌṩÁËÒ»ÖÖ³ÆΪÌî³äÀàµÄ¹¦Ð§¡£Ìî³äÀàÓÃÓÚÏòÒѾ±£´æµÄÊý¾Ý¿â±íÖÐÌí¼Ó²âÊÔÊý¾Ý»òĬÈÏÊý¾Ý¡£
ÓëǨáãÀàÀàËÆ£¬Ìî³äÀàÒ²ÊÇÒ»¸ö°üÀ¨Á½¸öÒªÁìµÄPHPÀ࣬ÕâÁ½¸öÒªÁì»®·ÖÊÇupÒªÁìºÍdownÒªÁì¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀýÌî³äÀ࣬ÓÃÓÚÏòÃûΪusersµÄÊý¾Ý¿â±íÖÐÌí¼ÓһЩ²âÊÔÊý¾Ý£º
<?php use thinkmigrationSeed; class AddTestUsers extends Seed { public function run() { $data = [ [ 'name' => 'ÕÅÈý', 'email' => 'zhangsan@test.com', 'password' => password_hash('123456', PASSWORD_DEFAULT), 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), ], [ 'name' => 'ÀîËÄ', 'email' => 'lisi@test.com', 'password' => password_hash('123456', PASSWORD_DEFAULT), 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), ], [ 'name' => 'ÍõÎå', 'email' => 'wangwu@test.com', 'password' => password_hash('123456', PASSWORD_DEFAULT), 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), ], ]; $this->table('users')->insert($data)->save(); } public function down() { $this->execute('TRUNCATE TABLE users;'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÌî³äÀàÖУ¬ÎÒÃÇÔÚupÒªÁìÖÐʹÓÃinsertÒªÁìÏòÊý¾Ý¿â±íÖÐÌí¼ÓÁËÈýÌõ²âÊÔÊý¾Ý¡£È»ºóÔÚdownÒªÁìÖУ¬ÎÒÃÇʹÓÃexecuteÒªÁìɾ³ýÁËÕâÈýÌõ²âÊÔÊý¾Ý¡£
Ö´ÐÐÌî³äÀàµÄÒªÁìÓëÖ´ÐÐǨáãÀàµÄÒªÁìÀàËÆ£¬ÎÒÃÇ¿ÉÒÔʹÓÃphp think seed:runÏÂÁîÖ´ÐÐÌî³äÀàµÄupÒªÁì¡£ÈôÊÇÐèÒª»Ø¹öÌî³äÀàµÄʹÃü£¬¿ÉÒÔʹÓÃphp think seed:rollbackÏÂÁî¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔÚThinkPHP6ÖÐʹÓÃÊý¾Ý¿âǨáãºÍÌî³äµÄÒªÁ죬²¢ÇÒͨ¹ýʾÀý´úÂëÑÝʾÁËÔõÑù½¨ÉèǨáãÀàºÍÌî³äÀ࣬ÒÔ¼°ÔõÑùÖ´ÐкͻعöǨáãºÍÌî³ä²Ù×÷¡£ÈôÊÇÄãÕýÔÚ¿ª·¢ºÍά»¤Ò»¸öÓ¦ÓóÌÐò£¬²¢ÇÒÐèÒª¶ÔÊý¾Ý¿â¾ÙÐÐÐ޸ĺ͸üУ¬ÇëÒ»¶¨Ê¹ÓÃÕâÖÖרҵµÄÊý¾Ý¿âǨá㹤¾ß£¬ÒÔ°ü¹ÜÄãµÄÊý¾ÝÇå¾²ºÍ׼ȷÐÔ¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑùʵÏÖÊý¾Ý¿âǨá㣿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡