0

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...

3 Answers 3

2

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 }

Sign up to request clarification or add additional context in comments.

1 Comment

You had part of the answer. Rest is here. Thanks!
0

Try changing the ORM file of that table. create an ORM file using Command promt. then edit the orm file .

    <mapped-superclass name="FOS\UserBundle\Entity\User" table="your_table_name">
    /.../
   </mapped>

Comments

0

Benoît 's answer help me figure it out. I gave too much attention to Symfony documentation's creation process that I forgot the essential: the UserInterface. No need to map anything, just implement this interface and defined methods like:

public function getUsername() {
    return $this->customLogin;
}

Then update the provider configuration (thanks Benoit):

providers:          

    db_sam_provider:
        entity:
            class: AppBundle:User
            property: customLogin 

The other parts are conform to Symfony documentation: here and here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.