PHPackages                             ocramius/doctrine-batch-utils - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. ocramius/doctrine-batch-utils

ActiveLibrary[Database &amp; ORM](/categories/database)

ocramius/doctrine-batch-utils
=============================

A set of utilities to operate with Doctrine ORM's batch processing functionality

2.12.0(5mo ago)3331.6M↓16.9%15[2 PRs](https://github.com/Ocramius/DoctrineBatchUtils/pulls)6MITPHPPHP ~8.3.0 || ~8.4.0 || ~8.5.0CI passing

Since Oct 9Pushed 2mo ago6 watchersCompare

[ Source](https://github.com/Ocramius/DoctrineBatchUtils)[ Packagist](https://packagist.org/packages/ocramius/doctrine-batch-utils)[ Docs](https://github.com/Ocramius/DoctrineBatchUtils)[ GitHub Sponsors](https://github.com/Ocramius)[ RSS](/packages/ocramius-doctrine-batch-utils/feed)WikiDiscussions 2.13.x Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (37)Used By (6)

DoctrineBatchUtils
==================

[](#doctrinebatchutils)

This repository attempts to ease the pain of dealing with [batch-processing](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/batch-processing.html)in the context of [Doctrine ORM](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/)transactions.

This repository is maintained by [Patrick Reimers (PReimers)](https://github.com/PReimers).

[![License](https://camo.githubusercontent.com/ddddcf7a634d10843b446e943f278f05dae1daca26ad72f48642633f58e0efb8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f6372616d6975732f646f637472696e652d62617463682d7574696c732e737667)](https://github.com/Ocramius/DoctrineBatchUtils/blob/master/LICENSE)[![Current release](https://camo.githubusercontent.com/83ecc21ead11dbf74e7c478e2dd65ebe3f8ef6c798c8511198df9a58bf5c850d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6372616d6975732f646f637472696e652d62617463682d7574696c732e737667)](https://packagist.org/packages/ocramius/doctrine-batch-utils)[![Build Status](https://github.com/Ocramius/DoctrineBatchUtils/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/Ocramius/DoctrineBatchUtils/actions/workflows/continuous-integration.yml)

Installation
------------

[](#installation)

Supported installation method is via [Composer](http://getcomposer.org/):

```
composer require ocramius/doctrine-batch-utils
```

Current features
----------------

[](#current-features)

As it stands, the only implemented utility in this repository is an [`IteratorAggregate`](http://php.net/manual/en/class.iteratoraggregate.php) that wraps around a DB transaction and calls [`ObjectManager#flush()`](https://github.com/doctrine/common/blob/v2.5.1/lib/Doctrine/Common/Persistence/ObjectManager.php#L120)and [`ObjectManager#clear()`](https://github.com/doctrine/common/blob/v2.5.1/lib/Doctrine/Common/Persistence/ObjectManager.php#L88)on the given [`EntityManager`](https://github.com/doctrine/doctrine2/blob/v2.5.1/lib/Doctrine/ORM/EntityManagerInterface.php).

### Example (array iteration)

[](#example-array-iteration)

It can be used as following:

```
use DoctrineBatchUtils\BatchProcessing\SimpleBatchIteratorAggregate;

$object1  = $entityManager->find('Foo', 1);
$object2  = $entityManager->find('Bar', 2);

$iterable = SimpleBatchIteratorAggregate::fromArrayResult(
    [$object1, $object2], // items to iterate
    $entityManager,       // the entity manager to operate on
    100                   // items to traverse before flushing/clearing
);

foreach ($iterable as $record) {
    // operate on records here
}
```

#### `$record` freshness

[](#record-freshness)

Please note that the `$record` inside the loop will always be "fresh" ([`managed`](http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#persisting-entities) state), as the iterator re-fetches it on its own: this prevents you from having to manually call [`ObjectManager#find()`](https://github.com/doctrine/common/blob/v2.5.1/lib/Doctrine/Common/Persistence/ObjectManager.php#L42)on your own for every iteration.

#### Automatic flushing/clearing

[](#automatic-flushingclearing)

In this example, the `EntityManager` will be flushed and cleared only once, but if there were more than 100 records, then it would flush (and clear) twice or more.

### Example (query/iterators)

[](#example-queryiterators)

The previous example is still not memory efficient, as we are operating on a pre-loaded array of objects loaded by the ORM.

We can use queries instead:

```
use DoctrineBatchUtils\BatchProcessing\SimpleBatchIteratorAggregate;

$iterable = SimpleBatchIteratorAggregate::fromQuery(
    $entityManager->createQuery('SELECT f FROM Files f'),
    100 // flush/clear after 100 iterations
);

foreach ($iterable as $record) {
    // operate on records here
}
```

Or our own iterator/generator:

```
use DoctrineBatchUtils\BatchProcessing\SimpleBatchIteratorAggregate;

// This is where you'd persist/create/load your entities (a lot of them!)
$results = function () {
    for ($i = 0; $i < 100000000; $i += 1) {
        yield new MyEntity($i); // note: identifier must exist in the DB
    }
};

$iterable = SimpleBatchIteratorAggregate::fromTraversableResult(
    $results(),
    $entityManager,
    100 // flush/clear after 100 iterations
);

foreach ($iterable as $record) {
    // operate on records here
}

// eventually after all records have been processed, the assembled transaction will be committed to the database
```

Both of these approaches are much more memory efficient.

###  Health Score

70

—

ExcellentBetter than 100% of packages

Maintenance81

Actively maintained with recent releases

Popularity58

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity92

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~127 days

Recently: every ~103 days

Total

31

Last Release

57d ago

Major Versions

1.1.x-dev → 2.0.02019-03-11

PHP version history (10 changes)1.0.0PHP ~5.5|~7.0

1.1.0PHP ^7.1.0

2.0.0PHP ^7.3.0

2.2.0PHP ^7.4.0|| ^8.0

2.3.x-devPHP ^7.4.0 || ^8.0

2.4.x-devPHP ^7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0

2.5.x-devPHP ~8.1.0 || ~8.2.0

2.6.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0

2.9.0PHP ~8.2.0 || ~8.3.0 || ~8.4.0

2.12.x-devPHP ~8.3.0 || ~8.4.0 || ~8.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/fd0445bc21fa116c259b5889377b90cbd8a34d49357321f76a74f6d2c2ae6b0c?d=identicon)[Ocramius](/maintainers/Ocramius)

---

Top Contributors

[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (300 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (226 commits)")[![Ocramius](https://avatars.githubusercontent.com/u/154256?v=4)](https://github.com/Ocramius "Ocramius (141 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (118 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (117 commits)")[![PReimers](https://avatars.githubusercontent.com/u/1785288?v=4)](https://github.com/PReimers "PReimers (54 commits)")[![azjezz](https://avatars.githubusercontent.com/u/29315886?v=4)](https://github.com/azjezz "azjezz (9 commits)")[![fezfez](https://avatars.githubusercontent.com/u/1162307?v=4)](https://github.com/fezfez "fezfez (7 commits)")[![IndraGunawan](https://avatars.githubusercontent.com/u/2704730?v=4)](https://github.com/IndraGunawan "IndraGunawan (6 commits)")[![holtkamp](https://avatars.githubusercontent.com/u/776405?v=4)](https://github.com/holtkamp "holtkamp (6 commits)")[![jdecool](https://avatars.githubusercontent.com/u/433926?v=4)](https://github.com/jdecool "jdecool (3 commits)")[![jmleroux](https://avatars.githubusercontent.com/u/1516770?v=4)](https://github.com/jmleroux "jmleroux (2 commits)")[![gsdevme](https://avatars.githubusercontent.com/u/319498?v=4)](https://github.com/gsdevme "gsdevme (2 commits)")[![Majkl578](https://avatars.githubusercontent.com/u/144181?v=4)](https://github.com/Majkl578 "Majkl578 (1 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (1 commits)")[![simPod](https://avatars.githubusercontent.com/u/327717?v=4)](https://github.com/simPod "simPod (1 commits)")

---

Tags

ormdoctrinedoctrine2batch processing

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ocramius-doctrine-batch-utils/health.svg)

```
[![Health](https://phpackages.com/badges/ocramius-doctrine-batch-utils/health.svg)](https://phpackages.com/packages/ocramius-doctrine-batch-utils)
```

###  Alternatives

[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58723.9M36](/packages/scienta-doctrine-json-functions)[sonata-project/doctrine-orm-admin-bundle

Integrate Doctrine ORM into the SonataAdminBundle

46117.7M155](/packages/sonata-project-doctrine-orm-admin-bundle)[doctrine/doctrine-orm-module

Laminas Module that provides Doctrine ORM functionality

4407.3M293](/packages/doctrine-doctrine-orm-module)[hautelook/alice-bundle

Symfony bundle to manage fixtures with Alice and Faker.

19519.4M34](/packages/hautelook-alice-bundle)[weirdan/doctrine-psalm-plugin

Stubs to let Psalm understand Doctrine better

876.9M50](/packages/weirdan-doctrine-psalm-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
