2017 © Pedro Peláez
 

library db-importer

In Memory List

image

mauretto78/db-importer

In Memory List

  • Wednesday, March 14, 2018
  • by mauretto78
  • Repository
  • 1 Watchers
  • 0 Stars
  • 232 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 14 % Grown

The README.md

Db-Importer

Scrutinizer Code Quality Build Status Codacy Badge license Packagist, (*1)

This library allows you to import data in your database with very low effort., (*2)

Basic Usage

To use Importer simply do this:, (*3)

use DbImporter\Importer;

// init Importer
$importer = Importer::init(
    $connection, // your DBAL connection
    $table,      // table to import data
    $mapping,    // mapping array
    $data,       // input data
    $ignoreErr,  // ignore errors (boolean). True is default value
    $mode        // insert mode. 'single' or 'multiple' are the only values allowed. 'multiple' is default value
);

// execute import query
$importer->execute()

Please note that you must pass a DBAL Connection instance to Importer class., (*4)

Avaliable drivers

Currently the supported drivers are:, (*5)

  • pdo_mysql (MySQL)
  • pdo_pgsql (PostgreSQL)
  • pdo_sqlite (Sqlite)

Mapping array

The mapping array is a simple key value array in which you specify the column name on your database's table and the corresponding key in the input data. Look at the following example:, (*6)

$mapping = [
    'id' => 'id_utente',             // 'id' is the column name on your database's table. 'id_utente' is the key in input data
    'name' => 'name_utente',         // 'name' is the column name on your database's table. 'name_utente' is the key in input data
    'username' => 'username_utente', // 'username' is the column name on your database's table. 'username_utente' is the key in input data
    'email' => 'email_utente',       // 'email' is the column name on your database's table. 'email_utente' is the key in input data
];

Data

The only requirement is the input data must be iterable (array or object). Here's the most simple example:, (*7)

// as simple associative array
$data = [
    [
        'id_utente' => 1,
        'name_utente' => 'Mauro',
        'email_utente' => 'assistenza@easy-grafica.com',
        'username_utente' => 'mauretto78',
    ],
    [
        'id_utente' => 2,
        'name_utente' => 'John',
        'email_utente' => 'john@doe.com',
        'username_utente' => 'johndoe',
    ],
    [
        'id_utente' => 3,
        'name_utente' => 'Maria',
        'email_utente' => 'maria@key.com',
        'username_utente' => 'maria',
    ]
];

//..

Working with Entities

You can use as your feed data an iterable object of entities. Getters are required. Look at the following example:, (*8)

// User entity
final class User
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $email;

    /**
     * @var string
     */
    private $username;

    /**
     * User constructor.
     * @param $id
     * @param $name
     * @param $email
     * @param $username
     */
    public function __construct(
        $id,
        $name,
        $email,
        $username
    ) {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
        $this->username = $username;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }
}

// use Doctrine\ArrayCollection as feed of Importer
$data = new ArrayCollection([
    new User(
        1,
        'Mauro',
        'assistenza@easy-grafica.com',
        'mauretto78'
    ),
    new User(
        2,
        'John',
        'john@doe.com',
        'johndoe'
    ), 
    new User(
        3,
        'Maria',
        'maria@key.com',
        'maria'
    )
]);

//..

Insert Mode (multiple or single)

You can decide how to build insert query:, (*9)

  • 'multiple' (default) - insert data in a unique multiple insert query
  • 'single' - insert data in a loop of insert queries

Limit of records in multiple insert queries

Please note that there is a limit to the maximum number of records that can be inserted in a single query. In case this limit is exceeded, a loop of multiple insertion queries will be executed., (*10)

This limit is:, (*11)

  • 4000 records for pdo_mysql driver
  • 4000 records for pdo_pgsql driver
  • 200 records for pdo_sqlite driver

Create Schema

If you need to create table scheme, use createSchema() method. Do the following:, (*12)

$keys = [
    'id' => 'integer',
    'album_id' => 'integer',
    'titolo' => 'string',
    'url' => 'string',
    'thumbnail_url' => 'string',
];

$uniqueKeys = ['id'];
$indexKeys = ['album_id', 'titolo'];

$importer->createSchema($keys, $uniqueKeys, $indexKeys);

Destroy Schema

To destroy table scheme, use destroySchema() method:, (*13)

// ..

$importer->destroySchema();

Clear data table

If you want to clear table data (maybe before importing data), use clearData() method instead:, (*14)

// ..

$importer->clearData();

Built With

  • DBAL - Database Abstraction Layer

Requirements

  • PHP 5.6+
  • MySQL 5.7+
  • PostgreSQL 9.5+

Support

If you found an issue or had an idea please refer to this section., (*15)

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details, (*16)

The Versions

08/02 2018
06/02 2018

v1.0.2

1.0.2.0 https://github.com/mauretto78/db-importer

In Memory List

  Sources   Download

MIT

The Requires

 

The Development Requires

database postgres dbal mysql sqlite importer

05/02 2018

v1.0.1

1.0.1.0 https://github.com/mauretto78/db-importer

In Memory List

  Sources   Download

MIT

The Requires

 

The Development Requires

database postgres dbal mysql sqlite importer

05/02 2018

v1.0.0

1.0.0.0 https://github.com/mauretto78/db-importer

In Memory List

  Sources   Download

MIT

The Requires

 

The Development Requires

database postgres dbal mysql sqlite importer