title: Login with 2 different table in Symfony2
body: <p>I'm using Symfony 2.4.3. I've try several methods from many tutorials but still can't make this login works. There are 2 different tables for my login page. <code>mst_pelajar</code> for <code>ROLE_USER</code> with <code>nis</code> field as username and <code>mst_pegawai</code> for <code>ROLE_ADMIN</code> with <code>na</code> field as username.</p>
  
  <p>I can make it works for <code>in_memory</code> username and password definitions. I try to make this login system works by insert plain text into database first. I get <code>bad credentials</code> error with this settings.</p>
  
  <p>Here my security.yml :</p>
  
  <pre><code>security:
      encoders:
          #Symfony\Component\Security\Core\User\User: plaintext
          Sifo\AdminBundle\Entity\MstPelajar: plaintext
          Sifo\AdminBundle\Entity\MstPegawai: plaintext
  
      role_hierarchy:
          #ROLE_ADMIN:       ROLE_USER
          #ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  
      providers:
      #in_memory:
      #    memory:
      #        users:
      #            user:  { password: userpass, roles: [ 'ROLE_USER' ] }
      #            admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
  
          admin_area:
              entity: { class: SifoAdminBundle:MstPegawai, property: na }
          user_area:
              entity: { class: SifoAdminBundle:MstPelajar, property: nis }
  
      firewalls:
          dev:
              pattern:  ^/(_(profiler|wdt)|css|images|js)/
              security: false
              anonymous: true
          alogin:
              pattern:  ^/admin/login$
              security: false
              anonymous: true
          ulogin:
              pattern:  ^/user/login$
              security: false
              anonymous: true
          admin_area:
              pattern:    ^/admin
              anonymous: false
              form_login:
                  check_path: /admin/login_check
                  login_path: /admin/login
              logout:
                  path:   /admin/logout
                  target: /admin
          user_area:
              pattern: ^/user
              anonymous: false
              form_login: 
                  check_path: /user/login_check
                  login_path: /user/login
              logout:
                  path:   /user/logout
                  target: /user
  
      access_control:
          - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/admin/, roles: ROLE_ADMIN }
          - { path: ^/user/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/user/, roles: ROLE_USER }
  </code></pre>
  
  <p>Entity for admin :</p>
  
  <pre><code>&lt;?php
  
  namespace Sifo\AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;
  
  /**
   * MstPelajar
   */
  class MstPelajar implements UserInterface, \Serializable
  {
      /**
       * @var integer
       */
      private $id;
  
       * @var string
       */
      private $nis;
  
  
      /**
       * @var string
       */
      private $password;
  
      /**
       * @var string
       */
      private $salt;
  
      /**
       * @var boolean
       */
      private $aktif;
  
      /**
       * @var \DateTime
       */
      private $timestamp;
  
      /**
       * @var string
       */
      private $operator;
  
      private $username;
  
      /**
       * Set id
       *
       * @param integer $id
       * @return MstPelajar
       */
      public function setId($id)
      {
          $this-&gt;id = $id;
  
          return $this;
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
  
      /**
       * Set nis
       *
       * @param string $nis
       * @return MstPelajar
       */
      public function setNis($nis)
      {
          $this-&gt;nis = $nis;
  
          return $this;
      }
  
      /**
       * Get nis
       *
       * @return string 
       */
      public function getNis()
      {
          return $this-&gt;nis;
      }
  
      /**
       * Set password
       *
       * @param string $password
       * @return MstPelajar
       */
      public function setPassword($password)
      {
          $this-&gt;password = $password;
  
          return $this;
      }
  
      /**
       * Get password
       *
       * @return string 
       */
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      /**
       * Set salt
       *
       * @param string $salt
       * @return MstPelajar
       */
      public function setSalt($salt)
      {
          $this-&gt;salt = $salt;
  
          return $this;
      }
  
      /**
       * Get salt
       *
       * @return string 
       */
      public function getSalt()
      {
          return $this-&gt;salt;
      }
  
      public function __construct()
      {
          $this-&gt;aktif = true;
          // may not be needed, see section on salt below
          // $this-&gt;salt = md5(uniqid(null, true));
      }
  
      public function getUsername()
      {
          return $this-&gt;nis;
      }
  
      public function getRoles()
      {
          return array('ROLE_USER');
      }
  
      public function eraseCredentials()
      {
      }
  
      public function serialize()
      {
          return serialize(array(
              $this-&gt;id,
              $this-&gt;nis,
              $this-&gt;password,
              // see section on salt below
              // $this-&gt;salt,
          ));
      }
  
      public function unserialize($serialized)
      {
          list (
              $this-&gt;id,
              $this-&gt;nis,
              $this-&gt;password,
              // see section on salt below
              // $this-&gt;salt
          ) = unserialize($serialized);
      }
  }
  </code></pre>
  
  <p>The user entity almost same with admin, I'll make admin login work first after get the problem. I just show important field at top.</p>
  
  <p>Here my DefaultController for admin :</p>
  
  <pre><code>&lt;?php
  
  namespace Sifo\AdminBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\Security\Core\SecurityContext;
  use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  
  use Sifo\AdminBundle\Form\DefaultType;
  
  class DefaultController extends Controller
  {
  
  public function indexAction()
      {
          return $this-&gt;render('SifoAdminBundle:Default:index.html.twig');
      }
  
  public function loginAction()
      {
          $request = $this-&gt;getRequest();
          $session = $request-&gt;getSession();
  
          // get the login error if there is one
          if ($request-&gt;attributes-&gt;has(SecurityContext::AUTHENTICATION_ERROR)) {
              $error = $request-&gt;attributes-&gt;get(SecurityContext::AUTHENTICATION_ERROR);
          } else {
              $error = $session-&gt;get(SecurityContext::AUTHENTICATION_ERROR);
              $session-&gt;remove(SecurityContext::AUTHENTICATION_ERROR);
          }
  
          return $this-&gt;render('SifoAdminBundle:Default:login.html.twig', array(
              // last username entered by the user
              'last_username' =&gt; $session-&gt;get(SecurityContext::LAST_USERNAME),
              'error'         =&gt; $error,
          ));
      }
  }
  </code></pre>