andanteproject / timestampable-bundle
一个用于处理实体 createdAt 和 updatedAt 日期的 Doctrine Symfony 扩展包
Requires
- php: ^8.2
- doctrine/common: ^2.13 || ^3.0
- doctrine/event-manager: ^1.2 | ^2.0
- symfony/clock: ^6.2 | ^7.0
- symfony/framework-bundle: ^5.0 | ^6.0 | ^7.0
Requires (Dev)
- ext-json: *
- doctrine/doctrine-bundle: ^2.10
- doctrine/orm: ^2.15.3
- friendsofphp/php-cs-fixer: ^3.58
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.2
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-symfony: ^1.0
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2024-09-14 09:17:34 UTC
README
Timestampable Bundle
Symfony 扩展包 - AndanteProject
一个用于处理实体 createdAt 和 updatedAt 日期的 Doctrine Symfony 扩展包。🕰
要求
Symfony 4.x-7.x 和 PHP 8.2。
安装
通过 Composer
$ composer require andanteproject/timestampable-bundle
特性
- 无需配置即可使用,但完全可自定义;
createdAt
和updatedAt
属性是?\DateTimeImmutable
;- 使用 Symfony Clock;
- 当你显式设置它们时,不会覆盖你的
createdAt
和updatedAt
值; - 无需注解/属性;
- 像魔法一样使用 ✨。
基本用法
安装后,确保您已经将扩展包注册到您的 symfony 扩展包列表中(config/bundles.php
)
return [ /// bundles... Andante\TimestampableBundle\AndanteTimestampableBundle::class => ['all' => true], /// bundles... ];
如果您使用的是 Symfony Flex,这将自动完成。否则,只需手动注册即可。
假设我们有一个 App\Entity\Article
doctrine 实体,我们希望跟踪创建和更新日期。您需要做的就是实现 Andante\TimestampableBundle\Timestampable\TimestampableInterface
并使用 Andante\TimestampableBundle\Timestampable\TimestampableTrait
特性。
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Andante\TimestampableBundle\Timestampable\TimestampableInterface; use Andante\TimestampableBundle\Timestampable\TimestampableTrait; /** * @ORM\Entity() */ class Article implements TimestampableInterface // <-- implement this { use TimestampableTrait; // <-- add this /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private ?int $id = null; /** * @ORM\Column(type="string") */ private string $title; public function __construct(string $title) { $this->title = $title; } // ... // Some others beautiful properties and methods ... // ... }
请确保根据您的 doctrine 工作流程更新您的数据库模式(如果你是牛逼的恶魔般的人物,可以使用 bin/console doctrine:schema:update --force
,如果你选择成为更好的开发者,可以使用 迁移)。
您应该会看到一个名为 created_at
和 updated_at
的新列(或者根据您的 doctrine 命名策略类似的名字)。
恭喜!您已经完成了!🎉
请记住,TimestampableInterface
和 TimestampableTrait
是使用 CreatedAtTimestampableInterface
+CreatedAtTimestampableTrait
和 UpdatedAtTimestampableInterface
+UpdatedAtTimestampableTrait
的快捷方式!如果您只需要跟踪 创建日期 或 更新日期,您可以使用这些更具体的接口!
不使用特性使用
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Andante\TimestampableBundle\Timestampable\TimestampableInterface; /** * @ORM\Entity() */ class Article implements TimestampableInterface // <-- implement this { // No trait needed /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private ?int $id = null; /** * @ORM\Column(type="string") */ private string $title; // DO NOT use ORM annotations to map these properties. See bundle configuration section for more info private ?\DateTimeImmutable $createdAt = null; private ?\DateTimeImmutable $updatedAt = null; public function __construct(string $title) { $this->title = $title; } public function setCreatedAt(\DateTimeImmutable $dateTime): void { $this->createdAt = $dateTime; } public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; } public function setUpdatedAt(\DateTimeImmutable $dateTime): void { $this->updatedAt = $dateTime; } public function getUpdatedAt(): ?\DateTimeImmutable { return $this->updatedAt; } }
这允许您,例如,为您的属性使用不同的名称(例如,使用 created
而不是 createdAt
,使用 updated
而不是 updatedAt
)。但是,您需要在 扩展包配置 中显式指定此内容。
配置(完全可选)
这个扩展包的设计是为了节省您的宝贵时间,并尽可能遵循最佳实践。
这意味着您甚至可以忽略在您的应用程序中创建 andante_timestampable.yml
配置文件。
但是,出于任何原因(例如遗留代码),您可以使用扩展包配置来更改大多数行为以满足您的需求。
andante_timestampable: default: created_at_property_name: createdAt # default: createdAt # The property to be used by default as createdAt date inside entities # implementing CreatedAtTimestampableInterface or TimestampableInterface updated_at_property_name: updatedAt # default: updatedAt # The property to be used by default as updatedAt date inside entities # implementing UpdatedAtTimestampableInterface or TimestampableInterface created_at_column_name: created_at # default: null # Column name to be used on database for create date. # If set to NULL will use your default doctrine naming strategy updated_at_column_name: updated_at # default: null # Column name to be used on database for update date. # If set to NULL will use your default doctrine naming strategy entity: # You can use per-entity configuration to override default config Andante\TimestampableBundle\Tests\Fixtures\Entity\Organization: created_at_property_name: createdAt Andante\TimestampableBundle\Tests\Fixtures\Entity\Address: created_at_property_name: created updated_at_property_name: updated created_at_column_name: created_date updated_at_column_name: updated_date
由AndanteProject团队用爱心打造 ❤️。