saving_hooks.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Saving hooks
  2. ============
  3. When a SonataAdmin is submitted for processing, two events are always called. One
  4. is before any persistence layer interaction and the other is afterwards, the
  5. events are named as follows:
  6. - new object : ``prePersist($object)`` / ``postPersist($object)``
  7. - edited object : ``preUpdate($object)`` / ``postUpdate($object)``
  8. - deleted object : ``preRemove($object)`` / ``postRemove($object)``
  9. It is worth noting that the update events are called whenever the Admin is successfully
  10. submitted, regardless of whether there are any actual persistence layer events. This
  11. differs from the use of preUpdate and postUpdate events in DoctrineORM and perhaps some
  12. other persistence layers.
  13. For example: if you submit an edit form without changing any of the values on the form
  14. then there is nothing to change in the database and DoctrineORM would not fire the **Entity**
  15. class's own ``preUpdate`` and ``postUpdate`` events. However, your **Admin** class's
  16. ``preUpdate`` and ``postUpdate`` methods *are* called and this can be used to your
  17. advantage.
  18. .. note::
  19. When embedding one Admin within another, for example using the ``sonata_type_admin``
  20. field type, the child Admin's hooks are **not** fired.
  21. Example used with the FOS/UserBundle
  22. ------------------------------------
  23. The ``FOSUserBundle`` provides authentication features for your Symfony Project,
  24. and is compatible with Doctrine ORM, Doctrine ODM and Propel. See
  25. `FOSUserBundle on GitHub`_ for more information.
  26. The user management system requires to perform specific calls when the user
  27. password or username are updated. This is how the Admin bundle can be used to
  28. solve the issue by using the ``preUpdate`` saving hook.
  29. .. code-block:: php
  30. <?php
  31. namespace FOS\UserBundle\Admin\Entity;
  32. use Sonata\AdminBundle\Admin\Admin;
  33. use FOS\UserBundle\Model\UserManagerInterface;
  34. class UserAdmin extends Admin
  35. {
  36. protected function configureFormFields(FormMapper $formMapper)
  37. {
  38. $formMapper
  39. ->with('General')
  40. ->add('username')
  41. ->add('email')
  42. ->add('plainPassword', 'text')
  43. ->end()
  44. ->with('Groups')
  45. ->add('groups', 'sonata_type_model', array('required' => false))
  46. ->end()
  47. ->with('Management')
  48. ->add('roles', 'sonata_security_roles', array( 'multiple' => true))
  49. ->add('locked', null, array('required' => false))
  50. ->add('expired', null, array('required' => false))
  51. ->add('enabled', null, array('required' => false))
  52. ->add('credentialsExpired', null, array('required' => false))
  53. ->end()
  54. ;
  55. }
  56. public function preUpdate($user)
  57. {
  58. $this->getUserManager()->updateCanonicalFields($user);
  59. $this->getUserManager()->updatePassword($user);
  60. }
  61. public function setUserManager(UserManagerInterface $userManager)
  62. {
  63. $this->userManager = $userManager;
  64. }
  65. /**
  66. * @return UserManagerInterface
  67. */
  68. public function getUserManager()
  69. {
  70. return $this->userManager;
  71. }
  72. }
  73. The service declaration where the ``UserManager`` is injected into the Admin class.
  74. .. configuration-block::
  75. .. code-block:: xml
  76. <service id="fos.user.admin.user" class="%fos.user.admin.user.class%">
  77. <tag name="sonata.admin" manager_type="orm" group="fos_user" />
  78. <argument />
  79. <argument>%fos.user.admin.user.entity%</argument>
  80. <argument />
  81. <call method="setUserManager">
  82. <argument type="service" id="fos_user.user_manager" />
  83. </call>
  84. </service>
  85. .. _FOSUserBundle on GitHub: https://github.com/FriendsOfSymfony/FOSUserBundle/