UnitOfWorkReset
in package
The one place where application code may clear the unit of work or detach entities.
Every method is named after why the caller drops managed state, so the operation log can tell
the reasons apart (see DoctrineOperationParticipant) and a later end-of-operation clear can
replace them one by one. Each method does exactly what the call sites did before (the same
clear() / detach() at the same moment); the only addition is a scalar note to the observer.
- abort(): a rejected save or a rollback whose half-built graph must not be flushed later;
- finishBatch(): a long run finished one batch and drops everything it loaded;
- resetBatch(): an owned phase after raw DBAL writes starts over with a fresh identity map;
- discard(): detach the given entities after a rollback (targeted, the rest stays managed);
- recoverAfterFailure() / reopenIfClosed(): infrastructure after a failed flush;
- beginOperation() / endOperation(): the boundaries of a root operation in
scopedmode, called only by DoctrineOperationParticipant after its own guards.
The read clear (GetAllTrait::clearDoctrine()) and the Doctrine infrastructure
(TransactionManager, EntityManagerReopener, OrmEntityHydrator without this service) stay
outside on purpose; UnitOfWorkResetBoundaryTest forbids every other clear/detach in app/.
The observer is set only with params.1.operations.observe on. Without it nothing but the
clear/detach happens. A failing observer never changes the outcome: the reset already ran.
Table of Contents
Constants
- KIND_ABORT : string = 'abort'
- KIND_BATCH : string = 'batch'
- KIND_DETACH : string = 'detach'
- KIND_OPERATION : string = 'operation'
- KIND_RECOVER : string = 'recover'
- KIND_RESET : string = 'reset'
- REASON_MAX_LENGTH : int = 64
Properties
- $entityManager : EntityManagerInterface
- $observer : UnitOfWorkResetObserver|null
Methods
- __construct() : mixed
- abort() : void
- A rejected save or rollback: drops the whole (possibly half-mutated) managed graph.
- beginOperation() : void
- `scoped` mode: a root operation starts with an empty identity map, so nothing loaded before it (startup reads, the previous work of the calling presenter) stays managed. The caller checks first that no work is pending and no transaction runs.
- discard() : void
- Detaches the given entities after a rollback (ids that are not in the DB) so a later flush in the same EM does not try to write them. A closed EM or an unmanaged entity is skipped.
- endOperation() : void
- `scoped` mode: a root operation ended; what it loaded must not leak into the next one. The caller checks first that the EM is open and no transaction runs.
- finishBatch() : void
- A batch of a long run is done: nothing loaded in it survives into the next one.
- recoverAfterFailure() : void
- After a failed save/flush that the caller handles itself: clears an open EM, or reopens a closed one (which clears it too). Never throws; a failure is logged to `$logChannel`.
- reopenIfClosed() : bool
- Reopens the EM only when a failed flush closed it (reopening clears it); an open EM is left untouched.
- resetBatch() : void
- An owned phase starts over after raw DBAL writes; references must be taken after this call.
- normalizeReason() : string
- Bounded, log-safe reason: `[a-z0-9._-]`, at most 64 chars, `unspecified` when empty.
- notify() : void
Constants
KIND_ABORT
public
string
KIND_ABORT
= 'abort'
KIND_BATCH
public
string
KIND_BATCH
= 'batch'
KIND_DETACH
public
string
KIND_DETACH
= 'detach'
KIND_OPERATION
public
string
KIND_OPERATION
= 'operation'
KIND_RECOVER
public
string
KIND_RECOVER
= 'recover'
KIND_RESET
public
string
KIND_RESET
= 'reset'
REASON_MAX_LENGTH
private
int
REASON_MAX_LENGTH
= 64
Properties
$entityManager read-only
private
EntityManagerInterface
$entityManager
$observer read-only
private
UnitOfWorkResetObserver|null
$observer
= null
Methods
__construct()
public
__construct(EntityManagerInterface $entityManager[, UnitOfWorkResetObserver|null $observer = null ]) : mixed
Parameters
- $entityManager : EntityManagerInterface
- $observer : UnitOfWorkResetObserver|null = null
abort()
A rejected save or rollback: drops the whole (possibly half-mutated) managed graph.
public
abort(string $reason) : void
Parameters
- $reason : string
beginOperation()
`scoped` mode: a root operation starts with an empty identity map, so nothing loaded before it (startup reads, the previous work of the calling presenter) stays managed. The caller checks first that no work is pending and no transaction runs.
public
beginOperation(string $reason) : void
Parameters
- $reason : string
discard()
Detaches the given entities after a rollback (ids that are not in the DB) so a later flush in the same EM does not try to write them. A closed EM or an unmanaged entity is skipped.
public
discard(string $reason, object ...$entities) : void
Parameters
- $reason : string
- $entities : object
endOperation()
`scoped` mode: a root operation ended; what it loaded must not leak into the next one. The caller checks first that the EM is open and no transaction runs.
public
endOperation(string $reason) : void
Parameters
- $reason : string
finishBatch()
A batch of a long run is done: nothing loaded in it survives into the next one.
public
finishBatch(string $reason) : void
Parameters
- $reason : string
recoverAfterFailure()
After a failed save/flush that the caller handles itself: clears an open EM, or reopens a closed one (which clears it too). Never throws; a failure is logged to `$logChannel`.
public
recoverAfterFailure(string $reason, string $logChannel) : void
Parameters
- $reason : string
- $logChannel : string
reopenIfClosed()
Reopens the EM only when a failed flush closed it (reopening clears it); an open EM is left untouched.
public
reopenIfClosed(string $reason, string $logChannel) : bool
Parameters
- $reason : string
- $logChannel : string
Return values
bool —true when the EM is open (again)
resetBatch()
An owned phase starts over after raw DBAL writes; references must be taken after this call.
public
resetBatch(string $reason) : void
Entities loaded before it are detached, so callers must not reuse them.
Parameters
- $reason : string
normalizeReason()
Bounded, log-safe reason: `[a-z0-9._-]`, at most 64 chars, `unspecified` when empty.
private
static normalizeReason(string $reason) : string
Parameters
- $reason : string
Return values
stringnotify()
private
notify(string $kind, string $reason, int $count) : void
Parameters
- $kind : string
- $reason : string
- $count : int