PHP code example of enterprisephp / doctrine-lock-bundle

1. Go to this page and download the library: Download enterprisephp/doctrine-lock-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

enterprisephp / doctrine-lock-bundle example snippets



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