Skip to content

Instantly share code, notes, and snippets.

@TheRatG
Created August 19, 2017 08:55
Show Gist options
  • Save TheRatG/e1f6f47711b16741188d7d10ee612220 to your computer and use it in GitHub Desktop.
Save TheRatG/e1f6f47711b16741188d7d10ee612220 to your computer and use it in GitHub Desktop.
Doctrine batch insert
// IMPORTANT - Disable the Doctrine SQL Logger
$manager->getConnection()->getConfiguration()->setSQLLogger(null);

while (($data = getData()) {
  $iteration++;

  $obj = new EntityObject();
  $obj->setName('henry');
  // Fill object...

  $manager->persist($obj);

  // IMPORTANT - Temporary store entities (of course, must be defined first outside of the loop)
  $tempObjets[] = $obj;

  if ($iteration % 500 == 0) {
    $manager->flush();

    // IMPORTANT - clean entities
    foreach($tempObjets as $tempObject) {
      $manager->detach($tempObject);
    }

    $tempObjets = null;
    gc_enable();
    gc_collect_cycles();
  }
}

source https://stackoverflow.com/questions/33427109/memory-usage-goes-wild-with-doctrine-bulk-insert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment