1. Go to this page and download the library: Download raymondoor/r3t 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/ */
raymondoor / r3t example snippets
use R3T\R3T;
use R3T\Operation\{AddColumnsOperation, CaptureOperation, DistinctOperation, ExtractOperation, JoinOperation, ModifyValuesOperation, RenameOperation, SettleOperation, UnionOperation};
R3T::boot();
R3T::setSourceDB('sqlite', '/path/to/db.sqlite'); // original
R3T::setOperationalDB('sqlite', '/path/to/temp/db.sqlite'); // transactional DB
R3T::setTargetDB('mysql','host=localhost;dbname=r3t;port=3306','user','pass'); // target database
R3T::connectDBs(); // establish PDO connection
$opr = R3T::operator(); // returns OperationManager
$usersOriginal = $opr::register(CaptureOperation::fromTable('users')); // eg. consists of [id, name, group, created_at]
$groupsExtracted = $opr::register(ExtractOperation::from($usersOriginal)->extract('group'));
$distinctGroups = $opr::register(DistinctOperation::from($groupsExtracted)->distinct('group'));
$renameAdjustGroups = $opr::register(RenameOperation::from($distinctGroups)->rename(['group' => 'group_name']));
$addIdToGroup = $opr::register(AddColumnsOperation::from($renameAdjustGroups)->add(['groups_id']));
$populateId = $opr::register(ModifyValuesOperation::from($addIdToGroup)->modify(function(iterable $records){ // callback
$i = 1;
foreach($records as $record){
$record['groups_id'] = $i;
$i++;
yield $record;
// return structure must not change. only values inside
}
}));
$joinToSyncGroupId = $opr::register(JoinOperation::from($usersOriginal)->join($populateId)->on('group_name')->source('group')->direction('left'));
$usersFinal = $opr::register(ExtractOperation::from($joinToSyncGroupId)->extract(['id', 'name', 'groups_id', 'created_at'])); // re-extract
$groupsAdjust = $opr::register(RenameOperation::from($populateId)->rename(['groups_id'=>'id','group_name'=>'name'])); // change col name on groups
$groupsAddTimestamp = $opr::register(AddColumnsOperation::from($groupsAdjust)->add(['created_at'])); // add created_at to groups
$groupsFinal = $opr::register(ModifyValuesOperation::from($groupsAddTimestamp)->modify(function(iterable $records){
foreach($records as $record){
$record['created_at'] = date('Y-m-d H:i:s');
yield $record;
}
})); // add created_at value to groups
$settleGroup = $opr::register(SettleOperation::from($groupsFinal)->settle('groups')); // table and column name must match target
$settleUser = $opr::register(SettleOperation::from($usersFinal)->settle('users')); // settle after groups to respect foreign key constraints
var_dump(R3T::analyze()); // array of operations' query
R3T::createTables(true); // creates intermediate tables. does not touch actual data yet.
R3T::transform(); // execute the transfer apart from settling to new DB
R3T::settle(); // finalize the transfer and insert to the new DB.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.