public function createAndPersistUser($username, $password) { # Persisting identity principal $principalidentity = new UserPrincipal(); $principalidentity->setDisplayname(ucwords($username)); $principalidentity->setUri('principals/' . $username); $principalidentity->setEmail('*****@*****.**'); $this->entityManager->persist($principalidentity); # Persisting user $user = new User(); $user->setUsername($username); # Not setting salt; handled by the user entity $user->setPassword($this->passwordencoder->encodePassword($password, $user->getSalt())); $user->addRole('ROLE_ADMIN'); $user->addRole('ROLE_FRONTEND_USER'); $this->entityManager->persist($user); $this->entityManager->flush(); return $user; }