Preserve elements when using symfony session

I am trying to use a session with Symfony.
Actually, I want to fill out a form before login.

For this reason I need this circuit:

  • Storing various fields in a session
  • Login / Register
  • Save to database


My controller:

    public function customMadeAction(Request $request)
    {
        $session = $this->container->get('session');

        $user = $this->container->get('security.context')->getToken()->getUser();

        $CustomMade = new CustomMade();
        $form = $this->createForm(new CustomMadeType(), $CustomMade);

        $form->handleRequest($request);

        if ($user === 'anon.') {
            if($form->isValid()) {

                # 1-Save in session
                var_dump($CustomMade); #I have an array with all I need
                $session->set('infos', $CustomMade); #It does not save my informations

                # 2-Redirect to login/register
                $securityContext = $this->container->get('security.context');
                if (!$securityContext->isGranted('ROLE_USER')) {
                    throw new AccessDeniedException('Accès refusé');
                } else {

                # 3-Save in database
                $em = $this->getDoctrine()->getManager();
                $em->persist($CustomMade);
                $em->flush();
                }
            }
        }else{
            if($form->isValid()) {
                $CustomMade->setIdUser($user->getId());
                $em = $this->getDoctrine()->getManager();
                $em->persist($CustomMade);
                $em->flush();
            }
        }
        return $this->render('FrontBundle:Forms:customMade.html.twig', array(
            'form' => $form->createView()
        ));
    }

      

+3


source to share


3 answers


If you want to save data, save only input data from the form (after verification), but not completely the essence of (possibly many ... shit, if it's not what you want).

You can just set the object / array to the session and Symfony will take care of serializing and unserializing it.

If so, replace

$session->set('infos', $CustomMade);

      



by

$session->set('infos', $form->getData());

      

So you later just do:

$data = $session->get('infos');

      

+2


source


I don't think you can store an object in a session. Here are some solutions:

  • save manually each field of your object in session
  • serialize your object and store it in session
  • save it to the database with a flag saying it is not yet attached to the user and store the id in the session


As I see it, 1 is the fastest to implement, but it will hurt to maintain if you add fields to your entity later. Solution 2 is better, but deserialization is not that easy. I think solution 3 is the best, it is not that hard to implement and will not cause a maintenance problem.

+3


source


Antoine's decision is right,

You will need to serialize the object in order to store it in the session.
The easiest way is to use JMSSerializerBundle and decode your object as json :

$serializer = $this->container->get('serializer');
$jsonDecoded = $serializer->serialize($doctrineobject, 'json');

      

Btw. as pointed out in the comments, this will serialize the entire doctrine structure associated with the entity.
However, the toArray () function can be implemented in essence to serialize an array structure, but this approach becomes very tricky when dealing with oneToOne oneToMany and manyToMany relationships

+2


source







All Articles