cyril-verloop/doctrine-entities

一些默认的 doctrine 实体/字段,需要 PHP >=8.2 和 Doctrine ORM 3.2。

10.0.0 2024-09-10 17:28 UTC

This package is auto-updated.

Last update: 2024-09-27 07:10:51 UTC


README

一些默认的 doctrine 实体/字段,需要 PHP >=8.2 和 Doctrine ORM >=3.2。

对于与 Doctrine ORM <3.2 的兼容性,请参阅本软件的 <8.0 版本。

这包括

  • AbstractIntId:一个映射的父类,具有整数标识符/主键的 $id
  • IntId:一个特质,添加一个整数标识符/主键的 $id
  • IntIdInterface:整数 ID 的接口;
  • Active:一个特质,添加一个 $active 布尔字段;
  • Available:一个特质,添加一个 $available 布尔字段;
  • Priority:一个特质,添加一个 $priority 整数字段;
  • Slug:一个特质,添加一个 $slug 文本字段;
  • Name:一个特质,添加一个 $name 文本字段;
  • Description:一个特质,添加一个 $description 文本字段;
  • NullableDescription:一个特质,添加一个可空的 $description 文本字段;
  • ConnectedAt:两个特质,添加一个 $connectedAt 字段(选择 \DateTime() 或 \DateTimeImmutable());
  • CreatedAt:两个特质,添加一个 $createdAt 字段(选择 \DateTime() 或 \DateTimeImmutable());
  • UpdatedAt:两个特质,添加一个 $updatedAt 字段(选择 \DateTime() 或 \DateTimeImmutable())。

License Type coverage Minimum PHP version

安装

作为 Composer 依赖项

在项目目录中运行

user@host project$ composer require "cyril-verloop/doctrine-entities"

用于开发目的

user@host ~$ cd [PATH_WHERE_TO_PUT_THE_PROJECT] # E.g. ~/projects/
user@host projects$ git clone https://github.com/cyrilverloop/doctrine-entities.git
user@host projects$ cd doctrine-entities
user@host doctrine-entities$ composer install -o
user@host doctrine-entities$ phive install --trust-gpg-keys 4AA394086372C20A,12CE0F1D262429A5,31C7E470E2138192,8AC0BAA79732DD42,C5095986493B4AA0

映射

存在注解、属性和 XML 映射。对于 XML 映射,请参阅下文。

实体

XML 文件位于 config/doctrine/ 目录中。您只需根据需要复制或引用它即可。如果使用 Symfony,则只需在 resources/config/packages/doctrine.yaml 文件中引用 XML 文件。

doctrine:
    orm:
        entity_managers:
            app: # your project name.
                mappings:
                    CyrilVerloop\DoctrineEntities:
                        type: xml
                        dir: '%kernel.project_dir%/vendor/cyril-verloop/doctrine-entities/config/doctrine'
                        prefix: CyrilVerloop\DoctrineEntities

特质

您需要在 XML 文件中复制所需的配置。

例如

<id name="id" type="integer">
    <generator strategy="AUTO" />
    <options>
        <option name="unsigned">true</option>
    </options>
</id>
<field name="connectedAt" column="connected_at" type="datetime" nullable="true" />
<field name="connectedAt" column="connected_at" type="datetime_immutable" nullable="true" />
<field name="createdAt" column="created_at" type="datetime" />
<field name="createdAt" column="created_at" type="datetime_immutable" />
<field name="updatedAt" column="updated_at" type="datetime" nullable="true" />
<field name="updatedAt" column="updated_at" type="datetime_immutable" nullable="true" />
<field name="active" column="active" type="boolean" />
<field name="available" column="available" type="boolean" />
<field name="priority" column="priority" type="smallint">
    <options>
        <option name="default">0</option>
    </options>
</field>
<field name="slug" column="slug" type="text" nullable="true" />
<field name="name" column="name" type="text" />
<field name="description" column="description" type="text" />
<field name="description" column="description" type="text" nullable="true" />

您还可以查看 resources/mappings/Example.orm.xml 文件。

使用方法

AbstractIntId / IntId / IntIdInterface

在版本 <8.0 中,IntId 是一个抽象类。 由于对 不同命名空间中映射的父类的多继承 支持不确定,现在它是一个特质。

如果您的实体需要一个整数作为标识符/主键

  • 它们可以扩展映射的父类 CyrilVerloop\DoctrineEntities\AbstractIntId
<?php

declare(strict_types=1);

namespace MyNamespace;

use CyrilVerloop\DoctrineEntities\AbstractIntId;

class Product extends AbstractIntId
{
    // Your code here.
}
  • 使用 CyrilVerloop\DoctrineEntities\IntId 特质
<?php

declare(strict_types=1);

namespace MyNamespace;

use CyrilVerloop\DoctrineEntities\IntId;

class Product
{
    use IntId;

    public function __construct()
    {
        // Do not forget to initiate the id :
        $this->id = null;
    }
}
  • 实现 CyrilVerloop\DoctrineEntities\IntIdInterface 接口
<?php

declare(strict_types=1);

namespace MyNamespace;

use CyrilVerloop\DoctrineEntities\IntIdInterface;

class Product implements IntIdInterface
{
    // Your code here.
}

Active / Available / Priority / Slug / Timestampable ...

如果您的实体需要其他字段,它们可以使用一个特质。

<?php

declare(strict_types=1);

namespace MyNamespace;

use CyrilVerloop\DoctrineEntities\Available;

class Product
{
    use Available;

    // Your code here.
}

持续集成

测试

要运行测试

user@host doctrine-entities$ ./tools/phpunit -c ./ci/phpunit.xml

生成的输出将在 ./ci/phpunit/ 中。查看 ./ci/phpunit/html/index.html 以获取代码覆盖率,并查看 ./ci/phpunit/testdox.html 以获取通过/失败的测试的详细列表。

要运行突变测试,您必须首先运行 PHPUnit,然后

user@host doctrine-entities$ ./tools/infection -c./ci/infection.json

生成的输出将在 ./ci/infection/ 中。

静态分析

要进行静态分析

user@host doctrine-entities$ ./tools/psalm -c ./ci/psalm.xml [--report=./psalm/psalm.txt --output-format=text]

如果您希望输出到文件而不是屏幕,请使用“--report=./psalm/psalm.txt --output-format=text”。

PHPDoc

要生成PHPDoc

user@host doctrine-entities$ ./tools/phpdocumentor --config ./ci/phpdoc.xml

生成的HTML文档将位于./ci/phpdoc/

标准

本项目中所有PHP文件遵循PSR-12标准。为了缩进代码

user@host doctrine-entities$ ./tools/phpcbf --standard=PSR12 --extensions=php -p ./src/ ./tests/