Skip to content

Instantly share code, notes, and snippets.

@vikbert
Last active April 13, 2018 09:29
Show Gist options
  • Select an option

  • Save vikbert/86c4a165ad7c4a167103f5f0abb5fa92 to your computer and use it in GitHub Desktop.

Select an option

Save vikbert/86c4a165ad7c4a167103f5f0abb5fa92 to your computer and use it in GitHub Desktop.
[doctrine] doctrine snippets #doctrine

truncate or reset the tables

// sample code in test cases to reset/truncate all test tables
$connection = static::$entityManager->getConnection();
$platform = $connection->getDatabasePlatform();
foreach (self::getMetadataOfClasses() as $classMetadata) {
  $truncateSql = $platform->getTruncateTableSql($classMetadata->getTableName());
  $connection->executeUpdate($truncateSql);
}

EntityManager vs Repository

lesende Zugriffe auf die Daten durch die Repositorys zu kapseln und den Entity Manager lediglich für das Schreiben und Löschen zu verwenden.

batch processing

$batchSize = 20;
$i = 0;
$q = $em->createQuery('select u from MyProject\Model\User u');
$iterableResult = $q->iterate();
foreach ($iterableResult as $row) {
    $user = $row[0];
    $user->increaseCredit();
    $user->calculateNewBonuses();
    if (($i % $batchSize) === 0) {
        $em->flush(); // Executes all updates.
        $em->clear(); // Detaches all objects from Doctrine!
    }
    ++$i;
}
$em->flush();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment