andanteproject/timestampable-bundle

一个用于处理实体 createdAt 和 updatedAt 日期的 Doctrine Symfony 扩展包

安装次数: 11,480

依赖: 0

建议者: 0

安全: 0

星级: 6

关注者: 3

分支: 2

开放问题: 0

类型:symfony-bundle

3.0.3 2024-06-14 08:52 UTC

This package is auto-updated.

Last update: 2024-09-14 09:17:34 UTC


README

Andante Project Logo

Timestampable Bundle

Symfony 扩展包 - AndanteProject

Latest Version Github actions Framework Php8 PhpStan

一个用于处理实体 createdAt 和 updatedAt 日期的 Doctrine Symfony 扩展包。🕰

要求

Symfony 4.x-7.x 和 PHP 8.2。

安装

通过 Composer

$ composer require andanteproject/timestampable-bundle

特性

  • 无需配置即可使用,但完全可自定义;
  • createdAtupdatedAt 属性是 ?\DateTimeImmutable
  • 使用 Symfony Clock
  • 当你显式设置它们时,不会覆盖你的 createdAtupdatedAt 值;
  • 无需注解/属性;
  • 像魔法一样使用 ✨。

基本用法

安装后,确保您已经将扩展包注册到您的 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_atupdated_at 的新列(或者根据您的 doctrine 命名策略类似的名字)。

恭喜!您已经完成了!🎉

请记住,TimestampableInterfaceTimestampableTrait 是使用 CreatedAtTimestampableInterface+CreatedAtTimestampableTraitUpdatedAtTimestampableInterface+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团队用爱心打造 ❤️。