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 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;
}
}
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...
}
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);
}
}