feat(unique): add #[Unique] attribute for single-field and composite unique constraints#143
Merged
clementtalleu merged 1 commit intoMay 7, 2026
Conversation
…unique constraints
Enforce uniqueness at flush() time using Redis WATCH + MULTI/EXEC, for both
single-field (property-level) and composite (class-level, IS_REPEATABLE) constraints.
Implementation:
- New #[Unique] attribute (TARGET_PROPERTY | TARGET_CLASS | IS_REPEATABLE)
- New UniqueConstraintViolationException with forField(), forFields(),
concurrentModification() factory methods
- RedisObjectManager.flush() split into a WATCH path (when unique constraints
are present) and the original path (no constraints)
- buildUniqueConstraintPlan() collects unique keys to WATCH, GET-check,
SET, and DEL across all pending operations (persist, merge, delete)
- Intra-flush collision detection: duplicate values in the same flush() call
are caught before touching Redis
- ObjectToPersist.previousValues stores the pre-merge snapshot so flush()
can resolve which unique key to delete when a unique field changes
- AbstractPersister.delete() now passes value: $object (was null, caused
ReflectionClass crash when building the unique key on delete)
- RedisClientInterface extended with get(), set(), watch(), unwatch();
exec() return type changed from void to bool (false = WATCH triggered)
- Both RedisClient (ext-redis) and PredisClient implementations updated
Unique key format:
unique:{className}:{field}:{value} (single-field)
unique:{className}:{f1,f2}:{v1}:{v2} (composite, fields sorted)
Tests:
- 17 functional tests (UniqueConstraintTest): persist/same-flush/merge/remove
scenarios for both Hash and Json formats, single-field and composite
- 6 unit tests (UniqueConstraintViolationExceptionTest)
Docs:
- docs/mapping.md: new "Unique constraints" section (single-field, composite,
merge/remove behavior, concurrency guarantee, limitations)
- docs/advanced_usage.md: exception handling snippet with link to mapping.md
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #143 +/- ##
============================================
- Coverage 88.10% 87.43% -0.67%
- Complexity 900 949 +49
============================================
Files 68 70 +2
Lines 2076 2221 +145
============================================
+ Hits 1829 1942 +113
- Misses 247 279 +32 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
clementtalleu
approved these changes
May 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ref: #142
Enforce uniqueness at flush() time using Redis WATCH + MULTI/EXEC, for both single-field (property-level) and composite (class-level, IS_REPEATABLE) constraints.
Implementation:
Unique key format:
unique:{className}:{field}:{value} (single-field)
unique:{className}:{f1,f2}:{v1}:{v2} (composite, fields sorted)
Tests:
Docs: