Skip to content

Unit of work doesn't properly track changes when using native lazy object #12403

Description

@r0drigo-arg

Bug Report

Q A
Version 3.6.2

Summary

I'm working on a project running PHP 8.4, Symfony 7.4 and Doctrine 3.6.2. I have two entities connected through a OneToOne relationship defined as:

class EntityA{
    #[ORM\OneToOne(inversedBy: 'entityA', targetEntity: EntityB::class, cascade: ['persist', 'remove'])]
    private $entityB;
}

class EntityB {
    #[ORM\OneToOne(mappedBy: 'entityB', targetEntity: EntityA::class, cascade: ['persist', 'remove'])]
    private $entityA;
}

Both setters are properly implemented, i.e., setting previous properties to null and updating new properties with the correct relationship. I have enabled enable_native_lazy_objects, replacing the existing enable_lazy_ghost_objects. Now when updating EntityA with a new EntityB, the relationship is not saved.
If I detach the entity before flushing, fetch eager entityB or set enable_lazy_ghost_objects this problem doesn't appear. From debugging it seems that UnitOfWork can't properly detect this change because the old entity is marked as lazy

Current behavior

Updating entityB on entityA which already has an entityB defined keeps the old relationship instead of saving the new one.

Expected behavior

That when changing the relationship, the changes are saved

How to reproduce

Creating two classes with a relationship defined like above, creating an entityA with and entityB, updating the entityA with a new entityB and flushing it will cause the change to not be saved

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions