title: Symfony 4 Doctrine, How to load user Roles from the Database
body: <p>New to Symfony. How do I load the current loggedin user's role from the database using Doctrine. I have 3 tables laid out like so. </p>
  
  <p><code>users</code> => (<code>user_id, username, password, email</code>)</p>
  
  <p><code>user_roles</code> => (<code>id,user_id,role_id</code>)</p>
  
  <p><code>roles</code> => (<code>role_id, role_name</code>)</p>
  
  <p>I have entities and their corresponding repositories for each table. </p>
  
  <p>My security.yaml looks like this.</p>
  
  <pre><code>security:
  encoders:
      App\Entity\User:
          algorithm: bcrypt
  
  providers:
      our_db_provider:
          entity:
              class: App\Entity\User
              property: username
              # if you're using multiple entity managers
              # manager_name: customer
  
  firewalls:
      dev:
          pattern: ^/(_(profiler|wdt)|css|images|js)/
          security: false
      main:
  #            pattern: ^/
  #            http_basic: ~
          provider: our_db_provider
          anonymous: true
          form_login:
              #login_path is GET request used ti display the form
              # needs to be route names(alias) not the path.
              login_path: login
  
              #check_path is a POST request
              check_path: logincheck
              use_forward: true
  
              default_target_path: default
              always_use_default_target_path: true
  </code></pre>
  
  <p>My <code>Entity/User</code> implements <code>UserInterface</code> component and by reading documents I came to know that the <code>getRoles()</code> method is responsible for updating user roles. I have created a custom method called <code>getUserRoles($id)</code> in my <code>UserRolesRepository.php</code> where I managed to return string array of the current user's roles however I am not able to access this method from the <code>Entity</code>. I know I should not access Repository methods from an Entity class, but I am dearly stuck at this stage. So for now my <code>getRoles()</code> method returns static array <code>return array('ROLE_ADMIN', 'ROLE_EDITOR');</code></p>
  
  <p>My <code>User</code> Entity Class </p>
  
  <pre><code>namespace App\Entity;
  
  use App\Repository\UserRepository;
  use Doctrine\ORM\EntityManager;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;
  use App\Repository\UserRolesRepository;
  use Doctrine\ORM\EntityRepository;
  use App\Services\Helper;
  
  /**
   * @ORM\Table(name="`user`")
   * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
   */
  class User implements UserInterface, \Serializable
  {
      /**
      * @ORM\Id()
      * @ORM\GeneratedValue()
      * @ORM\Column(type="integer")
      */
  private $id;
  
  /**
   * @ORM\Column(type="string", length=25, nullable=true)
   */
  private $username;
  
  /**
   * @ORM\Column(type="string", length=64, nullable=true)
   */
  private $password;
  
  /**
   * @ORM\Column(type="string", length=254, nullable=true)
   */
  private $email;
  
  /**
   * @ORM\Column(type="boolean", nullable=true)
   */
  private $isActive;
  
  private $roles;
  
  /**
   * @ORM\Column(type="string", length=254, nullable=true)
   */
  private $role;
  
  public function __construct()
  {
      $this-&gt;isActive = true;
  }
  
  /**
   * @return mixed
   */
  public function getRole()
  {
      return $this-&gt;role;
  }
  
  /**
   * @param mixed $role
   */
  public function setRole($role)
  {
      $this-&gt;role = $role;
  }
  
  public function getId()
  {
      return $this-&gt;id;
  }
  
  public function getUsername(): ?string
  {
      return $this-&gt;username;
  }
  
  public function setUsername(?string $username): self
  {
      $this-&gt;username = $username;
  
      return $this;
  }
  
  public function getPassword(): ?string
  {
      return $this-&gt;password;
  }
  
  public function setPassword(?string $password): self
  {
      $this-&gt;password = $password;
  
      return $this;
  }
  
  public function getEmail(): ?string
  {
      return $this-&gt;email;
  }
  
  public function setEmail(?string $email): self
  {
      $this-&gt;email = $email;
  
      return $this;
  }
  
  public function getIsActive(): ?bool
  {
      return $this-&gt;isActive;
  }
  
  public function setIsActive(?bool $isActive): self
  {
      $this-&gt;isActive = $isActive;
  
      return $this;
  }
  
  
  //return is required or else returns an fatal error.
  public function getRoles()
  {
      return array('ROLE_ADMIN','ROLE_EDITOR');
  }
  
  public function eraseCredentials()
  {
      // TODO: Implement eraseCredentials() method.
  }
  
  public function serialize()
  {
      // TODO: Implement serialize() method.
      return serialize(array(
          $this-&gt;id,
          $this-&gt;username,
          $this-&gt;password,
      ));
  }
  
  /** @see \Serializable::unserialize() */
  public function unserialize($serialized)
  {
      list (
          $this-&gt;id,
          $this-&gt;username,
          $this-&gt;password,
          // see section on salt below
          // $this-&gt;salt
          ) = unserialize($serialized, ['allowed_classes' =&gt; false]);
  }
  
  public function getSalt()
  {
      // TODO: Implement getSalt() method.
      return null;
  }
  }
  </code></pre>