PHPackages                             lorenzo/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. lorenzo/row-locker

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

lorenzo/row-locker
==================

Plugin to lock database rows using the CakePHP ORM

2.0.0(6y ago)1911.6k7[1 PRs](https://github.com/lorenzo/row-locker/pulls)MITPHPPHP &gt;=7.2.0

Since Nov 12Pushed 5y ago2 watchersCompare

[ Source](https://github.com/lorenzo/row-locker)[ Packagist](https://packagist.org/packages/lorenzo/row-locker)[ RSS](/packages/lorenzo-row-locker/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (4)Dependencies (3)Versions (5)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 lorenzo/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

36

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 58.6% of commits — single point of failure

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 ~519 days

Total

4

Last Release

2317d ago

Major Versions

1.0.2 → 2.0.02020-02-18

PHP version history (2 changes)1.0PHP &gt;=5.5

2.0.0PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b4694a64f9dab01ec0e776946b15c84113dee8a51344f1c394f40bd7a453312?d=identicon)[lorenzo](/maintainers/lorenzo)

---

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)")[![hmic](https://avatars.githubusercontent.com/u/876917?v=4)](https://github.com/hmic "hmic (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[josegonzalez/cakephp-upload

CakePHP plugin to handle file uploading sans ridiculous automagic

5461.4M11](/packages/josegonzalez-cakephp-upload)[cakephp/migrations

Database Migration plugin for CakePHP

13812.5M282](/packages/cakephp-migrations)[muffin/trash

Adds soft delete support to CakePHP ORM tables.

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

Simplistic webservices for CakePHP

88194.7k14](/packages/muffin-webservice)[riesenia/cakephp-duplicatable

CakePHP ORM plugin for duplicating entities (including related entities)

51400.8k4](/packages/riesenia-cakephp-duplicatable)[admad/cakephp-sequence

Sequence plugin for CakePHP to maintain ordered list of records

45509.3k6](/packages/admad-cakephp-sequence)

PHPackages © 2026

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