-
-
Notifications
You must be signed in to change notification settings - Fork 956
Description
API Platform version(s) affected: ^3.4 ^4.2 maybe all versions
Description
Even when setting skip_null_values to false, i've found a weird case where trying to patch null to a ManyToOne value does not work.
I'll do my best to explain my findings. So let's start with this entity
#[ORM\Entity]
#[ORM\Table(name: 'foo')]
#[UniqueEntity(fields: 'name')]
class Foo
{
#[ORM\Column(name: 'id_foo', type: 'integer')]
#[Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;
#[ORM\Column(name: 'name', type: 'text', nullable: false)]
private ?string $name = null;
#[ORM\ManyToOne(targetEntity: Bar::class, cascade: ['persist']]
#[ORM\JoinColumn(name: 'id_bar', referencedColumnName: 'id_bar')]
private ?Bar $bar = null;
...Patching a Foo with an existing or a new Bar works fine. But setting it to null does not work.
The deserialized Foo has a null Bar, but during the Validation (after the PRE_VALIDATION and before POST_VALIDATE hooks), during the execution of the UniqueEntityValidator the Foo::$bar is reset to its initial value.
I can reproduce the same issue with a custom validator containing a
$foo = $this->em->getRepository(Foo::class)->findOneByName($value->getName())I guess it has to do with some doctrine internals like proxy or stuff like that, but i have no proof.
When i have time, i'll make a minimal project.
Possible Solution
I've worked around with a custom validator and a native query.