Skip to main content Link Search Menu Expand Document (external link)

Timestamped

To set an entity component as timestamped, use the following annotation and trait:

use Silverback\ApiComponentsBundle\Annotation as Silverback;
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;

 #[Silverback\Timestamped]
class Foo
{
    use TimestampedTrait;

Default field for the created timestamp iscreatedAt and the modified timestamp modifiedAt. To customize these, update the annotation and your class:

use Silverback\ApiComponentsBundle\Annotation as Silverback;

 #[Silverback\Timestamped(createdAtField: 'customCreatedAt', modifiedAtField: 'customModifiedAt')]
class Foo
{
    // If not set, the Doctrine mapping is automatically configured with type="datetime_immutable" not nullable
    private ?DateTimeImmutable $customCreatedAt;

    // If not set, the Doctrine mapping is automatically configured with type="datetime" not nullable
    private ?DateTime $customModifiedAt;

    public function setCustomCreatedAt(DateTimeImmutable $customCreatedAt): self
    {
        if (!$this->customCreatedAt) {
            $this->customCreatedAt = $customCreatedAt;
        }

        return $this;
    }

    public function getCustomCreatedAt(): ?DateTimeImmutable
    {
        return $this->customCreatedAt;
    }

    public function setCustomModifiedAt(DateTime $customModifiedAt): self
    {
        $this->customModifiedAt = $customModifiedAt;

        return $this;
    }

    public function getCustomModifiedAt(): ?DateTime
    {
        return $this->customModifiedAt;
    }

Copyright © 2018-2020 Silverback IS. Distributed by an MIT license.