dev-master
9999999-dev
MIT
The Requires
by Sam Taylor
v0.1
0.1.0.0
MIT
The Requires
by Sam Taylor
Wallogit.com
2017 © Pedro Peláez
This package will allow you to import data to your database from a backup database that is not identical., (*1)
This is useful if you re-write an application and the database structure changes., (*2)
Using Composer, (*3)
$ composer require taylornetwork/backup-importer
$ php artisan vendor:publish
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, (*4)
Add connection details to config/backup-importer.php, (*5)
$ php artisan importer:run
$ php artisan importer:new CustomerImporter
Would generate an importer App\Backup\Importers\CustomerImporter.php by default., (*6)
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, (*7)
You can override the above assumptions on an importer by importer basis, (*8)
Add a protected $model variable in your importer, (*9)
protected $model = App\Models\Customer::class;
Add a protected $backupTableName variable in your importer, (*10)
protected $backupTableName = 'xyz_customers';
If you don't have a model for this importer set a protected $ignoreModel to true, (*11)
protected $ignoreModel = true;
You can override the columns that are taken from the backup table, or rename them., (*12)
Add a public getColumnMap() function that returns the array of columns to get., (*13)
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() functionThe 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., (*14)
$this->getModel()
$this->items()
$this->builder()
$this->select()
$this->increment() to add to the total of rows imported$this->increment() your return statement should be $this->getImportTotal()
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/, (*15)
App\CustomerApp\ServiceApp\CustomerServiceFor the customer and service models, you used the simple import method., (*16)
// 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();
}
}
MIT
MIT