retrieve from entity manager before ID is given
Here is the situation -
I need to persist an object between pages. On the first page I create the object, and create a form for it. On the second page, I persist the object. The user then has an option to go back and edit their choices, or finish and post.
I would prefer not to flush my changes to the database until the user is done e开发者_Go百科diting the object. The problem is that the object doesn't get a unique ID until it is flushed to the database.
Does anyone know how I can retrieve the persisted yet unflushed object? Is this even possible? Is there a better way to do this?
You serialize the object until you complete all it's properties. You can hold the serialized object in the session to persist between pages. When you are ready to update or insert just unserialize it and put it into the database.
EDIT: example
// page 1
$object->setProperty($value);
$_SESSION['object'] = serialize($object);
// page 2
$object = unserialize($_SESSION['object']);
// use $object
$_SESSION['object'] = serialize($object);
// page N
$object = unserialize($_SESSION['object']);
$object->insertIntoDB();
EDIT 2 - Don't forget to include the class definition in all pages that unserialize the object or else it won't work.
精彩评论