如何在PHP項目中使用phinx進行資料移轉和建表

來源:互聯網
上載者:User

標籤:命令   team   path   abs   dmi   env   資料   ice   apt   

建表

 

phinx\bin\phinx.bat migrate -e production

建設 phinx.yml檔案

paths:    migrations: %%PHINX_CONFIG_DIR%%\database\migrations    seeds: %%PHINX_CONFIG_DIR%%\database\seedsenvironments:    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%%\database\migrations下面的檔案樣本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 Phinx\Migration\AbstractMigration;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();    }}

  

 

 

資料移轉命令如下:

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‘       => ‘[email protected]‘,              ‘role‘        => Role::APP_ADMIN,          ],        ];        $users = $this->table(‘users‘);        $users->insert($data)              ->save();    }}

  

如何在PHP項目中使用phinx進行資料移轉和建表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.