fix(doctrine): do not replace new relations with assigned ids by refe… - #8498
Open
dylan-rumble wants to merge 1 commit into
Open
fix(doctrine): do not replace new relations with assigned ids by refe…#8498dylan-rumble wants to merge 1 commit into
dylan-rumble wants to merge 1 commit into
Conversation
dylan-rumble
marked this pull request as ready for review
September 2, 2026 14:26
…rences PersistProcessor::handleLazyObjectRelations() swapped any unmanaged related object holding a non-null identifier with a Doctrine reference, assuming the row already exists. With application-assigned identifiers (e.g. UUIDs), a freshly mapped nested entity was therefore never cascade-persisted, causing a foreign key violation on flush. Consult the unit of work state (STATE_NEW) before creating a reference so that new objects are left to cascade persist, while objects with database-generated ids or existing rows are still replaced by references. Fixes api-platform#8438
dylan-rumble
force-pushed
the
fix/doctrine-persist-cascade-assigned-id
branch
from
September 2, 2026 15:03
2177712 to
0e5f21f
Compare
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.
Problem
When using the ObjectMapper (
stateOptions+ input DTO),PersistProcessor::handleLazyObjectRelations()replaces every unmanaged related object holding a non-null identifier with agetReference()proxy, assuming "has an id" means "row exists in the database".That assumption only holds for
#[ORM\GeneratedValue]entities (id isnullpre-flush). With application-assigned identifiers (e.g. UUIDs), a freshly mapped nested entity is indistinguishable from a reference to an existing row: it gets swapped for a proxy, the proxy counts as managed,cascade: ['persist']never fires, and the flush fails with a foreign key violation (or, on SQLite, the entity is "not found" on refresh).Fix
Before creating a reference, ask the unit of work whether the object is genuinely new (
UnitOfWork::getEntityState()for ORM,getDocumentState()for ODM) and skip it if it isSTATE_NEW. Doctrine's own semantics give the right behavior for each case:STATE_DETACHEDwithout any lookup → still replaced by a reference (the [Docs/DX] Clarify and simplify DTO-to-Entity relationship mapping with stateOptions and ObjectMapper #7689 IRI case is unchanged).STATE_DETACHED→ replaced by a reference.STATE_NEW→ left alone so cascade persist inserts it.Tests
New
Issue8438*fixtures (owningTaskRulewithcascade: ['persist']toTaskRuleCondition, both keyed by application-assigned UUIDs) and two functional tests inStateOptionTest:testPostWithEntityClassOptionCascadePersistsNewRelationWithAssignedIdentifier— nestedconditionpayload with a pre-assigned UUID. Failed before this fix, passes now.testPostWithEntityClassOptionReferencesExistingRelationWithAssignedIdentifier—conditiongiven as an IRI to an existing UUID-keyed row still resolves to a reference (no duplicate insert).Note on the fixtures
The issue's example assigns the UUID in the entity constructor. With symfony/object-mapper ≥ 8.1, nested mapped targets are lazy ghosts whose initializer calls
$mapper->map($value, $target)when the mapper is decorated (API Platform decorates it viaapi_platform.object_mapper.relation). That path never passesconstructTarget, so the nested entity's constructor is never executed and a constructor-assigned id is never set. This cannot be detected downstream (isUninitializedLazyObject()is alreadyfalseinside the initializer) and is a separate upstream object-mapper concern. The fixtures therefore assign the UUID in the DTO constructor, which exercises the exactPersistProcessorheuristic this PR fixes; the entity constructors still assign a UUID as in the issue.