Detailed explanation of database migration using thinkphp5 migrate

php中世界最好的语言
Release: 2023-03-26 16:22:02
Original
1973 people have browsed it

This time I will bring you a detailed explanation of the use of thinkphp5 migrate database migration. What are the precautions when using thinkphp5 migrate database migration. The following is a practical case, let's take a look.

tp5 is very different from tp3.2

migrate is one of them. Through migrate, programmers can create a database in php code #Modify rollback and other operations

First download the migrate extension, and execute the command line in the current project directory

composer require topthink/think-migration
Copy after login
You can check whether migrate is successfully downloaded by using the php think command

Use migrate: create migrate file name (capital camel case method) to generate the migrate file under the database

It is possible that the creation fails and prompts that there is no method. Generally, the tp obtained by composer The version is too low, consider modifying the migrate version in the composer.json file to 1.* or ^1.0

Re-composer update

In the application Configure the database in the database.php under

The following is the content of one of the migrate files (there is a default method change() after creation, delete it)

 use think\migration\Migrator;
 use think\migration\db\Column;
 class CreateUserTable extends Migrator
 {
   /**
   * 建立用户表
   */
   public function up(){
     $table = $this->table('user');
     $table->addColumn('username' , 'string' , ['limit' => 30])
        ->addColumn('passwork' , 'string' , ['limit' => 32])
        ->addColumn('email' , 'string' , ['limit' => 25])
        ->addColumn('lastlogin_ip' , 'string' , ['limit' => 15])
        ->addTimestamps('create_time' , 'lastlogin_time')
        ->addColumn('status' , 'integer' , ['limit' => 1 , 'default' => 1])
        ->setId('user_id')
        ->save();
   }
   /**
   * 提供回滚的删除用户表方法
   */
   public function down(){
     $this->dropTable('user');
   }
 }
Copy after login
Some of the above methods , I didn’t see the official document. What I read online was what Xiao Teng explained.

Using migrate: run will execute all the migrate up methods.

You can use migrate: rollback. Roll back the last executed migrate file (with -t 0 parameter to roll back all)

You can view the current migration execution status through migrate: status

After executing the run method, the user table is successfully created

Very convenient

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps for Laravel to use Redis to share Session

Detailed explanation of the steps for Laravel to implement password reset

The above is the detailed content of Detailed explanation of database migration using thinkphp5 migrate. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!