aboutcoders/sequence-bundle
Symfony bundle providing an abstract sequence implementation backed by Doctrine ORM. Register the bundle, set abc_sequence db_driver, then fetch the sequence manager service (abc.sequence.sequence_manager) to generate/manage sequences in your app.
Installation:
composer require aboutcoders/sequence-bundle:dev-master
Add to config/bundles.php (Symfony 4+):
return [
// ...
Abc\Bundle\SequenceBundle\AbcSequenceBundle::class => ['all' => true],
];
Configure in config/packages/abc_sequence.yaml:
abc_sequence:
db_driver: orm # or 'doctrine' (alias for ORM)
First Use Case: Generate a sequence via the manager:
$sequenceManager = $this->get('abc.sequence.sequence_manager');
$nextId = $sequenceManager->getNextId('your_sequence_name');
Sequence Generation:
SequenceManager to fetch next IDs:
$nextId = $sequenceManager->getNextId('user_id_seq');
Integration with Doctrine:
sequences) with columns:
// Example migration
Schema::create('sequences', function (Blueprint $table) {
$table->string('name')->primary();
$table->bigInteger('next_id')->default(1);
});
SequenceManager to auto-increment IDs in entities:
$user->id = $sequenceManager->getNextId('user_id_seq');
Custom Sequence Logic:
SequenceManager or create a custom sequence provider:
class CustomSequenceProvider implements SequenceProviderInterface {
public function getNextId(string $sequenceName): int {
// Custom logic (e.g., UUID, timestamp-based)
return time();
}
}
services:
App\Service\CustomSequenceProvider:
tags: ['abc_sequence.sequence_provider']
Batch Processing:
$batchIds = $sequenceManager->getNextIds('user_id_seq', 10);
Database Locking:
SELECT ... FOR UPDATE to prevent race conditions. In high-concurrency apps, this may cause bottlenecks. Consider:
Sequence Naming Collisions:
module_entity_seq). Avoid generic names like id_seq.Initialization Issues:
next_id is not set in the sequences table, the bundle defaults to 1. Explicitly initialize sequences:
$sequenceManager->initializeSequence('user_id_seq', 1000);
Missing Sequences:
sequences table exists and has the correct schema. Run:
php bin/console doctrine:schema:validate
Provider Not Found:
db_driver in config matches your setup (orm/doctrine). For custom providers, ensure they’re tagged correctly.Performance:
sequences table. Add indexes if needed:
Schema::table('sequences', function (Blueprint $table) {
$table->index('name');
});
Event Listeners:
// config/services.yaml
App\EventListener\SequenceListener:
tags:
- { name: kernel.event_listener, event: abc.sequence.pre_generate, method: onPreGenerate }
Sequence Validation:
SequenceManager to validate sequence names or IDs:
public function getNextId(string $sequenceName): int {
if (!preg_match('/^[a-z_]+_[a-z_]+_seq$/', $sequenceName)) {
throw new \InvalidArgumentException('Invalid sequence name format.');
}
return parent::getNextId($sequenceName);
}
Multi-Database Support:
SequenceManager to a specific connection:
services:
abc.sequence.sequence_manager:
arguments:
$connection: '@doctrine.dbal.default_connection'
How can I help you explore Laravel packages today?