I am extending the Sonata User Bundle and creating some extra fields in the new user entity. These fields will only be updated within the Sonata admin area under users so they do not need to be available in the edit profile form. I am having trouble updating these fields via the Sonata User Manager and tried several different ways to extend/implement that class in Application\Sonata\UserBundle. Has anyone encountered this before and can give me a tutorial or step by step process of the cleanest way to extend the new User entity?
3 Answers
1. Create a new bundle
Something like AcmeUserBundle. Create it and register it as you do normally.
2. Create a new User entity
Then create a User
and Group
entity which extends Sonata\UserBundle\Entity\BaseUser
and Sonata\UserBundle\Entity\BaseGroup
. You should also add the configuration for the primary key, for instance:
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
3. Configure the entity
then, go to your app/config/config.yml
file and configure these new entities:
sonata_user:
class:
user: Acme\UserBundle\Entity\User
group: Acme\UserBundle\Entity\Group
4. Override the UserAdmin class
Then, you need to create a new UserAdmin class. To do this, just create a new UserAdmin
class inside your bundle, extend Sonata\UserBundle\Admin\Model\UserAdmin
and override the methods like this:
namespace Acme\UserBundle\Admin;
use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin;
class UserAdmin extends SonataUserAdmin
{
/**
* {@inheritdoc}
*/
protected function configureFormFields(FormMapper $formMapper)
{
parent::configureFormFields($formMapper);
$formMapper
->with('new_section')
->add(...)
// ...
->end()
;
}
}
5. Replace the old UserAdmin class
Then, you need to make sure Sonata uses the new UserAdmin class. You just need to set the sonata.user.admin.user.class
parameter to your new class and your ready!
# app/config/config.yml
parameters:
sonata.user.admin.user.class: Acme\UserBundle\Admin\UserAdmin

- 41,455
- 15
- 107
- 112
-
I am actually already to the end of these steps. My issue comes into play when I extend the User entity class with new properties and then want those properties to be updated on "Update" within the Sonata Admin area. The preUpdate() function is not updating the new fields. – Justin Griffith Mar 25 '13 at 23:54
-
Do you know how to inject other parameters into this admin class ? I don't see the related service. – Pierre de LESPINAY Jun 13 '13 at 08:35
-
1@PierredeLESPINAY just override `sonata.user.admin.user` – Wouter J Jun 13 '13 at 14:16
-
Do we have to redefine all the values ? tags, arguments, ... Do you have a YML example somewhere ? – Pierre de LESPINAY Jun 13 '13 at 15:13
-
Can you please provide any link which would detail this setup process? – Beniston Jul 18 '13 at 06:09
I found out the issue was a doctrine issue. My extended bundle was utilizing the original xml field mappings. I deleted those files and reverted to annotations. Everything worked brilliantly from there. I hope this helps someone else who is experiencing the same issue.

- 273
- 1
- 3
- 11
This is easy, yet the SonataUserBundle
documentation is pretty short on this. Basically, after setting up the two bundles as described here and here:
You need to create a class to extend the Sonata\UserBundle\Entity\BaseUser
class in SonataUserBundle
. Note that if you override the constructor, you still must call the parent object's constructor.
namespace Your\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManager;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
/**
* @ORM\Entity
* @ORM\Table(name="user",indexes={@ORM\Index(name="username_idx", columns={"username"})})
*/
class User extends BaseUser {
public function __construct()
{
parent::__construct();
// your code here
}
/**
* @ORM\Column(type="string")
*/
protected $firstName = "";
public function getFirstName() {
return $this->firstName;
}
public function setFirstName($firstName) {
$this->firstName = $firstName;
}
}
If you need to, you can also override the Sonata\UserBundle\Entity\BaseGroup
object in a similar way.
Then, edit your config.yml
to match your namespaces, like this
# FOS User Bundle Configuration
fos_user:
user_class: Your\Bundle\Entity\User
# To also override the Group object
# group:
# group_class: Your\Bundle\Entity\Group
# Sonata User Bundle configuration
sonata_user:
class:
user: Your\Bundle\Entity\User
# To also override the Group object
# group: Your\Bundle\Entity\Group
Clear the cache. Your entities will be used instead of the built-in ones.

- 5,563
- 5
- 42
- 56
-
Yes, but will they be used in the Sonata Admin area? That is what is happening to me. The entities work correctly for any front end areas, but not in the admin area. – Justin Griffith Mar 28 '13 at 04:39
-
@JustinGriffith, do the fields show in the Edit form of your entity? If yes, what happens when you click Update? It would help if you could edit your question to post your `config.yml` (minus private data) and the `AppKernel.php` file. Please try to describe the problem as precisely as possible. – likeitlikeit Apr 02 '13 at 11:58
-
@JustinGriffith By the way, to use `preUpdate()` (as stated in the comment to the answer by Wouter J), you need to use the `HasLifeCycleCallbacks`, as detailed e.g. [here](http://stackoverflow.com/a/10362437/1553481). – likeitlikeit Apr 02 '13 at 12:03