<?php class MyEntity { public ?int $id = null; public ?\DateTimeImmutable $updatedAt = null; public function setUpdatedAt(): self { if (null !== $this->id || null === $this->updatedAt) { $this->updatedAt = new DateTimeImmutable(); } return $this; } } // Insert without custom date $a = new MyEntity(); $a->setUpdatedAt(); var_dump($a); // Insert with custom date $b = new MyEntity(); $b->updatedAt = new \DateTimeImmutable('2022-09-16'); $b->setUpdatedAt(); var_dump($b); // Update $c = new MyEntity(); $c->id = 42; $c->updatedAt = new \DateTimeImmutable('2022-09-16'); $c->setUpdatedAt(); var_dump($c);
You have javascript disabled. You will not be able to edit any code.