如何在PHP项目中使用phinx进行数据迁移和建表

如何在PHP项目中使用phinx进行数据迁移和建表
最新回答
挂名女友

2026-03-31 10:28:35

建表

phinx\bin\phinx.bat migrate -e production

建设 phinx.yml文件
paths:
migrations: %%PHINX_CONFIG_DIR%%\database\migrations
seeds: %%PHINX_CONFIG_DIR%%\database\seeds

environments:
default_migration_table: phinxlog
default_database: development
production:
adapter: mysql
host: localhost
name: jitamin2
user: root
pass: ‘‘
port: 3306
charset: utf8

development:
adapter: mysql
host: localhost
name: development_db
user: root
pass: ‘‘
port: 3306
charset: utf8

testing:
adapter: mysql
host: localhost
name: testing_db
user: root
pass: ‘‘
port: 3306
charset: utf8

数据迁移命令如下:
phinx\bin\phinx.bat seed:run -e production

%%PHINX_CONFIG_DIR%%\database\seeds下面的文件示例CreateGroupsTable.php如下:
<?php

/*
* This file is part of Jitamin.
*
* Copyright (C) Jitamin Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Jitamin\Foundation\Security\Role;
use Phinx\Seed\AbstractSeed;

class UserSeeder extends AbstractSeed
{
/**
* Run Method.
*/
public function run()
{
$data = [
[
‘username‘ => ‘admin‘,
‘password‘ => bcrypt(‘admin‘),
‘email‘ => ‘admin@admin.com‘,
‘role‘ => Role::APP_ADMIN,
],
];

$users = $this->table(‘users‘);
$users->insert($data)
->save();
}
}