ecommit/doctrine-orm-refetch

重新获取 ORM Doctrine 对象。或者从快照中分离所有附加的实体

v1.1.1 2024-06-21 19:40 UTC

This package is auto-updated.

Last update: 2024-09-21 20:15:42 UTC


README

此库允许

  • 在清除对象管理器后重新获取 Doctrine ORM 对象
  • 从快照中分离所有附加的实体

Tests

安装

要使用 Composer 安装 doctrine-orm-refetch,只需运行

$ composer require ecommit/doctrine-orm-refetch

使用方法

创建实用工具($entityManager 是 Doctrine ORM 实体管理器)

use Ecommit\DoctrineOrmRefetch\RefetchManager;

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

重新获取对象

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

示例

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();

根据标准获取集合

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

示例

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();

快照

从快照中分离所有附加的实体(快照之前附加的实体保留)

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();

许可证

此库采用 MIT 许可证。请参阅 LICENSE 文件中的完整许可证。