ObjectMapper: PersistProcessor::handleLazyObjectRelations() breaks cascade persist of new relations with app-assigned ids (UUID in constructor)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 55/100
調査の方向性
まず、PersistProcessor::handleLazyObjectRelations() と、issue で言及されている既存の Issue7689 の fixtures/tests を見つけます。コンストラクターで UUID が割り当てられるネストされた relation を持つ mapped POST を再現します。新しい unmanaged の関連エンティティで non-null ids を持つものが cascade persist に委ねられ、既存の行への参照が引き続き正しく動作すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
API Platform version(s) affected: 4.3.5 (also present on 4.3 and main branches)
Description
When using the ObjectMapper (stateOptions + input DTO) on a Post operation, creating a new nested related entity fails with a foreign key constraint violation when the related entity uses an application-assigned identifier (e.g. a UUID generated in the constructor).
PersistProcessor::handleLazyObjectRelations() runs for every mapped operation ($operation->canMap()) and replaces any relation value that is not managed but has non-null identifiers with $manager->getReference($class, $identifiers):
// Do not get reference for partial objects or objects with null identifiers
if (!$identifiers || \count($identifiers) !== \count(array_filter($identifiers, static fn ($v) => null !== $v))) {
continue;
}
$reflectionProperty->setValue($data, $relManager->getReference($relClass, $identifiers));
This heuristic assumes "has an id" means "row exists in the database". That holds for #[ORM\GeneratedValue] entities (id is null pre-flush, so the swap is skipped and cascade: ['persist'] works — this is the case covered by the Issue7689 fixtures where Issue7689Category has a nullable auto-increment id). But with app-assigned ids (UUIDv4/v7 assigned in the constructor, a common pattern), a freshly mapped nested entity is indistinguishable from a reference to an existing one:
- The mapper creates a new related entity; its constructor assigns a UUID.
handleLazyObjectRelations()sees an unmanaged object with a non-null identifier and swaps it for agetReference()proxy.- The proxy counts as managed, so
cascade: ['persist']on the owning side never persists it. - Flush inserts the owning entity with an FK pointing to a nonexistent row:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (FOREIGN KEY ("condition_id") REFERENCES "..." ("id"))
How to reproduce
Entities (owning side cascades persist to the relation; both use constructor-assigned UUIDs):
#[ORM\Entity]
class TaskRule
{
public function __construct(
#[ORM\ManyToOne(targetEntity: TaskRuleCondition::class, cascade: ['persist'])]
#[ORM\JoinColumn(nullable: true)]
public ?TaskRuleCondition $condition = null,
#[ORM\Id, ORM\Column]
private(set) UuidV7 $id = new UuidV7(),
) {
}
}
#[ORM\Entity]
class TaskRuleCondition
{
public function __construct(
#[ORM\Column]
public string $completeWhen,
#[ORM\Id, ORM\Column]
private(set) UuidV7 $id = new UuidV7(),
) {
}
}
Input DTOs mapped with the ObjectMapper:
#[Map(target: TaskRule::class)]
final readonly class TaskRulePostInput
{
public function __construct(
public ?TaskRuleConditionInput $condition = null,
) {
}
}
POST with a nested condition payload -> FK violation on flush. Persisting the owning entity manually in a custom processor before PersistProcessor runs works around it, because the cascade marks the relation managed and handleLazyObjectRelations() then skips it via the contains() check.
Possible Solution
Only swap values that are genuinely placeholders for existing rows, instead of assuming any unmanaged object with an id is one:
- only replace actual uninitialized lazy objects (
ReflectionClass::isUninitializedLazyObject()on PHP >= 8.4), and/or - consult
UnitOfWork::getEntityState($value)—STATE_NEWobjects should be left alone so cascade persist can handle them.
Additional Context
- Introduced/extended in cc2f8855 ("fix(doctrine): post with mapped relation", #7689) — the accompanying test fixtures only cover DB-generated ids, where new entities still have null ids pre-flush.
- Related but distinct: #7735/#7738 fixed uninitialized-property crashes in the same method; #7801 concerns nested mapping on the output side.
- 主要言語
- PHP
- スター
- 2.6k
- フォーク
- 982
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 67
環境構築
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
api-platform/core のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
api-platform/core#8588 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
api-platform/core#8573 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
api-platform/core#8564 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
api-platform/core#8495 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
api-platform/core#8471 ·
メンテナーはふだん 1 日以内に返信
api-platform/core の issue をすべて見る
似ている issue
-
Update the one-click "GTM4WP" Delay JS exclusion so that it also works with a custom data layer nameオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
[Feature]: Remove Kanboard Discourse forum link from README.md and CONTRIBUTING.md, or moderate itオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
bug Localization
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
solana-foundation/pay-kit#341 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
cline/cline#14560 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信