0
votes

Im am writing a module to reset my users password. I have a ManyToOne relationship from my LoginHash Entity to my User entity.

/**
 * @ORM\Entity()
 * @ORM\Table(name="login_hash")
 */
class LoginHash
{

    //properties

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $user;

    // constructor (takes AppBundle\Entity\User as argument)
    // getters and setters
}

When a user requests a password reset, I fetch the User that corresponds to the given hash. Then I mutate the User object and try to flush it to the database.

public function onSuccess(Request $request)
{
    $login_hash = $this->em->getRepository(LoginHash::class)->findOneBy(['hash' => $this->hash]);
    $user       = $login_hash->getUser();
    $user->setPlainPassword($this->data->getPlainPassword());
    $login_hash->increaseTimesUsed();

    $this->em->flush();
}

This results into an Exception;

Entity Doctrine\ORM\EntityManager@00000000113b253c000000003f7b1f80 is not >managed. An entity is managed if its fetched from the database or registered as >new through EntityManager#persist 500 Internal Server Error - ORMInvalidArgumentException

I am lost. I am using a single Entity Managed injected into the same class where the onSuccess(Request $request) method lives. And I should not have to persist the objects since they are retrieved from the manager in the first place right? Even when I call persist($user) this exception gets thrown.

Finally I manage to flush($login_hash) to the database if I pass in the $login_hash entity as an argument. This results in that the rows in the login_hash table get updated, but not the user table.

What am I missing here?

Edit:

var_dumped the 00000000113b253c000000003f7b1f80 string, which is actually the reference to the entire EntityManager object.

-unitOfWork: UnitOfWork {#288 ▼
    -identityMap: array:2 [▼
        "AppBundle\Entity\LoginHash" => array:1 [▶]
        "AppBundle\Entity\User" => array:1 [▶]
1
Are you sure 00000000113b253c000000003f7b1f80 is instance of User? It seems to be that you're missing some other object. - Dmitry Malyshenko
Good idea. I var_dumped the object.. that string is the object reference to the entire EntityManager object. - Lee
What happens if you persist($login_hash) and then simply flush()? It certainly seems like you're missing a persist() on something, but I agree that you shouldn't need to flush, since you're retrieving that object through Doctrine. But basically, if you have a persist() on both the $user and $login_hash, followed by a flush(), then that will either work (which will at least give us a clue) or it will still have an error, and we'll know that the missing persist is for something else. - weaverryan
When I persist($login_hash) and then flush(), the same error occurs. I took a different approach to my logic; I had a Doctrine listener that hashes the password before the user was stored. It was as if that was causing the trouble (still not sure how).. but I am hashing the users password now without the listener and this works fine for me. - Lee

1 Answers

0
votes

you must have something like this in your entity:

public function __construct()
{
    $this->user= new ArrayCollection;
}

public function setUser($user)
{
$this->user[]=$user;
}

also, do not forget to persist before flush