Skip to content
Advertisement

logged user does not have all fields filled up

I’m using Symfony 2.8 and Doctrine and I have security.yml configured to point to the User class which implements UserInterface. My ORM schema is in YAML.

The problem:

  • In database the user has also specified “email” field, Symfony for LOGGED USER is not filling up that field and also “password” field is empty.

screenshot

When I do $this->get('doctrine.orm.entity_manager')->clear(User::class); then the entity manager is fetching correctly the entity. But when the entity is COMING FROM SESSION, then it’s incorrect.

The problem is also that when I try to fetch a fresh entity from database using find() on a repository then, the incorrect element from session is fetched when I will not use the $em->clear(User::class)

How to tell Symfony/Doctrine to construct my entity in that way that it will have all fields filled up, so it will become persistable?

JavaScript

And ORM configuration:

JavaScript

Advertisement

Answer

You implemented the UserInterface::eraseCredentials() method in a way that it unsets the email and the password:

JavaScript

Symfony will invoke this method before serializing the object to save it in the session.

The UserInterface::eraseCredentials() is meant to remove sensitive data from the user object so it does not end up in for example the session files, but there is no real need to remove the email, like you are doing. A better example would be if you are storing the plaintext version of the user’s password somewhere on this object, this is something you would never want to end up in session files.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement