Adding extra fields to FOSUserBundle / SonataUserBundle
By Iain Cuthbertson
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:
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="ApplicationSonataUserBundleEntityUser" table="fos_user_user">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
<many-to-one field="client" target-entity="MyvendorMyBundleEntityClient" orphan-removal="">
<join-columns>
<join-column name="client_id" referenced-column-name="id" nullable="1"/>
</join-columns>
</many-to-one>
</entity>
</doctrine-mapping>
Comments
valqk says: 2nd November 2013 at 8:31 am
A year later it’s still not documented.
Thanks man 🙂
Daniel says: 24th February 2014 at 8:26 pm
Thanx, I couldn’t find this info anywhere… except here 🙂
alexis says: 28th July 2017 at 6:14 pm
2012 – 2017, 5 years later, still not documented?????
Thanks, Man ! You save my life
Ehmz says: 19th February 2018 at 6:37 am
THANK YOU!!
Alisa says: 26th May 2019 at 8:37 am
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.
Add a new field to the xml, like:
Add new property to the Entity: … use Doctrine\ORM\Mapping as ORM; … /**
- @ORM\Column(type=”string”, length=65) */ private $middlename; …
- 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