首页 > 解决方案 > Symfony4 存储在具有关系的会话实体中

问题描述

我有一个多步骤表单,需要将我的实体存储在会话中。问题是实体具有关系,当我从会话中检索它时,关系属性正在消失(空)。

我知道我可以序列化实体并反序列化它,但它有很多检查/代码有很多关系......

public function route1(Session $session, ....)
{
  $entity = new Entity();
  $form = $this->createForm(EntityType::class, $entity);
  $form->handleRequest($request);

  if ($form->isSubmitted() && $form->isValid()) {
     $session->set('entityData', $entity);

     dump($session->get('entityData')); // Here it's fine, nested attributes are still set

     return $this->redirectToRoute('route2');
  }
}


public function route2(Session $session)
{
    $entity = $this->em->merge($session->get('entityData'));
    dump($entity, $entity->getCustomer()->getFistname()); // And here all nested attributes are null like on the image ...
}

在此处输入图像描述

标签: doctrine-ormsymfony4

解决方案


推荐阅读