A behavior allowing you to use optimistic locking in Propel 2
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
tests First version. Dec 4, 2014
.gitignore First version. Dec 4, 2014
LICENSE Added license file Jan 8, 2015
README.md Note for versionable Dec 4, 2014
composer.json First version. Dec 4, 2014
composer.lock First version. Dec 4, 2014
phpunit.xml First version. Dec 4, 2014



A behavior for Propel2 for optimistic locking.


<table name="user">
    <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
    <column name="username" type="VARCHAR" size="100" primaryString="true" />
    <behavior name="optimistic_locking" />

If you haven't installed this behavior through composer, you need to specify the full class name as behavior name:

    <behavior name="\MJS\OptimisticLocking\OptimisticLockingBehavior">

You can define a different locking columns. Default is version.

<behavior name="optimistic_locking" />
    <parameter name="version_column" value="locked_version"/>
$user = UserQuery::create()->findById($id);

try {
} catch (\MJS\OptimisticLocking\StaleObjectException $e) {
    //react on that case. Maybe show the edit form again with a hint
    //or reload $user and apply again your changes.

if (!$user->optimisticSave(){ 
    //whoops, there was someone faster.

Note for Versionable

This behavior is compatible to versionable-behavior. Make sure optimistic_locking behavior is loaded before versionable.

<behavior name="optimistic_locking" />
<behavior name="versionable" />