guikingone / symfony-uid-doctrine
允许使用 symfony/uid 作为 Doctrine 字段类型
0.1.1
2020-07-11 15:24 UTC
Requires
- php: >=7.2
- doctrine/orm: ^2.5
- symfony/uid: ^5.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- infection/infection: ^0.15.3
- phpstan/phpstan: ^0.12.30
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-12 00:34:33 UTC
README
guikingone/symfony-uid-doctrine
的目标是提供对 Symfony/Uid
作为 Doctrine
类型的支持。
安装
在安装以下项目时,请考虑使用 Packagist
和 Composer
composer require guikingone/symfony-uid-doctrine
配置
Uuid
Doctrine\DBAL\Types\Type::addType('uuid', Guikingone\SymfonyUid\Doctrine\Uuid\UuidType::class);
# config/packages/doctrine.yaml doctrine: dbal: types: uuid: Guikingone\SymfonyUid\Doctrine\Uuid\UuidType
Ulid
Doctrine\DBAL\Types\Type::addType('ulid', Guikingone\SymfonyUid\Doctrine\Ulid\UlidType::class);
# config/packages/doctrine.yaml doctrine: dbal: types: ulid: Guikingone\SymfonyUid\Doctrine\Ulid\UlidType
使用方法
Uuid
use Doctrine\ORM\Mapping as ORM; use Guikingone\SymfonyUid\Doctrine\Uuid\UuidGenerator; /** * @ORM\Entity * @ORM\Table(name="posts") */ class Post { /** * @var Symfony\Component\Uid\Uuid * * @ORM\Id * @ORM\Column(type="uuid", unique=true) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class=UuidGenerator::class) */ protected $id; public function getId(): Uuid { return $this->id; } }
如果你不想使用生成器,请考虑使用 __construct()
方法
use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Uid\Uuid; /** * @ORM\Entity * @ORM\Table(name="posts") */ class Post { /** * @var Symfony\Component\Uid\Uuid * * @ORM\Id * @ORM\Column(type="uuid", unique=true) */ protected $id; public function __construct() { $this->id = Uuid::v4(); } public function getId(): Uuid { return $this->id; } }
Ulid
use Doctrine\ORM\Mapping as ORM; use Guikingone\SymfonyUid\Doctrine\Ulid\UlidGenerator; /** * @ORM\Entity * @ORM\Table(name="posts") */ class Post { /** * @var Symfony\Component\Uid\Ulid * * @ORM\Id * @ORM\Column(type="ulid", unique=true) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class=UlidGenerator::class) */ protected $id; public function getId(): Ulid { return $this->id; } }
如果你不想使用生成器,请考虑使用 __construct()
方法
use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Uid\Ulid; /** * @ORM\Entity * @ORM\Table(name="posts") */ class Post { /** * @var Symfony\Component\Uid\Ulid * * @ORM\Id * @ORM\Column(type="ulid", unique=true) */ protected $id; public function __construct() { $this->id = new Ulid(); } public function getId(): Ulid { return $this->id; } }
贡献
欢迎贡献!请阅读 CONTRIBUTING 获取详细信息。