Documentation » Admin

8. Form Types »

« 6. Routing

7. Saving hooks

When the model is persited upon on the stated object two Admin methods are always called. You can extend this method to add custom business logic.

  • new object : prePersist($object) / postPersist($object)
  • edited object : preUpdate($object) / postUpdate($object)
  • deleted object : preRemove($object) / postRemove($object)

7.1. Example used with the FOS/UserBundle

The FOSUserBundle provides authentication features for your Symfony2 Project, and is compatible with Doctrine ORM & ODM. See for more information.

The user management system requires to perform specific call when the user password or username are updated. This is how the Admin bundle can be used to solve the issue by using the prePersist saving hook.

namespace FOS\UserBundle\Admin\Entity;

use Sonata\AdminBundle\Admin\Admin;
use FOS\UserBundle\Model\UserManagerInterface;

class UserAdmin extends Admin
    protected function configureFormFields(FormMapper $formMapper)
                ->add('plainPassword', 'text')
                ->add('groups', 'sonata_type_model', array('required' => false))
                ->add('roles', 'sonata_security_roles', array( 'multiple' => true))
                ->add('locked', null, array('required' => false))
                ->add('expired', null, array('required' => false))
                ->add('enabled', null, array('required' => false))
                ->add('credentialsExpired', null, array('required' => false))

    public function preUpdate($user)

    public function setUserManager(UserManagerInterface $userManager)
        $this->userManager = $userManager;

     * @return UserManagerInterface
    public function getUserManager()
        return $this->userManager;

The service declaration where the UserManager is injected into the Admin class.

<service id="fos.user.admin.user" class="%fos.user.admin.user.class%">
    <tag name="sonata.admin" manager_type="orm" group="fos_user" />
    <argument />
    <argument />

    <call method="setUserManager">
        <argument type='service' id='fos_user.user_manager' />

Found a typo? Something is wrong in this documentation? Just fork and edit it!