Skip to content

Deep_Copy

Romans Malinovskis edited this page Oct 18, 2018 · 2 revisions

Assuming you have the following models:

$invoice->hasMany('Lines', new InvoiceLine());
$payment->hasMany('Allocations', new Allocation());

// Allocation hasOne('invoice_id', Invoice());

you may want to copy to copy your structure either into a different data-set (e.g. Archived Invoices) or perhaps into different persistence, there are number of things to consider:

  • field structure may be different
  • data type mapping may be different
  • conditions may be different
  • relation structure may be different
  • ID will be different
  • internal references within our structure must be preserved (Allocation is many-to-many relationship)

The [Deep copy proposal would implement the entire process taking care of all items above and would allow developer to narrow down the launch the process with the code:

$associations = $invoice->deepCopy($archived_invoices, ['Lines']);
$associations = $payment->deepCopy($archived_payments, ['Allocations'], $associations);

// every time you deepCopy, more records will be added to associations.

Ultimately your code may look like this:

$src_db = Persistence::connect();
$dst_db = Persistence::connect(..);

$dest_db->atomic(function() use($src_db, $dst_db) {
  // transaction. Copy invoices first.

  $invoices = new Invoice($src_db);

  $allocations = $invoices->deepCopy(new Invoice($dst_db), ['Lines'], ['id'=>'original_id']);
  $payments = $invoices->ref('Allocations')->ref('payment_id');  // will need all payment data

  

});



Discuss here: https://forum.agiletoolkit.org/t/atk-data-deep-copy-feature/562