friends-of-ddd / sequence-number-generator-doctrine
Sequence number generator for Doctrine
1.0.1
2025-03-21 21:38 UTC
Requires
- php: >=8.1
- doctrine/orm: ^2.20.0|^3.3.0|^4.0
- friends-of-ddd/sequence-number-generator: ^1.0
Requires (Dev)
- ext-pdo_pgsql: *
- friendsofphp/php-cs-fixer: ^3.64
- phpunit/phpunit: ^10.5
- symfony/cache: ^6.4
Suggests
- friends-of-ddd/sequence-number-generator-symfony-bundle: A symfony bundle implementation of this library, for easy configuration
README
A doctrine implementation of friends-of-ddd/sequence-number-generator
.
Installation
composer require friends-of-ddd/sequence-number-generator-doctrine
You must create a table in DB which saves sequence values.
In case of Symfony you can use either bin/console doctrine:migrations:diff
(prefered) or bin/console doctrine:schema:update
.
Table with name sequence
will be created by default. If you need another name see "Configuration" section.
Configuration
Symfony and default table name:
By default, the sequences will be recorded in the table named sequence
.
# config/services.yaml:
services:
FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Doctrine\DoctrineSequenceNumberGenerator: ~
FriendsOfDdd\SequenceNumberGenerator\Domain\SequenceNumberGeneratorInterface:
'@FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Doctrine\DoctrineSequenceNumberGenerator'
FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Doctrine\SequenceFactory: ~
FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Doctrine\SequenceFactoryInterface:
'@FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Doctrine\SequenceFactory'
# config/packages/doctrine.yaml:
doctrine:
orm:
mappings:
FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Doctrine:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/vendor/friends-of-ddd/sequence-number-generator-doctrine/src'
prefix: 'FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Doctrine'
alias: SequenceNumberGeneratorEntity
Usage
Generate an incremented sequence number:
use FriendsOfDdd\SequenceNumberGenerator\Domain\SequenceNumberGeneratorInterface;
class OrderFactory
{
public function __construct(
private SequenceNumberGeneratorInterface $sequenceNumberGenerator // inject the service into your class
) {
}
public function create(): void
{
$newOrderId = $this->sequenceNumberGenerator->generateNext('order_id');
return new Order(
id: $newOrderId,
...
);
}
}
Testing
In unit tests you can use FriendsOfDdd\SequenceNumberGenerator\Infrastructure\InMemorySequenceNumberGenerator
to fake real database usage.
use FriendsOfDdd\SequenceNumberGenerator\Infrastructure\InMemorySequenceNumberGenerator;
class OrderFactoryTest extends \PHPUnit\Framework\TestCase
{
private OrderFactory $orderFactory;
protected function setUp(): void
{
$this->orderFactory = new OrderFactory(
sequenceNumberGenerator: new InMemorySequenceNumberGenerator()
);
}
public function testCreateNewOrder(): void
{
// arrange
$previousOrder = $this->orderFactory->create();
// act
$newOrder = $this->orderFactory->create();
// assert
self::assertSame($previousOrder->id + 1, $newOrder->id);
}
}
Supported PHP versions:
- 8.1.*
- 8.2.*
- 8.3.*
- 8.4.*
Supported Doctrine versions
- 2.20.0+
- 3.3.0+
- 4.0.0+