1

I have entity "Page" and put it to session when user click "Preview" in edit action. In preview view user can back to edit, but when I get entity from session and merge it (entity must be managed for put to form) then relational entities have only ID's. I want full load entity from session.

    if ($preview == 1) {
        if ($request->isMethod('POST')) {
            $em->detach($page);
            $request->getSession()->set('page', $page);
        } else {
            $page = $request->getSession()->get('page');
            echo '<pre>';
            var_dump($page);
            \Doctrine\Common\Util\Debug::dump($page, 4);
            //$page = $em->merge($page);
            //
        }
    }

This test show that var_dump print all fields in entity, but Doctrine dump (or merge and then var_dump) have only ID's.. Why?

psalkowski
  • 414
  • 1
  • 8
  • 20
  • It seems that you have to serialize a object before putting it in the session: http://stackoverflow.com/questions/9384836/symfony2-serialize-entity-object-to-session – A.L Jan 10 '14 at 14:24
  • Problem with that though is once unserialized, they are detached from the entity manager. – Flosculus Jan 10 '14 at 14:25
  • @n.1 why I should serialize? Entity loaded from session have all properties, but after merge \Doctrine\Common\Util\Debug::dump($page, 4); I lose all data except ID – psalkowski Jan 10 '14 at 14:36
  • @Flosculus What did u mean? Entity is detached after load from session, but I try merge it and then put to from -- result the same, have not any properties except ID's – psalkowski Jan 10 '14 at 14:37
  • @psalkowski: *why I should serialize?* I have already seen that an object must be serializable in order to be passed to the session but I have never tested, I shared the link in cased it helped you. I don't know much sorry. – A.L Jan 10 '14 at 14:39
  • See the section [What is the importance of serialize and unserialize?](http://symfony.com/doc/current/cookbook/security/entity_provider.html), it may explain why your entity wasn't saved properly in the session. – A.L Jan 10 '14 at 18:39

1 Answers1

2

In first of all it's not a good idea to store preview in session - users can of course edit more than one entity at the same time.

In my opinion better solution is to send form by preview button and call additionally view template with all fields in hidden inputs. Then back button will send the form again and you will be able to bind request with entity in the most simply way.

Piotr Pasich
  • 2,639
  • 2
  • 12
  • 14
  • I had not thought about it. I would try ASAP – psalkowski Jan 10 '14 at 14:39
  • Nice idea but the back button may display a confirmation window to the user, asking to resend the data. The solution may be to add an "edit" button which display the form again. – A.L Jan 10 '14 at 14:40
  • In case you didn't know about yet, see the [Symfony2 documentation](http://symfony.com/doc/current/book/forms.html#submitting-forms-with-multiple-buttons) to see how you can use several buttons. – A.L Jan 10 '14 at 15:30
  • I never knew about this before :) Thanks again! – psalkowski Jan 11 '14 at 19:58