tsdevelopment/doctrine-behaviors-hashidable

为您的 Doctrine 实体添加 hashidable 行为

v0.0.1 2020-07-03 16:00 UTC

This package is not auto-updated.

Last update: 2024-09-22 10:52:02 UTC


README

这个 PHP 库提供了特质和接口,可以将 hashidable 行为(如 YouTube 视频ID所知)添加到 Doctrine 实体中。

安装

composer require tsdevelopment/doctrine-behaviors-hashidable

用法

您只需定义一个 Doctrine 实体

  • 实现 HashidableInterface 接口
  • 添加 HashidableTrait 特质
<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use TSDevelopment\DoctrineBehaviorsHashidable\Contract\Entity\HashidableInterface;
use TSDevelopment\DoctrineBehaviorsHashidable\Traits\HashidableTrait;

/**
 * @ORM\Entity
 */
class HashidableEntity implements HashidableInterface
{
    use HashidableTrait;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var int
     */
    private $id;

    public function getId(): int
    {
        return $this->id;
    }
}

默认情况下,将使用 id 属性来创建 hashId。但您可以覆盖它

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use TSDevelopment\DoctrineBehaviorsHashidable\Contract\Entity\HashidableInterface;
use TSDevelopment\DoctrineBehaviorsHashidable\Traits\HashidableTrait;

/**
 * @ORM\Entity
 */
class HashidableCustomGetterEntity implements HashidableInterface
{
    use HashidableTrait;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var int
     */
    private $customId;

    public function getCustomId(): int
    {
        return $this->customId;
    }

    public function getHashidableField(): string
    {
        return 'customId';
    }
}

配置

内部,该包使用 https://github.com/roukmoute/hashids-bundle 生成 hashids。有关所有可能的选项,请参阅 configuration 部分。