项目作者: PUGX

项目描述 :
A Doctrine field type for ShortId
高级语言: PHP
项目地址: git://github.com/PUGX/shortid-doctrine.git
创建时间: 2015-11-03T09:47:00Z
项目社区:https://github.com/PUGX/shortid-doctrine

开源协议:MIT License

下载


ShortId Doctrine Type

Total Downloads
Build Status
Code Climate

A Doctrine field type for
ShortId for PHP.

Installation

Run the following command:

  1. 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:

  1. <?php
  2. \Doctrine\DBAL\Types\Type::addType('shortid', 'PUGX\Shortid\Doctrine\ShortidType');
  3. $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:

  1. <?php
  2. use PUGX\Shortid\Shortid;
  3. /**
  4. * @Entity
  5. * @Table
  6. */
  7. class Product
  8. {
  9. /**
  10. * @var Shortid
  11. *
  12. * @Id
  13. * @Column(type="shortid")
  14. * @GeneratedValue(strategy="NONE")
  15. */
  16. private $id;
  17. public function __construct(?Shortid $id = null)
  18. {
  19. $this->id = $id ?? Shortid::generate();
  20. }
  21. public function getId(): Shortid
  22. {
  23. return $this->id;
  24. }
  25. }

Example with auto-generated shortid:

  1. <?php
  2. use PUGX\Shortid\Shortid;
  3. /**
  4. * @Entity
  5. * @Table
  6. */
  7. class Product
  8. {
  9. /**
  10. * @var Shortid
  11. *
  12. * @Id
  13. * @Column(type="shortid")
  14. * @GeneratedValue(strategy="CUSTOM")
  15. * @CustomIdGenerator(class="PUGX\Shortid\Doctrine\Generator\ShortidGenerator")
  16. */
  17. private $id;
  18. public function getId(): Shortid
  19. {
  20. return $this->id;
  21. }
  22. }

If you want to customize ShortId length, you can use the length option in the Column annotation. Example:

  1. <?php
  2. use PUGX\Shortid\Shortid;
  3. /**
  4. * @Entity
  5. * @Table
  6. */
  7. class Product
  8. {
  9. /**
  10. * @var Shortid
  11. *
  12. * @Id
  13. * @Column(type="shortid", length=5)
  14. * @GeneratedValue(strategy="NONE")
  15. */
  16. private $id;
  17. public function __construct()
  18. {
  19. $this->id = Shortid::generate(5);
  20. }
  21. }

If you want to customize alphabet and/or to use the built-in generator, you need to setup ShortId in your bootstrap:

  1. <?php
  2. \Doctrine\DBAL\Types\Type::addType('shortid', 'PUGX\Shortid\Doctrine\ShortidType');
  3. $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('shortid', 'shortid');
  4. $factory = new \PUGX\Shortid\Factory();
  5. // alphabet must be 64 characters long
  6. $factory->setAlphabet('é123456789àbcdefghìjklmnòpqrstùvwxyzABCDEFGHIJKLMNOPQRSTUVWX.!@|');
  7. // length must be between 2 and 20
  8. $factory->setLength(5);
  9. PUGX\Shortid\Shortid::setFactory($factory);

Then, you must pay attention to configure every ShortId property with the same length (5 in this example).