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 [▶]