How to implement database 在CodeIgniter-4命令中迁移?

How to implement database 在CodeIgniter-4命令中迁移?

在CodeIgniter-4命令中迁移

Websites have become more demanding for businesses, now-a-days they deliver more values than past years. 根据报告 w3techs, PHP – Most admired scripting language for frontend and backend which is widely used as compared to other programming languages. Around 77% of all the websites on the internet are developed in PHP and other PHP开发框架.

When the time comes to decide with which PHP framework one should go for web development projects, 这应该是一个容易决定的任务. 借助这些PHP框架 网站开发 任务可以是多种多样的 & faster. Amongst them, CodeIgniter is one of the most widely used PHP frameworks.

Modern day programming is used for developing large scale projects nowadays and the work is distributed under several developers. Each of them have to work with either a separate database or work on the same Database. And the developers need to change and also keep track of the database for the same project. In this case database migration comes in picture.

Database migrations in Codeigniter-4 are used to track and run the changes of the database with the help of Command Line Interface with the help of Spark. Spark is the built-in official commands provided by Codeigniter-4.

See Also: Developers’ guide for establishing and adhering to Coding Standards

These are some of the most used database migration spark commands used in Codeigniter-4. Open the project directory in Command Prompt and run the following commands:

1. php spark make:migration [name-of-migration]

This automatically creates the migration file for the project under 应用程序/数据库/迁移/ 项目中的目录.

Example:

php spark make:migration Testmigrate . php
In this file there are mainly two functions

  1. up()
  2. down()
Example:
公共功能up()
{
$this->forge->addField([
            'blog_unique_id' => [
                'type'           => 'BIGINT',
                'constraint'     => 20,
                'auto_increment' => true,
            ],
            'blog_title' => [
                'type'       => 'VARCHAR',
                'constraint' => '150',
            ],
            'blog_details' => [
                'type' => 'TEXT',
                'null' => true,
            ],
        ]);
        $this->forge->addKey(blog_unique_id, true);
        $this->forge->createTable('tbl_blog');
}

In down() functions handles the rollback operations performs on the database 

Example
公共函数down()
    {
        $this->forge->dropTable('tbl_blog');
    }

In the given example the in down method there are dropTable blog for remove table from Database

See Also: 8 Advantages of Custom PHP开发

2. PHP spark迁移

Migrate command is used to execute the migrations and perform Database Operations. The above command runs and executes all the migrations. 它只执行 up() 方法

3. PHP spark migrate:回滚

The above Command runs and executes the down() 迁移方法.

4. PHP spark migrate:刷新

The above Command Refreshes the database by simply performing rollback first and then the migration. 这意味着它首先执行 down() 方法,然后 up() 迁移方法 

5. PHP spark migrate:状态

It displays the graphical representation of the status of all the migrations 

Example

PHP spark migrate:状态

6. PHP spark make:seed [name-of-seed]

This automatically creates the migration file for the project under 应用程序/数据库/种子/ 项目中的目录. 播种机用于

Example:
php spark

//Example:
公共函数run()
{
$this->db->table('tbl_blog)->truncate(); 
$this->db->table('tbl_blog')->insert([
    "blog_title" => "hello",
    "blog_details" => 'world', 
]); 
}

在上面的例子中, remove all the fields from Database and then insert a new blog with blog_title of ‘hello’ 和blog_details with ‘world’.

7. PHP spark db:种子 

Seed command is used to execute the seeding and perform Database Operations. The above command runs and executes all the seeds at time. This needs the seeder name for the parameter

8. PHP spark db:seed [name-of-seed]

The above command runs and executes the individual seeds at a time by the file path parameter. 

For more details please review official documentation by codeigniter4 Team below.

http://codeigniter4.github.io/userguide/dbmgmt/migration.html

最后的想法:

你在做什么 CodeIgniter项目 and want to get your database migrated or facing issues while altering the database? SpryBit is here with a solution for you, we have expertise in CodeIgniter发展. 今天就贝博体彩APP下载的专家!

CodeIgniter,PHP,软件开发,Web开发
, , , , ,

2 Replies to “How to implement database 在CodeIgniter-4命令中迁移?”

  1. Nikunj Gondaliya says:

    Thank you ojash for valuable information for codeIgniter 4 migration commands.

  2. Keval Moradiya says:

    优秀的文章! Learned valuable tips for migrating data to CodeIgniter 4

Leave a Reply

Your email address will not be published. 必填项被标记 *

特别感谢! for visiting.

你脑子里的任何计划?

Contact us 或打电话给我们获得免费的估计