PHPackages                             ecommit/doctrine-orm-refetch - 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. ecommit/doctrine-orm-refetch

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

ecommit/doctrine-orm-refetch
============================

Refetch ORM Doctrine objects. Or detach all entities attached since a snapshot

v1.1.2(3mo ago)13.5k↓50%1MITPHPPHP ^8.1CI passing

Since Feb 2Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/e-commit/doctrine-orm-refetch)[ Packagist](https://packagist.org/packages/ecommit/doctrine-orm-refetch)[ RSS](/packages/ecommit-doctrine-orm-refetch/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (10)Versions (7)Used By (0)

Doctrine ORM Refetch
====================

[](#doctrine-orm-refetch)

This library allows to

- re-fetch Doctrine ORM objects after clear the object manager
- detach all entities attached since a snapshot

[![Tests](https://github.com/e-commit/doctrine-orm-refetch/workflows/Tests/badge.svg)](https://github.com/e-commit/doctrine-orm-refetch/workflows/Tests/badge.svg)

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

[](#installation)

To install doctrine-orm-refetch with Composer just run :

```
$ composer require ecommit/doctrine-orm-refetch
```

Usage
-----

[](#usage)

Create the utility (`$entityManager` is the Doctrine ORM entity manager):

```
use Ecommit\DoctrineOrmRefetch\RefetchManager;

$refetchManager = RefetchManager::create($entityManager);
```

### Refetch an object

[](#refetch-an-object)

```
$myObject = $refetchManager->getFetchedObject($myObject);
//or $refetchManager->refreshObject($myObject);
```

Example:

```
use Ecommit\DoctrineOrmRefetch\RefetchManager;

$refetchManager = RefetchManager::create($entityManager);

$author = $entityManager->getRepository(Author::class)->find(1);

$queryBuilder = $entityManager->getRepository(Book::class)->createQueryBuilder('b');
$queryBuilder->select('b')
    ->andWhere('b.bookId != :bookId')
    ->setParameter('bookId', 7);
$iterableResult = $queryBuilder->getQuery()->iterate();

$i = 0;
foreach ($iterableResult as $row) {
    ++$i;
    $book = current($row);

    if (!$book->getAuthors()->contains($author)) {
        $book->addAuthor($author);
    }

    if (0 === $i % 20) {
        //$author is managed
        $entityManager->flush();
        $entityManager->clear();
        //$author is not managed
        $author = $refetchManager->getObject($author);
        //$author is managed
    }
}

$entityManager->flush();
$entityManager->clear();
```

### Get collection by critera

[](#get-collection-by-critera)

```
$collection = $refetchManager->getCollectionFromCriteria($criteria, 'MyClass');
```

Example:

```
use Doctrine\Common\Collections\Criteria;
use Ecommit\DoctrineOrmRefetch\RefetchManager;

$refetchManager = RefetchManager::create($entityManager);

$ctiteria = Criteria::create()
    ->andWhere(Criteria::expr()->gt('authorId', 2));
$authors = $refetchManager->getCollectionFromCriteria($ctiteria, Author::class);

$queryBuilder = $entityManager->getRepository(Book::class)->createQueryBuilder('b');
$queryBuilder->select('b')
    ->andWhere('b.bookId != :bookId')
    ->setParameter('bookId', 9);
$iterableResult = $queryBuilder->getQuery()->iterate();

$i = 0;
foreach ($iterableResult as $row) {
    ++$i;
    $book = current($row);

    foreach ($authors as $author) {
        if (!$book->getAuthors()->contains($author)) {
            $book->addAuthor($author);
        }
    }

    if (0 === $i % 20) {
        $entityManager->flush();
        $entityManager->clear();
        $authors = $refetchManager->getCollectionFromCriteria($ctiteria, Author::class);
    }
}

$entityManager->flush();
$entityManager->clear();
```

### Snapshot

[](#snapshot)

Detach all entities attached since a snapshot (entities attached before the snapshot are kept)

```
use Ecommit\DoctrineOrmRefetch\SnapshotManager;

$snapshotManager = SnapshotManager::create($entityManager);

$author = $entityManager->getRepository(Author::class)->find(1);

$snapshotManager->snapshot();

$queryBuilder = $entityManager->getRepository(Book::class)->createQueryBuilder('b');
$queryBuilder->select('b')
    ->andWhere('b.bookId != :bookId')
    ->setParameter('bookId', 7);
$iterableResult = $queryBuilder->getQuery()->iterate();

$i = 0;
foreach ($iterableResult as $row) {
    ++$i;
    /** @var Book $book */
    $book = current($row);

    if (!$book->getAuthors()->contains($author)) {
        $book->addAuthor($author);
    }

    if (0 === $i % 2) {
        // $author and $book are managed
        $entityManager->flush();
        $snapshotManager->clear(); // Detach all entities attached since the snapshot
        // Only $author is managed
    }
}

$entityManager->flush();
$snapshotManager->clear();
```

License
-------

[](#license)

This librairy is under the MIT license. See the complete license in *LICENSE* file.

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance83

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~367 days

Recently: every ~355 days

Total

6

Last Release

91d ago

Major Versions

v0.2.0 → v1.0.02022-07-24

PHP version history (2 changes)v0.1.0PHP ^7.2|^8.0

v1.1.1PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/73191162d97fffaa1a23d23326a7a77fbaa94ce41e49cf4ee6cbf0b9c9800c80?d=identicon)[e-commit](/maintainers/e-commit)

---

Top Contributors

[![hlecorche](https://avatars.githubusercontent.com/u/188749?v=4)](https://github.com/hlecorche "hlecorche (46 commits)")

---

Tags

doctrinephprefetchsnapshotsnapshotdoctrinerefetch

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ecommit-doctrine-orm-refetch/health.svg)

```
[![Health](https://phpackages.com/badges/ecommit-doctrine-orm-refetch/health.svg)](https://phpackages.com/packages/ecommit-doctrine-orm-refetch)
```

###  Alternatives

[knplabs/doctrine-behaviors

Doctrine Behavior Traits

92212.7M64](/packages/knplabs-doctrine-behaviors)[scienta/doctrine-json-functions

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

58723.9M36](/packages/scienta-doctrine-json-functions)[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[fresh/doctrine-enum-bundle

Provides support of ENUM type for Doctrine2 in Symfony applications.

4636.8M12](/packages/fresh-doctrine-enum-bundle)[damienharper/auditor-bundle

Integrate auditor library in your Symfony projects.

4542.8M](/packages/damienharper-auditor-bundle)[ergebnis/factory-bot

Provides a fixture factory for doctrine/orm entities.

81702.8k](/packages/ergebnis-factory-bot)

PHPackages © 2026

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