PHP code example of ecommit / doctrine-orm-refetch
1. Go to this page and download the library: Download ecommit/doctrine-orm-refetch library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ecommit / doctrine-orm-refetch example snippets
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();