taylornetwork/backup-importer

v0.1 2018-03-27 20:37 UTC

This package is auto-updated.

Last update: 2024-09-24 12:09:17 UTC


README

此包允许您将数据从不同的备份数据库导入到您的数据库中。

当您重写应用程序且数据库结构发生变化时,这非常有用。

安装

使用Composer

$ composer require taylornetwork/backup-importer

发布配置

$ php artisan vendor:publish

添加备份数据库配置

默认情况下,这将使用您的mysql数据库连接,从您的.env文件中获取DB_BACKUP_DATABASE值或使用backup作为数据库名称。

将连接详细信息添加到config/backup-importer.php

使用方法

运行导入器

$ php artisan importer:run

创建导入器

$ php artisan importer:new CustomerImporter

默认情况下,将生成App\Backup\Importers\CustomerImporter.php导入器。

简单导入器

use TaylorNetwork\BackupImporter\BaseImporter;

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

默认情况下,导入器假定以下内容:

  • 有一个要导入的模型
  • 正在导入的模型是App\Customer(可通过高级配置进行更改)
  • 备份表名为customers
  • 备份表字段与模型的字段相同

高级导入器

您可以在导入器的基础上覆盖上述假设

覆盖模型

在您的导入器中添加一个受保护的$model变量

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

覆盖备份表名称

在您的导入器中添加一个受保护的$backupTableName变量

protected $backupTableName = 'xyz_customers';

忽略模型(用于交叉表)

如果您为此导入器没有模型,设置一个受保护的$ignoreModeltrue

protected $ignoreModel = true;

覆盖备份表中的列

您可以覆盖从备份表获取的列,或重新命名它们。

添加一个公共的getColumnMap()函数,该函数返回要获取的列的数组。

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

示例

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();    
    }
}

自定义import()函数

默认情况下,import()函数将返回$this->simpleImport(),这对于没有关系的简单表是很好的,但是您可能需要自定义导入逻辑。

注意

  • 通过$this->getModel()访问模型
  • 通过$this->items()访问数据库查询数据
  • 通过$this->builder()访问更复杂的查询的流畅构建器
  • 通过$this->select()在select调用之后访问流畅构建器
  • 每次导入一行时,应调用$this->increment()以增加导入行总数
  • 如果您使用$this->increment(),则您的返回语句应为$this->getImportTotal()

示例

假设您有一个应用程序,该应用程序具有客户和服务。每个客户可以有多个服务及其属性。您有以下模型,它们存储在app/

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

对于客户和服务模型,您使用了简单导入方法。

// 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();
    }
}