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()>]
As @zedling commented, you could simply not call the
setUpdatedAt()
method duringprePersist
.But if you still want
$updatedAt
to be set automatically onprePersist
and be able to override it in your fixtures, I'd change thesetUpdatedAt
method byIf it's an update, the
$id
won't benull
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