https://github.com/e-commit/doctrine-orm-refetch
This library allows to re-fetch Doctrine ORM objects after clear the object manager. Or detach all entities attached since a snapshot.
https://github.com/e-commit/doctrine-orm-refetch
doctrine php refetch snapshot
Last synced: 10 months ago
JSON representation
This library allows to re-fetch Doctrine ORM objects after clear the object manager. Or detach all entities attached since a snapshot.
- Host: GitHub
- URL: https://github.com/e-commit/doctrine-orm-refetch
- Owner: e-commit
- License: mit
- Created: 2020-02-11T13:24:55.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-15T16:11:51.000Z (10 months ago)
- Last Synced: 2025-03-15T17:23:00.682Z (10 months ago)
- Topics: doctrine, php, refetch, snapshot
- Language: PHP
- Homepage:
- Size: 65.4 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Doctrine ORM Refetch
This library allows to
* re-fetch Doctrine ORM objects after clear the object manager
* detach all entities attached since a snapshot

## Installation ##
To install doctrine-orm-refetch with Composer just run :
```bash
$ composer require ecommit/doctrine-orm-refetch
```
## Usage ##
Create the utility (`$entityManager` is the Doctrine ORM entity manager):
```php
use Ecommit\DoctrineOrmRefetch\RefetchManager;
$refetchManager = RefetchManager::create($entityManager);
```
### Refetch an object ###
```php
$myObject = $refetchManager->getFetchedObject($myObject);
//or $refetchManager->refreshObject($myObject);
```
Example:
```php
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 ###
```php
$collection = $refetchManager->getCollectionFromCriteria($criteria, 'MyClass');
```
Example:
```php
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 ###
Detach all entities attached since a snapshot (entities attached before the snapshot are kept)
```php
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 ##
This librairy is under the MIT license. See the complete license in *LICENSE* file.