Skip to content

Lock Protection

Edit this page

Lock protection will prevent data corruption when multiple users edit an object at the same time.

Example

  1. Alice starts to edit the object
  2. Bob starts to edit the object
  3. Alice submits the form
  4. Bob submits the form

In this case, a message will tell Bob that someone else has edited the object, and that he must reload the page and apply the changes again.

Enable Lock Protection

By default, lock protection is disabled. You can enable it in your sonata_admin configuration:

1
2
3
4
5
# config/packages/sonata_admin.yaml

sonata_admin:
    options:
        lock_protection: true

You must also configure each entity that you want to support by adding a field called $version on which the Doctrine Version feature is activated.

Using Annotations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// src/Entity/Car.php

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

class Car
{
    // ...

    #[ORM\Column(type: Types::INTEGER)]
    #[ORM\Version]
    private ?int $version = null;

    // ...
}

Using XML:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<!-- src/Resources/orm/Car.orm.xml -->
<doctrine-mapping>
    <entity name="App\Entity\Car">
        <!-- ... -->

        <field name="version" type="integer" version="true"/>

        <!-- ... -->
    </entity>
</doctrine-mapping>

For more information about this visit the Doctrine docs

Note

If the object model manager does not support object locking, the lock protection will not be triggered for the object. Currently, only the SonataDoctrineORMAdminBundle supports it.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version