2017 © Pedro PelĂĄez
 

symfony-bundle doctrine-lock-bundle

Lock object against delete, update events

image

enterprisephp/doctrine-lock-bundle

Lock object against delete, update events

  • Monday, April 4, 2016
  • by behramcelen
  • Repository
  • 1 Watchers
  • 11 Stars
  • 392 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 18 % Grown

The README.md

DoctrineLockBundle

  • Lock objects against insert, delete, update events
  • Lock entities against delete, update events

SensioLabsInsight knpbundles.com, (*1)

  • 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:, (*2)

$ 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., (*3)

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:, (*4)

<?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);

The Versions