indragunawan / sequence-bundle
Sequence Implementation
Installs: 4 293
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 5
Open Issues: 2
Type:symfony-bundle
Requires
- php: ^7.1.3
- doctrine/doctrine-bundle: ^1.6.10 || ^2.0
- doctrine/orm: ^2.5.11
- symfony/config: ^3.4 || ^4.0 || ^5.0
- symfony/console: ^3.4||^4.0 || ^5.0
- symfony/dependency-injection: ^3.4 || ^4.0 || ^5.0
Requires (Dev)
- phpunit/phpunit: ^7.5.2 || ^8.0
This package is auto-updated.
Last update: 2025-01-08 13:45:42 UTC
README
Documentation
Installation
Step 1: Download bundle
If your project already uses Symfony Flex, execute this command to download, register and configure the bundle automatically:
composer require indragunawan/sequence-bundle
If you install without using Symfony Flex, first add the bundle by using composer then enable the bundle by adding new Indragunawan\SequenceBundle\IndragunawanSequenceBundle()
to the list of registered bundles in the app/AppKernel.php file of your project
Step 2: Create your Sequence class
<?php // src/App/Entity/Sequence.php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Indragunawan\SequenceBundle\Model\Sequence as BaseSequence; /** * @ORM\Entity(repositoryClass="App\Repository\SequenceRepository") */ class Sequence extends BaseSequence { /** * @ORM\Id() * @ORM\GeneratedValue(strategy="IDENTITY") * @ORM\Column(type="integer") */ private $id; public function getId() { return $this->id; } }
Step 3: Configure the bundle
# app/config/config.yml indragunawan_sequence: orm: class: App\Entity\Sequence manager_name: default #if not set then the default value is 'default'
Step 4: Update your database schema
$ php bin/console doctrine:schema:update --force
Usage
Insert your sequence manually to sequence table in database
Format
For format you can see table below:
Reserved Placeholder
- NUMBER (an integer contains sequence counter).
- php date format See this.
- Rj, Rn, Ry, RY, Rg for Roman numeral of date format.
Example
another example can be see in Tests/Utils/PlaceholderReplacerTest.php
SequenceManager
NOTE: Sequence manager can only be use inside doctrine transaction
Call in Controller
// use Indragunawan\SequenceBundle\Services\SequenceManager $sequenceManager = $this->get(SequenceManager::class); $em->transactional(function () use ($entity) { $entity->setSeqNum($sequenceManager->getNextValue('sequence_name')); });
Call in Event Listener
// use Indragunawan\SequenceBundle\Services\SequenceManagerInterface // Inject SequenceManagerInterface on constructor public function prePersist(LifecycleEventArgs $args) { $entity = $args->getObject(); if ($entity instanceof EntityClass) { $args->getObjectManager()->transactional(function () use ($entity) { $entity->setSeqNum($this->sequenceManager->getNextValue('sequence_name')); }); } }
ResetCounter
$ php bin/console indragunawan:sequence:reset-counter sequence_name
You can put this command into Crontab for periodically restart the counter.
License
This bundle is under the MIT license. See the complete license