Skip to content

Commit a2e350b

Browse files
author
vagrant
committed
Update docs.
1 parent 4c79a30 commit a2e350b

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

README.md

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ composer require reshadman/laravel-optimistic-locking
1010

1111
### Basic usage
1212
use the `\Reshadman\OptimisticLocking\OptimisticLocking` trait
13-
in your model, and add the `lock_version` integer field to the
14-
table of the model:
13+
in your model:
1514

1615
```php
1716
<?php
@@ -21,6 +20,7 @@ class BlogPost extends Model {
2120
}
2221
```
2322

23+
and add the `lock_version` integer field to the table of the model:
2424
```php
2525
<?php
2626

@@ -49,6 +49,53 @@ So if two authors are editing the same content concurrently,
4949
you can keep track of your **Read State**, And ask the second
5050
author to rewrite his changes.
5151

52+
### Disabling and enabling optimistic locking
53+
You can disable and enable optimistic locking for a specific
54+
instance:
55+
56+
```php
57+
<?php
58+
$blogPost->disableLocking();
59+
$blogPost->enableLocking();
60+
```
61+
62+
By default optimistic locking is enabled when you use
63+
`OptimisticLocking` trait in your model, to alter the default
64+
behaviour you can set the lock strictly to `false`:
65+
66+
```php
67+
<?php
68+
class BlogPost extends \Illuminate\Database\Eloquent\Model
69+
{
70+
use \Reshadman\OptimisticLocking\OptimisticLocking;
71+
72+
protected $lock = false;
73+
}
74+
```
75+
and then you may enable it: `$blogPost->enableLocking();`
76+
77+
### Use a different column for tracking version
78+
By default the `lock_version` column is used for tracking
79+
version, you can alter that by overriding the following method
80+
of the trait:
81+
82+
```php
83+
<?php
84+
class BlogPost extends \Illuminate\Database\Eloquent\Model
85+
{
86+
use \Reshadman\OptimisticLocking\OptimisticLocking;
87+
88+
/**
89+
* Name of the lock version column.
90+
*
91+
* @return string
92+
*/
93+
protected static function lockVersionColumn()
94+
{
95+
return 'track_version';
96+
}
97+
}
98+
```
5299

53100
## What is optimistic locking?
54101
For detailed explanation read the concurrency section of [*Patterns of Enterprise Application Architecture by Martin Fowler*](https://www.martinfowler.com/eaaCatalog/optimisticOfflineLock.html).
@@ -74,14 +121,17 @@ business logic. That is simply via adding the following criteria
74121
to the update query of a **optimistically lockable model**:
75122

76123
```php
77-
$query->where('id', $this->id)->where('lock_version', $this->lock_version + 1)
124+
<?php
125+
$query->where('id', $this->id)
126+
->where('lock_version', $this->lock_version + 1)
127+
->update($changes);
78128
```
79129

80-
If the version has been updated before your update, it will simply
81-
update no records and means that the model has been updated before
82-
current update attempt or has been deleted.
130+
If the resource has been updated before your update attempt, then the above will simply
131+
update **no** records and it means that the model has been updated before
132+
current attempt or it has been deleted.
83133

84134
## Running tests
85-
Clone the repo perform a composer install and run:
135+
Clone the repo, perform a composer install and run:
86136

87137
```vendor/bin/phpunit```

0 commit comments

Comments
 (0)