Maybe someone can help me understand something I can't explain on a Symfony listener. Here is the subject:
I have an entity product, which have a fields : $creationDate and a $name.
I also have a ProductListener to execute some other action upon entity changes.
Now I want to update this product name, so in my form I simply enter the new name, click validate and the process goes on, without changing the name.
After some digging, I've found it comes from the ProductListener, at some point of my code I have :
if ($event->hasChangedField('productCreationDate')) {
$this->handleModification();
}
While I try to update the $name of the product, the ProductListener consider that the $creationDate have been changed (it did not). If I comment this condition, the update of the $name happens correctly.
So I know the issue comes from this condition, the question is why does it enter it in the first place, when the $creationDate field has not been changed.
So I searched why the listener believed the $creationDate field had been updated. I noticed the timezone of old and new values of this field were different so I updated the code as follow to make sure both were on timezone UTC:
/**
*
* @var DateTimeImmutable
* @ORM\Column(type="datetime_immutable", nullable=false)
* @Groups({"product.write", "product.read"})
* @ApiProperty(attributes={"normalization_context"={
* "datetime_format"="Y-m-d H:i:s",
* },
* })
*/
private $dateCreation;
It fixed the timezone difference between old and new value of this field.
But the problem still occurs. Through my debug I managed to see the new and old value of the field $creationDate which are both equal to :
date="2020-07-01 00:00:00.000000"
timezone_type={int}3
timezone="UTC"
date="2020-07-01 00:00:00.000000"
timezone_type={int}3
timezone="UTC"
The fact that the date is in the future is normal. But nevertheless, both of those dates are identical, I can't find why the hasChangeField method of the event object would see it as changed.
The other methods of the ProductListener does not regard the $creationDate field. I do not have other doctrine lifecycle events.
Has this happened to anyone already? Does anyone have some hints or solutions? Thanks!