nucleos/doctrine-extensions

有用的 Doctrine 事件钩子。


README

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

Continuous Integration Code Coverage Type Coverage

此库提供了一些有用的 Doctrine 钩子。

安装

打开命令控制台,进入您的项目目录,并执行以下命令以下载此库的最新稳定版本

composer require nucleos/doctrine-extensions

使用

可确认实体

如果您需要需要确认的实体,只需在您的实体类中实现 Nucleos\Doctrine\Model\ConfirmableInterface

如果您不需要 symfony 框架,您需要注册 Nucleos\Doctrine\EventListener\ORM\ConfirmableListener

可删除实体

如果您需要软删除的实体,只需在您的实体类中实现 Nucleos\Doctrine\Model\DeletableInterface

如果您不需要 symfony 框架,您需要注册 Nucleos\Doctrine\EventListener\ORM\DeletableListener

生命周期感知实体

如果您需要生命周期信息(创建/更新日期),只需在您的实体类中实现 Nucleos\Doctrine\Model\LifecycleDateTimeInterface

如果您不需要 symfony 框架,您需要注册 Nucleos\Doctrine\EventListener\ORM\LifecycleDateListener

位置感知实体

如果您需要可排序的实体,只需在您的实体类中实现 Nucleos\Doctrine\Model\PositionAwareInterface

如果您不需要 symfony 框架,您需要注册 Nucleos\Doctrine\EventListener\ORM\SortableListener

唯一活动实体

如果您需要只能有一个活动状态的实体,只需在您的实体类中实现 Nucleos\Doctrine\Model\UniqueActiveInterface

如果您不需要 symfony 框架,您需要注册 Nucleos\Doctrine\EventListener\ORM\UniqueActiveListener

表前缀

如果您需要为所有应用程序表和序列添加前缀,您可以使用 TablePrefixEventListener。如果表名已经以定义的前缀开头,它将被忽略。

如果您不需要 symfony 框架,您需要注册 Nucleos\Doctrine\EventListener\ORM\TablePrefixEventListener

迁移使用

  1. id 列从 integer 更改为 guid

  2. 创建一个新的迁移

// src/Migrations/Version123.php
<?php

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Nucleos\Doctrine\Migration\IdToUuidMigration;

class Version123 extends AbstractMigration
{
    private IdToUuidMigration $idToUuidMigration;

    public function __construct(Connection $connection, LoggerInterface $logger)
    {
        parent::__construct($connection, $logger);

        $this->idToUuidMigration = new IdToUuidMigration($this->connection, $logger);
    }


    public function postUp(Schema $schema): void
    {
        $this->idToUuidMigration->migrate('my_table_name');
    }
}

Symfony 使用

如果您想在 symfony 中使用此库,可以使用桥梁。

启用 Bundle

然后,通过将其添加到项目 config/bundles.php 文件中注册的包列表中启用 Bundle。

// config/bundles.php

return [
    // ...
    Nucleos\Doctrine\Bridge\Symfony\Bundle\NucleosDoctrineBundle::class => ['all' => true],
];

配置 Bundle

创建一个名为 nucleos_doctrine.yaml 的配置文件

# config/packages/nucleos_doctrine.yaml

nucleos_doctrine:
    table:
        prefix: 'acme_'

许可证

此库受 MIT 许可证 的约束。