承接 carono/yii2-migrate 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

carono/yii2-migrate

Composer 安装命令:

composer require carono/yii2-migrate

包简介

Trait to help create databases in migrations for Yii2

README 文档

README

Scrutinizer Code Quality Latest Stable Version Total Downloads License Build Status Code Coverage

[ENG] [RUS]

MigrationTrait

To expand the migration capabilities, you must add a trait \carono\yii2migrate\traits\MigrationTrait or extend the migration class from \carono\yii2migrate\Migration

public function tableOptions()

Return the array with the settings for creating tables, where the key is the name of the db driver.
When creating tables through createTable(), if no properties are specified, they will be picked up from this function

    public function tableOptions()
    {
        return [
            'mysql' => 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'
        ];
    }

public function newTables()

Return an array where the key is the name of the table, and the values are columns with types.
If you call the $this->upNewTables() function, all specified tables will be created via createTable()
If you call the function $this->downNewTables(), all specified table will be deleted using dropTable()

    public function newTables()
    {
        return [
            '{{%logs}}' => [
                'data' => $this->string(),
                '@tableOptions' => [
                    'mysql' => 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=MyISAM'
                ]
            ]
        ];
    }
    
    public function safeUp()
    {
        $this->upNewTables();
    }
    
    public function safeUp()
    {
        $this->downNewTables();
    }

pubic function newColumns()

Return an array where the key is the name of an existing table and the values are columns with types.
If you call the function $this->upNewColumns(), all specified columns will be created using addColumn()
If you call the function $this->downNewColumns(), all specified columns will be deleted after dropColumn()

    public function newColumns()
    {
        return [
            '{{%company}}' => [
                'address' => $this->string(),
                'is_active' => $this->boolean()
            ]
        ];
    }
    
    public function safeUp()
    {
        $this->upNewColumns();
    }
    
    public function safeUp()
    {
        $this->downNewColumns();
    }    

public function newIndex()

Return an array where the key is the name of an existing table and the values are the index parameters via $this->index()
If you call the $this->upNewIndex() function, all specified indexes will be created via createIndex()
If you call the function $this->downNewIndex(), all specified columns will be deleted using the dropIndex()

    public function newIndex()
    {
        return [
            '{{%company}}' => [
                $this->index()->columns(['name'])->unique(true)
            ],
        ];
    }
    
    
    public function safeUp()
    {
        $this->upNewIndex();
    }
    
    public function safeUp()
    {
        $this->downNewIndex();
    }        

Working with foreign keys

Create a table, specifying a foreign key, by table name only

$this->createTable('{{%user}}', [
    'id' => $this->primaryKey(),
    'company_id' => $this->foreignKey('{{%company}}')
]);

Adding a foreign key column

$this->addColumn('{{%user}}', 'company_id', $this->foreignKey('{{%company}}'));

Adding a foreign key to an existing column

$this->alterColumn('{{%user}}', 'company_id', $this->foreignKey('{{%company}}'));

Adding foreign key with auto name

$this->addForeignKey(null, '{{%user}}', 'photo_id', '{{%photo}}', 'id');

Delete foreign key by column name

$this->dropForeignKeyByColumn('{{%user}}', 'company_id');

Working with indexes

Create an index with an automatic name

$this->createIndex(null, '{{%user}}', 'name');

Deleting an index by column name

$this->dropIndexByColumn('{{%user}}', 'name');

(!) It is necessary to pay attention, if there are several columns on the index, then it is necessary to specify them in the necessary sequence. If there are several indexes with such a set and sequence, all of them will be deleted. (!) Does not work correctly with postgreSQL (yiisoft/yii2#16639)

$this->createIndex(null, '{{%user}}', ['name', 'surname']);
$this->dropIndexByColumn('{{%user}}', ['name', 'surname']);

Pivot tables

To implement many-to-many tables, you can use the $this->pivot() function, a table with 2 keys will be created. The names of the keys in the PivotTable are generated automatically, so they can be set via refColumn() and sourceColumn()

Create a PivotTable by creating a table. The result is the table {{%user}}[id] {{%pv_user_photos}}[user_id, photo_id]

$this->createTable('{{%user}}', ['id' => $this->primaryKey(), 'photos' => $this->pivot('{{%photo}}')]);

Create a PivotTable by adding a column.

$this->addColumn('{{%user}}', 'photos', $this->pivot('{{%photo}}'));

Specify the name of the PivotTable

$this->addColumn('{{%user}}', 'photos', $this->pivot('{{%photo}}')->tableName('{{%album}}'));

PivotTrait

Trait to help work with pivot tables.

$company - table model Company (requires trait PivotTrait)
$user - model of table User
PvCompanyDirector - a pivot table of the two models: company and user
Pivot table - a table which contains 2 primary key

Added to the table PvCompanyDirector a bunch of the end user company

$company->addPivot($user, PvCompanyDirector::class, $attributes = []);

Get the PvCompanyDirector model for the company-user bundle

$company->getPivot($model, PvCompanyDirector::class, $condition = []);

Removed a bunch of the user-company

$company->deletePivot($model, PvCompanyDirector::class);

Remove all users from PvCompanyDirector for this company

$company->deletePivots(PvCompanyDirector::class);

Save to a temporary link variable so that you can use them later

$company->storagePivot($user, PvCompanyDirector::class, ['hire_at' => '2010-01-01 00:00:00']);
$users = $company->getStoragePivots(PvCompanyDirector::class)); // The list of models that have been added earlier

