xeon
xeon

Reputation: 849

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

Upvotes: 29

Views: 24306

Answers (3)

Kees Schepers
Kees Schepers

Reputation: 2248

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..

Upvotes: 22

giuseppe
giuseppe

Reputation: 907

Your is a business logic problem effectively.

Querying down the Database a findby Query on Object that are not flushed yet, means heaving much more the DB layer querying object that you have already in your function scope.

Also Keep in mind a findOneBy will retrieve also other object previously saved with same features.

If you need to find only among those new created objects, you should make f.e. them in a Session Array Variable, and iterate them with the foreach.

If you need a mix of already saved items + some new items, you should threate the 2 parts separately, one with a foreach , other one with the repository query!

Upvotes: 0

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45721

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.

Upvotes: 3

Related Questions