Database migration of migrate in thinkphp5

不言
Release: 2023-03-30 14:14:02
Original
1567 people have browsed it

Here is an introduction to the tp5 migrate database migration tool. It is very simple and practical. Friends in need can take a look at the examples in this article

tp5 is very different from tp3.2

Migrate is one of them. Through migrate, programmers can create database modification rollback and other operations in the PHP code.

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 migrate under database The file

may fail to be created and prompts that there is no solution. Generally, the tp version obtained by composer is too low. Consider modifying the migrate version in the composer.json file to 1.* or ^1.0

and try again. Just composer update

Configure the database in database.php under application

The following is the content of one of the migrate files (after creation) A default method change(), 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

For some of the above methods, I didn’t see the official documentation. What I saw online was Xiao Teng’s explanation

Using migrate :run will execute all the migrate up methods

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

You can use migrate:status Check the current migration execution situation

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

##It is very convenient

The above is the entire article Content, I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

thinkPHP database addition, deletion, modification and query operations

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

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!