Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. SonataAdminBundle
  5. Lock Protection

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
    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Save your teams and projects before they sink

    Save your teams and projects before they sink

    Version:

    Table of Contents

    • Example
    • Enable Lock Protection

    Symfony footer

    Avatar of sparrowek, a Symfony contributor

    Thanks sparrowek for being a Symfony contributor

    1 commit • 2 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony