Adding extra fields to FOSUserBundle / SonataUserBundle

Sadly, this isn’t really documented (at time of writing).

Adding new protected variables to your User.php entity will not actually create database entries when you try to do a doctrine:schema:update.

While one still needs to have the protected variables in this entity class, along with getters and setters, the actual creation work is within UserBundle/Resources/config/doctrine/User.orm.xml

Here is an example for adding a foreign key:




    

        
            
        

        
            
                
            
        

    

5 thoughts on “Adding extra fields to FOSUserBundle / SonataUserBundle”

  1. 7 years later, still not documented 🙂 Thank you so much for your post, that helped me a lot as well.

    I thought my comment might be helpful to somebody as well, for them who are interested in not only having new fields created in DB, but also getters and setters to be generated in the Entity class as well.
    I’m using Symfony 4 and SonataUserBundle.

    1. Add a new field to the xml, like:

    2. Add new property to the Entity:

    use Doctrine\ORM\Mapping as ORM;

    /**
    * @ORM\Column(type=”string”, length=65)
    */
    private $middlename;

    3. Run command bin/console make:entity –regenerate and give the namespace like “App\Application\Sonata\UserBundle\Entity”
    Result:
    updated: src/Application/Sonata/UserBundle/Entity/User.php
    no change: src/Application/Sonata/UserBundle/Entity/Group.php

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.