diff --git a/src/Listener/UpdatedAt.php b/src/Listener/UpdatedAt.php index 9a438cf..626318e 100644 --- a/src/Listener/UpdatedAt.php +++ b/src/Listener/UpdatedAt.php @@ -6,12 +6,14 @@ use Cycle\ORM\Command\StoreCommandInterface; use Cycle\ORM\Entity\Behavior\Attribute\Listen; +use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate; use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnUpdate; final class UpdatedAt { public function __construct( - private string $field = 'updatedAt' + private string $field = 'updatedAt', + private bool $nullable = false ) { } @@ -22,4 +24,12 @@ public function __invoke(OnUpdate $event): void $event->command->registerAppendix($this->field, $event->timestamp); } } + + #[Listen(OnCreate::class)] + public function onCreate(OnCreate $event): void + { + if (!$this->nullable) { + $event->state->register($this->field, $event->timestamp); + } + } } diff --git a/src/UpdatedAt.php b/src/UpdatedAt.php index 0c21c2b..71b4894 100644 --- a/src/UpdatedAt.php +++ b/src/UpdatedAt.php @@ -14,9 +14,10 @@ use Doctrine\Common\Annotations\Annotation\Target; /** - * UpdatedAt behavior will automate adding an updating date to your entity. The behavior has two parameters: + * UpdatedAt behavior will automate adding an updating date to your entity. The behavior has three parameters: * - field - is a property in the entity - * - column - is a column in the database. + * - column - is a column in the database + * - nullable - if this parameter is set to false, will be set initial value when an entity is creating * Behavior requires a field with the DateTime type. * A property in an entity and a field in the database can be added in several ways: * - Can be added by a behavior automatically. @@ -29,7 +30,8 @@ * @Target({"CLASS"}) * @Attributes({ * @Attribute("field", type="string"), - * @Attribute("column", type="string") + * @Attribute("column", type="string"), + * @Attribute("nullable", type="boolean") * }) */ #[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor] @@ -39,7 +41,8 @@ final class UpdatedAt extends BaseModifier public function __construct( private string $field = 'updatedAt', - ?string $column = null + ?string $column = null, + private bool $nullable = false ) { $this->column = $column; } @@ -52,7 +55,8 @@ protected function getListenerClass(): string protected function getListenerArgs(): array { return [ - 'field' => $this->field + 'field' => $this->field, + 'nullable' => $this->nullable ]; } diff --git a/tests/Behavior/Fixtures/UpdatedAt/Post.php b/tests/Behavior/Fixtures/UpdatedAt/Post.php index f6fd76d..c78b15a 100644 --- a/tests/Behavior/Fixtures/UpdatedAt/Post.php +++ b/tests/Behavior/Fixtures/UpdatedAt/Post.php @@ -19,5 +19,6 @@ class Post #[Column(type: 'datetime', nullable: true)] public ?\DateTimeImmutable $updatedAt = null; public ?\DateTimeImmutable $customUpdatedAt = null; + public \DateTimeImmutable $notNullableUpdatedAt; public ?string $content = null; } diff --git a/tests/Behavior/Functional/Driver/Common/UpdatedAt/ListenerTest.php b/tests/Behavior/Functional/Driver/Common/UpdatedAt/ListenerTest.php index 8fb725e..cd9981d 100644 --- a/tests/Behavior/Functional/Driver/Common/UpdatedAt/ListenerTest.php +++ b/tests/Behavior/Functional/Driver/Common/UpdatedAt/ListenerTest.php @@ -27,6 +27,7 @@ public function setUp(): void 'id' => 'primary', 'updated_at' => 'datetime,nullable', 'custom_updated_at' => 'datetime,nullable', + 'not_nullable_updated_at' => 'datetime', 'content' => 'string,nullable' ] ); @@ -41,19 +42,28 @@ public function setUp(): void 'id' => 'id', 'updatedAt' => 'updated_at', 'customUpdatedAt' => 'custom_updated_at', + 'notNullableUpdatedAt' => 'not_nullable_updated_at', 'content' => 'content' ], SchemaInterface::LISTENERS => [ - UpdatedAt::class, [ UpdatedAt::class, - ['field' => 'customUpdatedAt'] + ['nullable' => true] + ], + [ + UpdatedAt::class, + ['field' => 'customUpdatedAt', 'nullable' => true] + ], + [ + UpdatedAt::class, + ['field' => 'notNullableUpdatedAt', 'nullable' => false] ] ], SchemaInterface::TYPECAST => [ 'id' => 'int', 'updatedAt' => 'datetime', - 'customUpdatedAt' => 'datetime' + 'customUpdatedAt' => 'datetime', + 'notNullableUpdatedAt' => 'datetime' ], SchemaInterface::SCHEMA => [], SchemaInterface::RELATIONS => [], @@ -73,6 +83,19 @@ public function testCreate(): void $this->assertNull($data->customUpdatedAt); } + public function testCreateNotNullable(): void + { + $post = new Post(); + + $this->save($post); + + $this->orm->getHeap()->clean(); + $select = new Select($this->orm, Post::class); + $data = $select->fetchOne(); + + $this->assertInstanceOf(\DateTimeImmutable::class, $data->notNullableUpdatedAt); + } + public function testUpdate(): void { $post = new Post();