pugx/shortid-doctrine
最新稳定版本:v1.3.0
Composer 安装命令:
composer require pugx/shortid-doctrine
包简介
Doctrine type for shortid-php
关键字:
README 文档
README
A Doctrine field type for ShortId for PHP.
Installation
Run the following command:
composer require pugx/shortid-doctrine
Note
if you use Symfony, you should require pugx/shortid-doctrine-bundle instead.
Examples
To configure Doctrine to use shortid as a field type, you'll need to set up
the following in your bootstrap:
<?php \Doctrine\DBAL\Types\Type::addType('shortid', 'PUGX\Shortid\Doctrine\ShortidType'); $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('shortid', 'shortid');
Then, in your entities, you may annotate properties by setting the @Column
type to shortid.
You can generate a PUGX\Shortid\Shortid object for the property in your constructor, or
use the built-in generator.
Example with ShortId created manually in constructor:
<?php use Doctrine\ORM\Mapping as ORM; use PUGX\Shortid\Shortid; #[ORM\Entity] #[ORM\Table] class Product { #[ORM\Id] #[ORM\Column(type: 'shortid') #[ORM\GeneratedValue(strategy: 'NONE') private Shortid $id; public function __construct(?Shortid $id = null) { $this->id = $id ?? Shortid::generate(); } public function getId(): Shortid { return $this->id; } }
Example with auto-generated shortid:
<?php use Doctrine\ORM\Mapping as ORM; use PUGX\Shortid\Doctrine\Generator\ShortidGenerator; use PUGX\Shortid\Shortid; #[ORM\Entity] #[ORM\Table] class Product { #[ORM\Id] #[ORM\Column(type: 'shortid') #[ORM\GeneratedValue(strategy: 'CUSTOM') #[ORM\CustomIdGenerator(class: ShortidGenerator::class) private Shortid $id; // etc... }
If you want to customize ShortId length, you can use the length option in the Column annotation. Example:
<?php // use... #[ORM\Entity] #[ORM\Table] class Product { #[ORM\Id] #[ORM\Column(type: 'shortid', length: 5) #[ORM\GeneratedValue(strategy: 'NONE') private Shortid $id; public function __construct() { $this->id = Shortid::generate(5); } }
If you want to customize alphabet and/or to use the built-in generator, you need to setup ShortId in your bootstrap:
<?php \Doctrine\DBAL\Types\Type::addType('shortid', 'PUGX\Shortid\Doctrine\ShortidType'); $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('shortid', 'shortid'); $factory = new \PUGX\Shortid\Factory(); // alphabet must be 64 characters long $factory->setAlphabet('é123456789àbcdefghìjklmnòpqrstùvwxyzABCDEFGHIJKLMNOPQRSTUVWX.!@|'); // length must be between 2 and 20 $factory->setLength(5); PUGX\Shortid\Shortid::setFactory($factory);
Then, you must pay attention to configure every ShortId property with the same length (5 in this example).
统计信息
- 总下载量: 53.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-03