Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read objects persisted but not yet flushed with doctrine

I'm new to symfony2 and doctrine. here is the problem as I see it. i cannot use :

$repository = $this->getDoctrine()->getRepository('entity');
$my_object = $repository->findOneBy($index);

on an object that is persisted, BUT NOT FLUSHED YET !! i think getRepository read from DB, so it will not find a not-flushed object.

my question: how to read those objects that are persisted (i think they are somewhere in a "doctrine session") to re-use them before i do flush my entire batch ?

every profile has 256 physical plumes.

every profile has 1 plumeOptions record assigned to it.

In plumeOptions, I have a cartridgeplume which is a FK for PhysicalPlume.

every plume is identified by ID (auto-generated) and an INDEX (user-generated).

rule: I say profile 1 has physical_plume_index number 3 (=index) connected to it.

now, I want to copy a profile with all its related data to another profile.

new profile is created. New 256 plumes are created and copied from older profile.

i want to link the new profile to the new plume index 3.

check here: http://pastebin.com/WFa8vkt1

like image 254
xeon Avatar asked Oct 24 '11 15:10

xeon


People also ask

What is doctrine flush?

The flush() method syncs data of entities with managed and removed states to the database. Doctrine will issue INSERT , UPDATE , and DELETE SQL queries for the sync. Before that call, all changes are only in-memory and are never synchronized to the database.

What is persist doctrine?

The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share. You can use these interfaces and abstract classes to build your own mapper if you don't want to use the full data mappers provided by Doctrine.

What is persist flush?

Persist and Flush​ flush() . em. persist(entity) is used to mark new entities for future persisting. It will make the entity managed by given EntityManager and once flush will be called, it will be written to the database.


2 Answers

I think you might want to have a look at this function:

$entityManager->getUnitOfWork()->getScheduledEntityInsertions()

Gives you back a list of entity objects which are persisting yet.

Hmm, I didn't really read your question well, with the above you will retrieve a full list (as an array) but you cannot query it like with getRepository. I will try found something for u..

like image 193
Kees Schepers Avatar answered Sep 28 '22 20:09

Kees Schepers


I think you might look at the problem from the wrong angle. Doctrine is your persistance layer and database access layer. It is the responsibility of your domain model to provide access to objects once they are in memory. So the problem boils down to how do you get a reference to an object without the persistance layer?

Where do you create the object you need to get hold of later? Can the method/service that create the object return a reference to the controller so it can propagate it to the other place you need it? Can you dispatch an event that you listen to elsewhere in your application to get hold of the object?

In my opinion, Doctrine should be used at the startup of the application (as early as possible), to initialize the domain model, and at the shutdown of the application, to persist any changes to the domain model during the request. To use a repository to get hold of objects in the middle of a request is, in my opinion, probably a code smell and you should look at how the application flow can be refactored to remove that need.

like image 27
PatrikAkerstrand Avatar answered Sep 28 '22 19:09

PatrikAkerstrand