Learn how to add new fields to your user system built with FOSUserBundle manually easily.

As shown in one of our previous articles "How to implement a user system with FOSUserBundle in Symfony", this bundle allow you to have a super user system easily.

However you may want to add more fields to the user table because the normal structure has only the basic fields to grant the access to a user.

Implementation

To add new fields manually, you only need to add a variable with the name, properties, the getter and setter, update the schema of the database (or add fields manually with a database manager) and you'll be ready to go.

The FosUserBundle requires a User.php class which handles the user system, this class has been previously created during the implementation of FOSUserBundle and it should look similar to :

Note : remember that this class is created somewhere in your project if you have already implemented the user bundle.

<?php
namespace mynamespace;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
    // Change the targetEntity path if you want to create the group
    
    /**
     * @ORM\ManyToMany(targetEntity="userBundle\Entity\Group")
     * @ORM\JoinTable(name="fos_user_user_group",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    protected $groups;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

And now, to add a simple field i.e "about", the previous class will now include the following methods and variables :

<?php
/**
 * @var string
 *
 * @ORM\Column(name="about", type="string", length=255,nullable=true)
 */
private $about;

/**
 * Get about
 *
 * @return String
 */
public function getAbout()
{
    return $this->about;
}

/**
 * Set about
 *
 * @param String $about
 * @return User
 */
public function setAbout($about)
{
    $this->about = $about;

    return $this;
}

And the class finally should look like :

<?php
namespace mynamespace;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="about", type="string", length=255,nullable=true)
     */
    private $about;
    
    // Change the targetEntity path if you want to create the group
    
    /**
     * @ORM\ManyToMany(targetEntity="userBundle\Entity\Group")
     * @ORM\JoinTable(name="fos_user_user_group",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    protected $groups;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    /**
     * Get about
     *
     * @return String
     */
    public function getAbout()
    {
        return $this->about;
    }
    
    /**
     * Set about
     *
     * @param String $about
     * @return User
     */
    public function setAbout($about)
    {
        $this->about = $about;
    
        return $this;
    }
}

Now, just update the schema of your database using the following command (or add the fields in the database manually):

php bin/console doctrine:schema:update --force

Now if you open your database, you'll see the new about field. Read more about the @ORM\Column in the doctrine official documentation here.

Field examples

The following snippet shows different types of fields of common use :

<?php

///// AutoIncrementable integer field
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $myfieldname;

///// Limit string with nullable type
/**
 * @var string
 *
 * @ORM\Column(name="myfieldname", type="string", length=255,nullable=true)
 */
private $myfieldname;

///// Long text field
/**
 * @var string
 *
 * @ORM\Column(name="myfieldname", type="text")
 */
private $myfieldname;

///// Boolean field
/**
 * @var string
 *
 * @ORM\Column(name="myfieldname", type="boolean")
 */
private $myfieldname;

Have fun


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors