recipe_lock_protection.rst 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. Lock Protection
  2. ===============
  3. Lock protection will prevent data corruption when multiple users edit an object at the same time.
  4. Example
  5. -------
  6. 1) Alice starts to edit the object
  7. 2) Bob starts to edit the object
  8. 3) Alice submits the form
  9. 4) Bob submits the form
  10. In this case, a message will tell Bob that someone else has edited the object,
  11. and that he must reload the page and apply the changes again.
  12. Enable Lock Protection
  13. ----------------------
  14. By default, lock protection is disabled.
  15. You can enable it in your ``sonata_admin`` configuration :
  16. .. configuration-block::
  17. .. code-block:: yaml
  18. # app/config/config.yml
  19. sonata_admin:
  20. options:
  21. lock_protection: true
  22. 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.
  23. Using Annotations:
  24. .. code-block:: php
  25. <?php
  26. // src/AppBundle/Entity/Car.php
  27. namespace AppBundle\Entity\Car;
  28. use Doctrine\ORM\Mapping as ORM;
  29. class Car
  30. {
  31. // ...
  32. /**
  33. * @ORM\Column(type="integer")
  34. * @ORM\Version
  35. */
  36. protected $version;
  37. // ...
  38. }
  39. Using XML:
  40. .. code-block:: xml
  41. <?xml version="1.0" encoding="utf-8"?>
  42. <!-- src/AppBundle/Resources/orm/Car.orm.xml -->
  43. <doctrine-mapping>
  44. <entity name="AppBundle\Entity\Car">
  45. <!-- ... -->
  46. <field name="version" type="integer" version="true" />
  47. <!-- ... -->
  48. </entity>
  49. </doctrine-mapping>
  50. For more information about this visit the `Doctrine docs <http://doctrine-orm.readthedocs.org/en/latest/reference/transactions-and-concurrency.html?highlight=optimistic#optimistic-locking>`_
  51. .. note::
  52. If the object model manager does not support object locking,
  53. the lock protection will not be triggered for the object.
  54. Currently, only the ``SonataDoctrineORMAdminBundle`` supports it.