enterprisephp / doctrine-lock-bundle
Lock object against delete, update events
Installs: 2 584
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^5.5.9|~7.0
- psr/log: ^1.0
- symfony/framework-bundle: ^2.7|^3.0
Requires (Dev)
- jms/serializer-bundle: ^1.0
- sensio/framework-extra-bundle: ^3.0
- symfony/browser-kit: ^2.7|^3.0
- symfony/dependency-injection: ^2.7|^3.0
- symfony/form: ^2.7|^3.0
- symfony/phpunit-bridge: ~2.7|^3.0
- symfony/security-bundle: ^2.7|^3.0
- symfony/serializer: ^2.7|^3.0
- symfony/twig-bundle: ^2.7|^3.0
- symfony/validator: ^2.7|^3.0
- symfony/web-profiler-bundle: ^2.7|^3.0
- symfony/yaml: ^2.7|^3.0
Suggests
- jms/serializer-bundle: Add support for advanced serialization capabilities, recommended, requires ^1.0
- sensio/framework-extra-bundle: Add support for route annotations and the view response listener, requires ^3.0
- symfony/expression-language: Add support for using the expression language in the routing, requires ^2.7|^3.0
- symfony/serializer: Add support for basic serialization capabilities and xml decoding, requires ^2.7|^3.0
- symfony/validator: Add support for validation capabilities in the ParamFetcher, requires ^2.7|^3.0
This package is not auto-updated.
Last update: 2024-11-09 19:28:33 UTC
README
- Lock objects against insert, delete, update events
- Lock entities against delete, update events
Related Links;###
- https://github.com/doctrine/doctrine2/blob/master/docs/en/reference/transactions-and-concurrency.rst#locking-support
- http://dev.mysql.com/doc/refman/5.7/en/lock-tables.html
- https://www.wikiwand.com/en/Lock_(database)
- http://stackoverflow.com/questions/129329/optimistic-vs-pessimistic-locking
- Development steps can be followed from https://github.com/behramcelen/symfony-bundle-develop
- A basic test and logic command can be found in https://github.com/behramcelen/symfony-bundle-develop/blob/master/src/AppBundle/Command/LockBundleTestCommand.php#L41
Installation
Step 1: Download the Bundle
Open a command console, go to your project directory and execute the following command to download the latest version of this bundle:
$ composer require enterprisephp/doctrine-lock-bundle "dev-master"
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new EP\DoctrineLockBundle\EPDoctrineLockBundle(), ); // ... } // ... }
Usage
Doctrine Object Lock
use EP\DoctrineLockBundle\Params\ObjectLockParams; // ... $objectLocker = $container->get('ep.doctrine.object.locker'); //lock fully $objectLocker->lock(new DummyEntity()); //lock delete process $objectLocker->lock(new DummyEntity(), ObjectLockParams::DELETE_LOCK); //lock insert process $objectLocker->lock(new DummyEntity(), ObjectLockParams::INSERT_LOCK); //lock update process $objectLocker->lock(new DummyEntity(), ObjectLockParams::UPDATE_LOCK);
Doctrine Object Unlock
use EP\DoctrineLockBundle\Params\ObjectLockParams; // ... $objectLocker = $container->get('ep.doctrine.object.locker'); //unlock full lock $objectLocker->unlock(new DummyEntity()); //unlock delete process $objectLocker->unlock(new DummyEntity(), ObjectLockParams::DELETE_LOCK); //unlock insert process $objectLocker->unlock(new DummyEntity(), ObjectLockParams::INSERT_LOCK); //unlock update process $objectLocker->unlock(new DummyEntity(), ObjectLockParams::UPDATE_LOCK);
Doctrine Object Switch Lock
use EP\DoctrineLockBundle\Params\ObjectLockParams; // ... $objectLocker = $container->get('ep.doctrine.object.locker'); //switch full lock $objectLocker->switchLock(new DummyEntity()); //switch delete process $objectLocker->switchLock(new DummyEntity(), ObjectLockParams::DELETE_LOCK); //switch insert process $objectLocker->switchLock(new DummyEntity(), ObjectLockParams::INSERT_LOCK); //unswitchlock update process $objectLocker->switchLock(new DummyEntity(), ObjectLockParams::UPDATE_LOCK);
Doctrine Object Is Locked
use EP\DoctrineLockBundle\Params\ObjectLockParams; // ... $objectLocker = $container->get('ep.doctrine.object.locker'); //is full locked $objectLocker->isLocked(new DummyEntity()); //is delete locked $objectLocker->isLocked(new DummyEntity(), ObjectLockParams::DELETE_LOCK); //is insert locked $objectLocker->isLocked(new DummyEntity(), ObjectLockParams::INSERT_LOCK); //is update locked $objectLocker->isLocked(new DummyEntity(), ObjectLockParams::UPDATE_LOCK);
Example Use
$objectLocker = $container->get('ep.doctrine.object.locker'); //lock object $objectLocker->lock(new DummyEntity()); $em->persist(new DummyEntity()); // this will throw LockedObjectException
Doctrine Entity Lock
Add Lockable annotation and lockable trait
namespace AppBundle\Entity; use EP\DoctrineLockBundle\Traits\LockableTrait; use EP\DoctrineLockBundle\Annotations\Lockable; /** * @Lockable */ class DummyEntity { use LockableTrait; // ...
Example Use
//create new dummy entity $dummyEntity = new DummyEntity(); $dummyEntity ->setTitle('Dummy Entity Title') ->setDescription('Dummy Entity Description') ->setUpdateLocked(true) //lock entity for update process ->setDeleteLocked(true) //lock entity for delete process ; $em->persist($dummyEntity); $em->flush(); $dummyEntity->setTitle('Update Dummy Entity Title'); $em->persist($dummyEntity); $em->flush(); // this will throw LockedEntityException because entity have update lock $em->remove($dummyEntity); // this will throw LockedEntityException because entity have delete lock
Unlock Entity Lock
//unlock update lock $dummyEntity->setUpdateLocked(false); //unlock delete lock $dummyEntity->setDeleteLocked(false);