zoukankan      html  css  js  c++  java
  • 如何在PHP项目中使用phinx进行数据迁移和建表

    建表

    phinxinphinx.bat migrate -e production
    

    建设 phinx.yml文件

    paths:
        migrations: %%PHINX_CONFIG_DIR%%databasemigrations
        seeds: %%PHINX_CONFIG_DIR%%databaseseeds
    
    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_CONFIG_DIR%%databasemigrations下面的文件示例20161222061456_create_users_table.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 PhinxMigrationAbstractMigration;
    
    class CreateUsersTable extends AbstractMigration
    {
        /**
         * Change Method.
         */
        public function change()
        {
            $table = $this->table('users');
            $table->addColumn('username', 'string', ['limit'=>50])
                  ->addColumn('password', 'string', ['null' => true])
                  ->addColumn('is_ldap_user', 'boolean', ['null' => true, 'default' => false])
                  ->addColumn('name', 'string', ['null' => true])
                  ->addColumn('email', 'string')
                  ->addColumn('google_id', 'string', ['null'=> true, 'limit' => 30])
                  ->addColumn('github_id', 'string', ['null' => true, 'limit' => 30])
                  ->addColumn('notifications_enabled', 'boolean', ['null' => true, 'default' => false])
                  ->addColumn('timezone', 'string', ['null' => true, 'limit' => 50])
                  ->addColumn('language', 'string', ['null' => true, 'limit' => 5])
                  ->addColumn('disable_login_form', 'boolean', ['null' => true, 'default' => false])
                  ->addColumn('twofactor_activated', 'boolean', ['null' => true, 'default' => false])
                  ->addColumn('twofactor_secret', 'string', ['null' => true, 'limit' => 16])
                  ->addColumn('token', 'string', ['null'=> true, 'default' => ''])
                  ->addColumn('notifications_filter', 'integer', ['null' => true, 'default' => 4])
                  ->addColumn('nb_failed_login', 'integer', ['null' => true, 'default' => 0])
                  ->addColumn('lock_expiration_date', 'biginteger', ['null' => true])
                  ->addColumn('gitlab_id', 'integer', ['null' => true])
                  ->addColumn('role', 'string', ['limit' => 25, 'default' => 'app-user'])
                  ->addColumn('is_active', 'boolean', ['null' => true, 'default' => true])
                  ->addColumn('avatar_path', 'string', ['null' => true])
                  ->addColumn('skin', 'string', ['null' => true, 'limit'=>15])
                  ->addIndex(['username'], ['unique' => true])
                  ->addIndex(['email'], ['unique' => true])
                  ->create();
        }
    }
    

      

    数据迁移命令如下:

    phinxinphinx.bat seed:run -e production
    
    %%PHINX_CONFIG_DIR%%databaseseeds下面的文件示例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 JitaminFoundationSecurityRole;
    use PhinxSeedAbstractSeed;
    
    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();
        }
    }
    

      

  • 相关阅读:
    Nokia N78拍照最佳设置!
    摩托Milestone购机鉴别篇
    sdf
    js随笔
    jssip无法识别以数字开始的域
    ASP.NET页面生命周期与优化
    在SQLServer2005中使用全文搜索
    简析正则表达式
    《WCF编程》之错误
    《WCF编程》之实例管理
  • 原文地址:https://www.cnblogs.com/haoliansheng/p/6640721.html
Copyright © 2011-2022 走看看