坚持实体坚持不持续的领域变革

时间:2017-03-20 13:57:56

标签: symfony doctrine-orm

我是一个拥有plainPassword和密码属性的实体。在表单中,我映射在plainPassword上。之后,当用户验证表单时,我会在plainPassword上进行密码验证。

为了对密码进行编码,我使用了一个可以监听prePersist和preUpdate的EventSubscriber。它适用于注册表单,因为它是一个新实体,用户填充一些持久化属性,然后学说继续存在并刷新。

但是,当我只是想编辑密码时,它不起作用,我想是因为用户只是编辑非持久化属性。然后,Doctrine并没有试图坚持下去。但我需要它,进入订阅者。

有人知道怎么做吗? (我在另一个实体中遇到了类似的问题)目前,我在控制器中进行操作......

非常感谢。

我的UserSubscriber

  class UserSubscriber implements EventSubscriber
  {
      private $passwordEncoder;
      private $tokenGenerator;

      public function __construct(UserPasswordEncoder $passwordEncoder, TokenGenerator $tokenGenerator)
      {
          $this->passwordEncoder = $passwordEncoder;
          $this->tokenGenerator = $tokenGenerator;
      }

      public function getSubscribedEvents()
      {
          return array(
              'prePersist',
              'preUpdate',
          );
      }

      public function prePersist(LifecycleEventArgs $args)
      {
          $object = $args->getObject();
          if ($object instanceof User) {
              $this->createActivationToken($object);
              $this->encodePassword($object);
          }
      }

      public function preUpdate(LifecycleEventArgs $args)
      {
          $object = $args->getObject();
          if ($object instanceof User) {
              $this->encodePassword($object);
          }
      }

      private function createActivationToken(User $user)
      {
          // If it's not a new object, return
          if (null !== $user->getId()) {
              return;
          }

          $token = $this->tokenGenerator->generateToken();
          $user->setConfirmationToken($token);
      }

      private function encodePassword(User $user)
      {
          if (null === $user->getPlainPassword()) {
              return;
          }

          $encodedPassword = $this->passwordEncoder->encodePassword($user, $user->getPlainPassword());
          $user->setPassword($encodedPassword);
      }

我的用户实体:

class User implements AdvancedUserInterface, \Serializable
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(name="email", type="string", length=255, unique=true)
 * @Assert\NotBlank()
 * @Assert\Email()
 */
private $email;

/**
 * @Assert\Length(max=4096)
 */
private $plainPassword;

/**
 * @ORM\Column(name="password", type="string", length=64)
 */

private $password;

ProfileController可:

class ProfileController extends Controller 
{
/**
 * @Route("/my-profile/password/edit", name="user_password_edit")
 * @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')")
 */
public function editPasswordAction(Request $request)
{
    $user = $this->getUser();
    $form = $this->createForm(ChangePasswordType::class, $user);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        // Encode the password
        // If I decomment it, it's work, but I want to do it autmaticlally, but in the form I just change the plainPassword, that is not persisted in database
        //$password = $this->get('security.password_encoder')->encodePassword($user, $user->getPlainPassword());
        //$user->setPassword($password);
        $em = $this->getDoctrine()->getManager();
        $em->flush();
        $this->addFlash('success', 'Your password have been successfully changed.');
        return $this->redirectToRoute('user_profile');
    }
    return $this->render('user/password/edit.html.twig', [
        'form' => $form->createView(),
    ]);
}

}

1 个答案:

答案 0 :(得分:1)

您可以通过直接操作UnitOfWork强制Doctrine将对象标记为脏。

$em->getUnitOfWork()->scheduleForUpdate($entity)

但是,我强烈反对这种做法,因为它违反了精心设计的抽象层。