PHP code example of divineomega / uxdm
1. Go to this page and download the library: Download divineomega/uxdm 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/ */
divineomega / uxdm example snippets
$csvSource = new CSVSource('users.csv');
$pdoDestination = new PDODestination(new PDO('mysql:dbname=test-database;host=127.0.0.1', 'root', 'password'), 'users');
$migrator = new Migrator;
$migrator->setSource($csvSource)
->setDestination($pdoDestination)
->setFieldsToMigrate(['id', 'email', 'name'])
->setKeyFields(['id'])
->withProgressBar()
->migrate();
$pdoSource = new PDOSource(new PDO('mysql:dbname=old-test;host=127.0.0.1', 'root', 'password123'), 'users');
$pdoDestination = new PDODestination(new PDO('mysql:dbname=new-test;host=127.0.0.1', 'root', 'password456'), 'new_users');
$migrator = new Migrator;
$migrator->setSource($pdoSource)
->setDestination($pdoDestination)
->setFieldsToMigrate(['id', 'email', 'name'])
->setKeyFields(['id'])
->withProgressBar()
->migrate();
$pdoSource = new PDOSource(new PDO('mysql:dbname=old-test;host=127.0.0.1', 'root', 'password123'), 'users');
$pdoDestination = new PDODestination(new PDO('mysql:dbname=new-test;host=127.0.0.1', 'root', 'password456'), 'new_users');
$migrator = new Migrator;
$migrator->setSource($pdoSource)
->setDestination($pdoDestination)
->setFieldsToMigrate(['id', 'email', 'name'])
->setValidationRules([
'id' => [new Required(), new IsNumeric()],
'email' => [new Required(), new IsString(), new IsEmail()],
'name' => [new Required(), new IsString()],
])
// ->validateBeforeMigrating()
->setKeyFields(['id'])
->withProgressBar()
->migrate();
$migrator = new Migrator;
$migrator->setSource($pdoSource)
->setDestination($pdoDestination)
->setFieldsToMigrate(['id', 'email', 'name'])
->setKeyFields(['id'])
->setFieldMap(['name' => 'full_name'])
->withProgressBar()
->migrate();
class NameCaseTransformer implements TransformerInterface
{
public function transform(DataRow $dataRow): void
{
$nameDataItem = $dataRow->getDataItemByFieldName('name');
$nameDataItem->value = ucwords(strtolower($nameDataItem->value));
}
}
$migrator = new Migrator;
$migrator->setSource($pdoSource)
->setDestination($pdoDestination)
->setFieldsToMigrate(['id', 'email', 'name'])
->setKeyFields(['id'])
->addTransformer(new NameCaseTransformer())
->withProgressBar()
->migrate();
class AddRandomNumberTransformer implements TransformerInterface
{
public function transform(DataRow &$dataRow): void
{
$dataRow->addDataItem(new DataItem('random_number', rand(1,1000)));
}
}
$migrator = new Migrator;
$migrator->setSource($pdoSource)
->setDestination($pdoDestination)
->setFieldsToMigrate(['id', 'email', 'name'])
->setKeyFields(['id'])
->addTransformer(new AddRandomNumberTransformer())
->withProgressBar()
->migrate();
class EmailToHashTransformer implements TransformerInterface
{
public function transform(DataRow $dataRow): void
{
$emailDataItem = $dataRow->getDataItemByFieldName('email');
$dataRow->addDataItem(new DataItem('email_hash', md5($emailDataItem->value)));
$dataRow->removeDataItem($emailDataItem);
}
}
$migrator = new Migrator;
$migrator->setSource($pdoSource)
->setDestination($pdoDestination)
->setFieldsToMigrate(['id', 'email', 'name'])
->setKeyFields(['id'])
->addTransformer(new EmailToHashTransformer())
->withProgressBar()
->migrate();