PHP code example of mauretto78 / db-importer

1. Go to this page and download the library: Download mauretto78/db-importer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mauretto78 / db-importer example snippets


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


$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
];


// as simple associative array
$data = [
    [
        'id_utente' => 1,
        'name_utente' => 'Mauro',
        'email_utente' => '[email protected]',
        'username_utente' => 'mauretto78',
    ],
    [
        'id_utente' => 2,
        'name_utente' => 'John',
        'email_utente' => '[email protected]',
        'username_utente' => 'johndoe',
    ],
    [
        'id_utente' => 3,
        'name_utente' => 'Maria',
        'email_utente' => '[email protected]',
        'username_utente' => 'maria',
    ]
];

//..

// 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',
        '[email protected]',
        'mauretto78'
    ),
    new User(
        2,
        'John',
        '[email protected]',
        'johndoe'
    ), 
    new User(
        3,
        'Maria',
        '[email protected]',
        'maria'
    )
]);

//..

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

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

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

// ..

$importer->destroySchema();

// ..

$importer->clearData();