PHP code example of ang3 / doctrine-orm-batch-process
1. Go to this page and download the library: Download ang3/doctrine-orm-batch-process 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/ */
ang3 / doctrine-orm-batch-process example snippets
use Ang3\Doctrine\ORM\Batch\BatchProcess;
$myProcess = new BatchProcess($entityManager, $myIterator, $myHandler = null, $bufferSize = 20);
// For chaining calls
$myProcess = BatchProcess::create($entityManager, $myIterator);
/*
* SHORTCUT METHODS FOR BUILT-IN ITERATORS
*/
// Iterate from identified entities
$myProcess = BatchProcess::iterateEntities($entityManager, $entityFqcn, array $identifiers);
// Iterate from an ORM query or query builder instance
$myProcess = BatchProcess::iterateQueryResult($query);
// Iterate from iterable or callable
$myProcess = BatchProcess::iterateData($entityManager, iterable|\Closure $data);
$nbIterations = $myProcess->execute();
use Ang3\Doctrine\ORM\Batch\Handler\PersistEntityHandler;
$myHandler = PersistEntityHandler::new()
// Handler options...
->skipInsertions() // Ignore new entities (without ID)
->skipUpdates() // Ignore stored entities (with ID)
->onPrePersist($myCallable) // This callable is called BEFORE each persist.
->onPostPersist($myCallable) // This callable is called AFTER each persist.
;
use Ang3\Doctrine\ORM\Batch\Handler\RemoveEntityHandler;
$myHandler = RemoveEntityHandler::new()
// Handler options...
->onPreRemove($myCallable) // This callable is called BEFORE each removing.
->onPostRemove($myCallable) // This callable is called AFTER each removing.
;
use Ang3\Doctrine\ORM\Batch\Handler\CallableHandler;
$myHandler = CallableHandler::new($myCallable);
use Ang3\Doctrine\ORM\Batch\Handler\ChainHandler;
$myHandler = ChainHandler::new()
// Handler options...
->append($myHandler1) // Add a handler at the end of the chain
->prepend($myHandler2) // Add a handler at the beginning of the chain
->clear() // Remove all handlers from the chain
;
namespace App\Doctrine\Batch\Handler;
use Ang3\Doctrine\ORM\Batch\Handler\BatchHandlerInterface;
final class MyHandler implements BatchHandlerInterface
{
public function __invoke(Iteration $iteration): void
{
// Data from the process iterator
$data = $iteration->getData();
// You can retrieve the entity manager from the iteration.
$em = $iteration->getEntityManager();
}
}
// ...
final class MyHandler implements BatchHandlerInterface
{
// If you want to add options:
use BatchHandlerTrait;
// Internal constants used to define options
private const OPTION_ENABLED = 'enabled';
public function __invoke(Iteration $iteration): void
{
// Retrieves option values
$enabled = $this->getOption(self::OPTION_ENABLED);
// your logic with the option...
}
// Return the handler to allow chaining calls
public function enableOption(bool $enabled = true): self
{
$this->setOption(self::OPTION_ENABLED, $enabled);
return $this;
}
}
$myProcess->restoreIdGenerator(MyClass1::class, MyClass2::class/*, ...*/);
// ... Or restore all directly
$myProcess->restoreAllIdGenerators();
$myLoadedEntity; // attached
$myProcess->execute();
dump($myLoadedEntity); // Probably detached - If you persist, a new entity could be created!
$myLoadedEntity; // attached
$myProcess
->addTransactionalEntity($myLoadedEntity) // This variable is passed by reference
->execute();
dump($myLoadedEntity); // reloaded by the process on each flush/clear
$myProcess->onFlush($myCallable);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.