PHPackages                             mir-insight/row-locker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mir-insight/row-locker

ActiveCakephp-plugin[Database &amp; ORM](/categories/database)

mir-insight/row-locker
======================

Plugin to lock database rows using the CakePHP ORM

1.0.5(1y ago)0108MITPHPPHP ^8.1

Since May 6Pushed 1y agoCompare

[ Source](https://github.com/Mir-Insight/row-locker)[ Packagist](https://packagist.org/packages/mir-insight/row-locker)[ RSS](/packages/mir-insight-row-locker/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (7)Used By (0)

RowLocker plugin for the CakePHP ORM
====================================

[](#rowlocker-plugin-for-the-cakephp-orm)

This plugin offers a simple implementation of row locking by storing a timestamp in a field of the row and the name of the lock owner.

Row locking can be useful in CMS-like systems where many people try to change the same record at the same time. By locking the row you can prevent or alert the users from possible data overwrite.

Installation
------------

[](#installation)

You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).

```
composer require mir-insight/row-locker

```

**Note:** Above will install package compactible with CakePHP4. Please refer to [Versions section](https://github.com/lorenzo/row-locker#versions) to install package with CakePHP3.

And then enable the plugin:

```
bin/cake plugin load RowLocker

```

Configuration
-------------

[](#configuration)

Any table to which you wish to apply RowLocker needs to have the following columns:

- `locked_time`: `DATETIME`
- `locked_by` (optional) Can be of any type that identify your users (INT, VARCHAR, UUID...)
- `locked_session` (optional) `VARCHAR(100)` Used for debugging purposes

Usage
-----

[](#usage)

To use RowLocker you first need to add the `LockableInterface` and `LockableTrait` to your entity:

```
use RowLocker\LockableInterface;
use RowLocker\LockableTrait;
...

class Article extends Entity implements LockableInterface
{
    use LockableTrait;

    ...
}
```

Finally, add the behavior to your Table class:

```
class ArticlesTable extends Table
{
    public function initialize()
    {
        ...
        $this->addBehavior('RowLocker.RowLocker');
    }
}
```

### Locking Rows

[](#locking-rows)

To lock any row first load it and the call `lock()` on it. The lock will last for 5 minutes:

```
$article = $articlesTable->get($id);
$article->lock($userId, $sessionId); // both arguments are actaully optional
$articlesTable->save($article);
```

RowLocker provides a shortcut for doing the above for one or many rows, by using the `autoLock` finder:

```
$article = $articlesTable
    ->findById($id)
    ->find('autoLock', ['lockingUser' => $userId, 'lockingSession' => $sessionId])
    ->firstOrFail(); // This locks the row

$article->isLocked(); // return true
```

### Unlocking a Row

[](#unlocking-a-row)

Just call `unlock()` in the entity:

```
$article->unlock();
$articlesTable->save($article);
```

### Finding Unlocked Rows

[](#finding-unlocked-rows)

In order to find unlocked rows (or with locks owned by the same user), use the `unlocked` finder:

```
$firstUnlocked = $articlesTable
    ->find('unlocked', ['lockingUser' => $userId])
    ->firstOrFail();
```

### Safely Locking Rows

[](#safely-locking-rows)

In systems with high concurrency (many users trying to get a lock of the same row), it is highly recommended to use the provided `lockingMonitor()` function:

```
$safeLocker = $articlesTable->lockingMonitor();
// Safely lock the row
$safeLocker(function () use ($id, $userId, $sessionId) {
    $article = $articlesTable
        ->findById($id)
        ->find('autoLock', ['lockingUser' => $userId, 'lockingSession' => $sessionId])
        ->firstOrFail();
});
```

What the locking monitor does is running the inner callable inside a `SERIALIZABLE` transaction.

Versions
--------

[](#versions)

RowLocker has several releases, each compatible with different releases of CakePHP. Use the appropriate version by downloading a tag, or checking out the correct branch.

- `1.x` tags are compatible with CakePHP 3.x and greater.
- `2.x` tags is compatible with CakePHP 4.0.x and is stable to use.

###  Health Score

33

—

LowBetter than 74% of packages

Maintenance52

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

6

Last Release

368d ago

PHP version history (2 changes)1.0.0PHP &gt;=7.2.0

1.0.1PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/c6608203e8d7abd458adb5a1c60615f614224e274f7ee50af78683b20d57b5db?d=identicon)[miguelenes1985](/maintainers/miguelenes1985)

---

Top Contributors

[![ishan-biztech](https://avatars.githubusercontent.com/u/61005017?v=4)](https://github.com/ishan-biztech "ishan-biztech (17 commits)")[![lorenzo](https://avatars.githubusercontent.com/u/37621?v=4)](https://github.com/lorenzo "lorenzo (11 commits)")[![miguelenes1985](https://avatars.githubusercontent.com/u/196306183?v=4)](https://github.com/miguelenes1985 "miguelenes1985 (11 commits)")[![hmic](https://avatars.githubusercontent.com/u/876917?v=4)](https://github.com/hmic "hmic (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mir-insight-row-locker/health.svg)

```
[![Health](https://phpackages.com/badges/mir-insight-row-locker/health.svg)](https://phpackages.com/packages/mir-insight-row-locker)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[josegonzalez/cakephp-upload

CakePHP plugin to handle file uploading sans ridiculous automagic

5451.3M9](/packages/josegonzalez-cakephp-upload)[muffin/trash

Adds soft delete support to CakePHP ORM tables.

851.3M11](/packages/muffin-trash)[muffin/webservice

Simplistic webservices for CakePHP

88191.0k13](/packages/muffin-webservice)[admad/cakephp-sequence

Sequence plugin for CakePHP to maintain ordered list of records

46489.9k6](/packages/admad-cakephp-sequence)[riesenia/cakephp-duplicatable

CakePHP ORM plugin for duplicating entities (including related entities)

51384.5k4](/packages/riesenia-cakephp-duplicatable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
