Coder Social home page Coder Social logo

e-commit / doctrine-orm-refetch Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 55 KB

This library allows to re-fetch Doctrine ORM objects after clear the object manager. Or detach all entities attached since a snapshot.

License: MIT License

PHP 100.00%
refetch doctrine php snapshot

doctrine-orm-refetch's Introduction

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

Installation

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

$ composer require ecommit/doctrine-orm-refetch

Usage

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

use Ecommit\DoctrineOrmRefetch\RefetchManager;

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

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

$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

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

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

doctrine-orm-refetch's People

Contributors

hlecorche avatar

Watchers

 avatar  avatar

Forkers

hlecorche

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.