PHP code example of pugx / shortid-doctrine

1. Go to this page and download the library: Download pugx/shortid-doctrine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pugx / shortid-doctrine example snippets

 php


\Doctrine\DBAL\Types\Type::addType('shortid', 'PUGX\Shortid\Doctrine\ShortidType');
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('shortid', 'shortid');
 php


use PUGX\Shortid\Shortid;

/**
 * @Entity
 * @Table
 */
class Product
{
    /**
     * @var Shortid
     *
     * @Id
     * @Column(type="shortid")
     * @GeneratedValue(strategy="NONE")
     */
    private $id;

    public function __construct(?Shortid $id = null)
    {
        $this->id = $id ?? Shortid::generate();
    }

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


use PUGX\Shortid\Shortid;

/**
 * @Entity
 * @Table
 */
class Product
{
    /**
     * @var Shortid
     *
     * @Id
     * @Column(type="shortid")
     * @GeneratedValue(strategy="CUSTOM")
     * @CustomIdGenerator(class="PUGX\Shortid\Doctrine\Generator\ShortidGenerator")
     */
    private $id;

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


use PUGX\Shortid\Shortid;

/**
 * @Entity
 * @Table
 */
class Product
{
    /**
     * @var Shortid
     *
     * @Id
     * @Column(type="shortid", length=5)
     * @GeneratedValue(strategy="NONE")
     */
    private $id;

    public function __construct()
    {
        $this->id = Shortid::generate(5);
    }
}