Doctrine 2/ Multi-step form / entity into session

I am working under Symfony2 and Doctrine 2.1.6 and I try to setup a multi-step form. Between each form page, I try to send the doctrine entity into $_SESSION.

According to that dotrine documentation it is possible and even the way to settle multipage forms: http://docs.doctrine-project.org/en/2.1/cookbook/entities-in-session.html

But according to a lot of other post on stackoverflow, it is just not possible to send entities into session.

I have the following controller Action where i pretty much copied/ past the doctrine documentation.

public function indexAction(Request $request, $id)
{
    $session = $request->getSession();
    $em = $this->getDoctrine()->getEntityManager();


    if (isset($_SESSION['propertyAdd'])) {   
    $property = $_SESSION['propertyAdd'];
    $property = $em->merge($property);
    }

    else {

    $property = new property;

    }

    $form = $this->createForm(new propertyType($this->getDoctrine()),$property);

    // check form
    if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);
            if ($form->isValid()){

            $em->detach($property);
            $_SESSION['propertyAdd'] = $property;

            // redirection to next step here  

            }
            }
   return $this->render('AddProperty:'.$id.'.html.twig', array(
        'form' => $form->createView(),));

}

the line $_SESSION['propertyAdd'] = $property; give me the following error:

Fatal error: Uncaught exception 'ErrorException' with message 'Notice: Unknown: "id" returned as member variable from __sleep() but does not exist in Unknown line 0' in G:..\Symfony\vendor\symfony\src\Symfony\Component\HttpKernel\Debug\ErrorHandler.php on line 65

If I replace this line by using the Symfony2 helper

$session->set('propertyAdd', $property);

It throws the following exception:

Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::serialize() must return a string or NULL

Is the doctrine example workable.

Answers


This doesn't answer your question, but why would you:

  • Create an entity
  • Serialize it
  • Put it in the session (I personally don't believe it's a good thing to transform an object to a string)
  • Get it from the session in the form's next step
  • Deserialize it
  • Add the new data to it
  • Serialize it
  • Put it again in the session

An so on...

Why don't you store the form data directly in the session, and create the entity after all the form's steps were complete?

If you're doing this to validate the entity, you can simply use forms (that aren't linked to an entity) and add the validation constraints to them.


Need Your Help

ProgrammingError at / relation "main_post" does not exist

django postgresql

Hello I'm using django and postgresql. And I'm getting the above error. I have database set up like this:

How do I binding data to UserControl

c# wpf datagrid user-controls

I have a my UserControl with Button, TextBlock and DataGrid: