Search code examples
symfonydoctrine-ormfosuserbundlesymfony-security

Symfony Login form with existing user class


I'm trying to use (and understand) how Security works in Symfony. I've created a login form and it works with hard-coded users.

Now I want to use an existing user table in my database. The table has all the requiered fields but with different column names. The entity also exists, also with different names (for example "customUserId" instead of "id").

Something like (with "MAGIC_MAPPING"):

/**
 * @ORM\Table(name="custom_user_table")
 * @ORM\Entity
 */
class User implements UserInterface, \Serializable
{
    /**
     * @ORM\Column(name="customUserId", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @MAGIC_MAPPING so it links this to the mandatory "id" field
     */
    private $customUserId;

    ...
}

Is there a way to map my existing fields so Symfony uses them for login purpose? Or at least can I make it work without changing my database structure (only the entity class)?

I've tried seleveral actions, from this forum and from Symfony documentation, but it always ends with ugly error I can't fix.

Any idea or lead? Thanks a lot for your help, I've been struggling on this for several hours now...


Solution

  • You have to change the providers configuration inside the security.yml file

    here is an example, i use the email field from my administrator entity, I think you can do the same with your entity

    providers: administrator: entity: { class: Entity:Administrator, property: email }