How to change updatedAt value without Doctrine lifecycle callback updating it just after?

97 views Asked by At

Some entities have an updatedAt field which value is well updated through prePersist and preUpdate Doctrine lifecycle callbacks.

But when setting up my project, I populate my db with AliceDataFixtures so all fixtures have the same createdAt and updatedAt values.

I was thinking about creating a Symfony command to update this values after fixtures loading in order to get more realistic data. The problem is that it solves only the createdAt issue.

Is it possible to temporary disable Doctrine lifecycle callbacks ?

Can I override its behaviour ?

Edit:

All entities use a trait defining 2 methods called by Doctrine lifecycle callbacks.

public function setCreatedAt(): self
{
    $this->createdAt = new DateTimeImmutable();

    return $this;
}

public function setUpdatedAt(): self
{
    $this->updatedAt = new DateTimeImmutable();

    return $this;
}
<lifecycle-callbacks>
    <lifecycle-callback type="prePersist" method="setCreatedAt" />
    <lifecycle-callback type="prePersist" method="setUpdatedAt" />
    <lifecycle-callback type="preUpdate" method="setUpdatedAt" />
</lifecycle-callbacks>

I donĀ“t set them in fixtures but if I try as below, the lifecycle callback still changes the value to the current date afterwards.

Model\MyEntity:
  entity_{1..30}:
    __construct:
      ...
    __calls:
      - setCustomUpdatedAt: [<pastDateTimeImmutable()>]
1

There are 1 answers

1
AymDev On BEST ANSWER

As @zedling commented, you could simply not call the setUpdatedAt() method during prePersist.

But if you still want $updatedAt to be set automatically on prePersist and be able to override it in your fixtures, I'd change the setUpdatedAt method by

public function setUpdatedAt(): self
{
    // I assumed that your entity has an auto-generated identifier called `$id`
    if (null !== $this->id || null === $this->updatedAt) {
        $this->updatedAt = new DateTimeImmutable();
    }

    return $this;
}

If it's an update, the $id won't be null and $updatedAt will be updated. If it's an insert (persist), then it will be updated only if it has no value yet. That should let you set it on the fixtures you want.

Here is a demo link: https://3v4l.org/F9dVj