Created
February 5, 2019 16:00
-
-
Save f1r3starter/7a023c46d16499ec46bb21f1bd711dc1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Doctrine\ORM\Sequencing; | |
use Doctrine\ORM\EntityManagerInterface; | |
class IdentityGenerator implements Generator | |
{ | |
private const NUM_LENGTH = 8; | |
/** | |
* The name of the sequence to pass to lastInsertId(), if any. | |
* | |
* @var string | |
*/ | |
private $sequenceName; | |
/** | |
* @param string|null $sequenceName The name of the sequence to pass to lastInsertId() | |
* to obtain the last generated identifier within the current | |
* database session/connection, if any. | |
*/ | |
public function __construct(?string $sequenceName = null) | |
{ | |
$this->sequenceName = $sequenceName; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function generate(EntityManagerInterface $em, ?object $entity) | |
{ | |
$lastId = (int) $em->getConnection()->lastInsertId($this->sequenceName); | |
$currentYear = date('Y'); | |
return substr($lastId, 0, 2) < $currentYear | |
? str_pad($currentYear, self::NUM_LENGTH, '0', STR_PAD_RIGHT) + 1 | |
: $lastId; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function isPostInsertGenerator() : bool | |
{ | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment