定制 taylornetwork/backup-importer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

taylornetwork/backup-importer

Composer 安装命令:

composer require taylornetwork/backup-importer

包简介

README 文档

README

This package will allow you to import data to your database from a backup database that is not identical.

This is useful if you re-write an application and the database structure changes.

Install

Using Composer

$ composer require taylornetwork/backup-importer

Publish Config

$ php artisan vendor:publish

Add Backup Database Config

By default this will use your mysql database connection with the DB_BACKUP_DATABASE value from your .env file or backup as the database name

Add connection details to config/backup-importer.php

Usage

Run your importers

$ php artisan importer:run

Create an Importer

$ php artisan importer:new CustomerImporter

Would generate an importer App\Backup\Importers\CustomerImporter.php by default.

Simple Importer

use TaylorNetwork\BackupImporter\BaseImporter;

class CustomerImporter extends BaseImporter
{
    public function import(): int
    {
        return $this->simpleImport();
    }
}

By default the importer assumes the following

  • There is a model to import to
  • The model being imported is App\Customer (see Advanced Config to change)
  • The backup table name is customers
  • The backup table fields are all the same as the model's fields

Advanced Importers

You can override the above assumptions on an importer by importer basis

Override Model

Add a protected $model variable in your importer

protected $model = App\Models\Customer::class;

Override the Backup Table Name

Add a protected $backupTableName variable in your importer

protected $backupTableName = 'xyz_customers';

Ignore Model (For a Pivot Table)

If you don't have a model for this importer set a protected $ignoreModel to true

protected $ignoreModel = true;

Override Columns From Backup Table

You can override the columns that are taken from the backup table, or rename them.

Add a public getColumnMap() function that returns the array of columns to get.

public function getColumnMap(): array
{
    return [
        'firstname as first_name,
        'lastname as last_name',
        'address',
    ];
}

Example

use TaylorNetwork\BackupImporter\BaseImporter;

class CustomerImporter extends BaseImporter
{
    /**
     * Set the model to import to
     */
    protected $model = App\Models\Customer::class;
    
    /**
     * Set the backup table name
     */
    protected $backupTableName = 'xyz_customers';
    
    /**
     * Set the columns to get from the backup table
     */
    public function getColumnMap(): array
    {
        return [
            'firstname as first_name',
            'lastname as last_name',
            'address',
        ];
    }

    
    public function import(): int
    {
       return $this->simpleImport();    
    }
}

Customizing the import() function

The import() function by default will return $this->simpleImport() which is fine for simple tables with no relations, however you will likely want to customize the import logic.

Notes

  • Access the model by $this->getModel()
  • Access the database query data by $this->items()
  • Access the fluent builder for more complex queries by $this->builder()
  • Access the fluent builder AFTER the select call by $this->select()
  • Whenever you import a row you should call $this->increment() to add to the total of rows imported
  • If you use $this->increment() your return statement should be $this->getImportTotal()

Example

Let's say you have an application that has customers and services. Each customer can have many services with properties. You have the following models which you store in app/

  • App\Customer
  • App\Service
  • App\CustomerService

For the customer and service models, you used the simple import method.

// App\Backup\Importers\CustomerServiceImporter.php

use TaylorNetwork\BackupImporter\BaseImporter;
use App\Customer;

class CustomerServiceImporter extends BaseImporter
{
    public function getColumnMap(): array
    {
        return [
            'customer_id',
            'service_id',
            'qty',
            'description',
            'last_service_date',
        ];
    }
    
    public function import(): int
    {
        $rows = $this->select()->where('last_service_date', '!=', null)->get();
        
        foreach($rows as $row) {
            Customer::find($row->customer_id)->services()->create([
                'service_id' => $row->service_id,
                'qty' => $row->qty,
                'desc' => $row->description,
                'last_date' => $row->last_service_date,
            ]);
            
            $this->increment();
        }
        
        return $this->getImportTotal();
    }
}

taylornetwork/backup-importer 适用场景与选型建议

taylornetwork/backup-importer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 03 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 taylornetwork/backup-importer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-27