- Lock objects against insert, delete, update events
- Lock entities against delete, update events
- 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
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.
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(),
);
// ...
}
// ...
}
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);
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);
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);
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);
$objectLocker = $container->get('ep.doctrine.object.locker');
//lock object
$objectLocker->lock(new DummyEntity());
$em->persist(new DummyEntity()); // this will throw LockedObjectException
namespace AppBundle\Entity;
use EP\DoctrineLockBundle\Traits\LockableTrait;
use EP\DoctrineLockBundle\Annotations\Lockable;
/**
* @Lockable
*/
class DummyEntity
{
use LockableTrait;
// ...
//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 update lock
$dummyEntity->setUpdateLocked(false);
//unlock delete lock
$dummyEntity->setDeleteLocked(false);