Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Listener/UpdatedAt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false looks like behavior changing

) {
}

Expand All @@ -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);
}
}
}
14 changes: 9 additions & 5 deletions src/UpdatedAt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]
Expand All @@ -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;
}
Expand All @@ -52,7 +55,8 @@ protected function getListenerClass(): string
protected function getListenerArgs(): array
{
return [
'field' => $this->field
'field' => $this->field,
'nullable' => $this->nullable
];
}

Expand Down
1 change: 1 addition & 0 deletions tests/Behavior/Fixtures/UpdatedAt/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
29 changes: 26 additions & 3 deletions tests/Behavior/Functional/Driver/Common/UpdatedAt/ListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
);
Expand All @@ -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 => [],
Expand All @@ -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();
Expand Down