DoctrineBatchUtils

:loop: A set of utilities to operate with Doctrine ORM's batch processing techniques

Github星跟踪图

DoctrineBatchUtils

This repository attempts to ease the pain of dealing with
batch-processing
in the context of Doctrine ORM
transactions.

This repository is maintained by Patrick Reimers (PReimers).

License
Current release
Travis-CI build status

Installation

Supported installation method is via Composer:

composer require ocramius/doctrine-batch-utils

Current features

As it stands, the only implemented utility in this repository is an
IteratorAggregate that
wraps around a DB transaction and calls
ObjectManager#flush()
and ObjectManager#clear()
on the given EntityManager.

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

Please note that the $record inside the loop will always be "fresh"
(managed state),
as the iterator re-fetches it on its own: this prevents you from having to
manually call ObjectManager#find()
on your own for every iteration.

Automatic flushing/clearing

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)

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.

主要指标

概览
名称与所有者Ocramius/DoctrineBatchUtils
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-10-09 02:29:08
推送于2025-06-21 14:40:47
最后一次提交
发布数14
最新版本名称2.11.0 (发布于 2025-02-05 12:05:35)
第一版名称1.0.0 (发布于 2015-10-15 07:48:32)
用户参与
星数334
关注者数7
派生数15
提交数0.9k
已启用问题?
问题数19
打开的问题数3
拉请求数288
打开的拉请求数4
关闭的拉请求数94
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?