The preservation of the ties of a temporary variable.
$clear - completely clears all links before adding

$company->savePivots($clear); // Save all links added via storagePivot

The change in behavior of the migration class

public function createIndex($name, $table, $columns, $unique = false)

  • $name you can specify null to generate the index name automatically

public function createTable($table, $columns, $options = null)

  • $columns supports the $this->foreignKey() and $this->pivot()
  • if $options is not specified, options are pulled from $this->tableOptions, if there are no options, then from @tableOptions to $columns

public function alterColumn($table, $column, $type)

  • $type supports type $this->foreignKey()

public function addColumn($table, $column, $type)

  • $type supports type $this->foreignKey() and $this->pivot()

public function addPrimaryKey($name, $table, $columns)

  • $name you can specify null to generate the index name automatically

public function dropColumn($table, $column)

  • Before deleting the table, foreign keys are cleared

An example of a complete migration

<?php

use yii\db\Migration;
use \yii\db\Schema;
/**
 * Class m180712_120503_init
 */
class m180712_120503_init extends Migration
{
    use \carono\yii2migrate\traits\MigrationTrait;

    public function tableOptions()
    {
        return [
            'mysql' => 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'
        ];
    }

    public function newTables()
    {
        return [
            '{{%logs}}' => [
                'data' => $this->string(),
                '@tableOptions' => [
                    'mysql' => 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=MyISAM'
                ]
            ],
            '{{%user}}' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(),
                'parents' => $this->pivot('{{%user}}') // Create a pivot table on itself
            ],
            '{{%photo}}' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer()
            ],
            '{{%company}}' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(),
                // Create a pivot table {{%pv_company_directors}}
                'directors' => $this->pivot('{{%user}}', 'director_id')->columns(
                    [
                        'hire_at' => $this->dateTime(),
                        // A foreign key with SET NULL rule is when you remove data from {{%user}}
                        'hired_id' => $this->foreignKey('{{%user}}', null)->onDeleteNull()->unsigned()
                    ]
                ),
                '@tableOptions' => [
                    'mysql' => 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'
                ]
            ],
            '{{%pv_company_user_photo}}' => [
                // Create a PivotTable of several keys
                'company_id' => $this->foreignKey('{{%company}}', null, Schema::TYPE_PK),
                'user_id' => $this->foreignKey('{{%user}}', null, Schema::TYPE_PK),
                'photo_id' => $this->foreignKey('{{%photo}}', null, Schema::TYPE_PK),
            ]
        ];
    }

    public function newColumns()
    {
        return [
            '{{%company}}' => [
                // Create a FK to user
                'user_id' => $this->foreignKey('{{%user}}'),
                // Create a pivot table employees
                'users' => $this->pivot('{{%user}}')->tableName('{{%employees}}')
            ]
        ];
    }

    public function newIndex()
    {
        return [
            '{{%company}}' => [
                $this->index()->columns(['name'])->unique(true)->length(10)
            ],
        ];
    }

    public function safeUp()
    {
        $this->upNewTables();
        $this->upNewColumns();
        // Add a FK to an existing column
        $this->alterColumn('{{%photo}}', 'user_id', $this->foreignKey('{{%user}}'));
        $this->upNewIndex();
        $this->createIndex(null, '{{%user}}', 'name');
    }

    public function safeDown()
    {
        $this->dropIndexByColumn('{{%user}}', 'name');
        $this->downNewIndex();
        // Remove the FK on the column name
        $this->dropForeignKeyByColumn('{{%photo}}', 'user_id');
        $this->downNewColumns();
        $this->downNewTables();
    }
}

The resulting database schema

carono/yii2-migrate 适用场景与选型建议

carono/yii2-migrate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.29k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 carono/yii2-migrate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 carono/yii2-migrate 我们能提供哪些服务?
定制开发 / 二次开发

基于 carono/yii2-migrate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 8
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-20