Doctrine2 ORM does not update objects that are modified outside of the script

I really did not know what to call it or to search for an already posted question, so I apologize if this has been noticed here before.

I get some unwanted results with the following code:

// get object managers
$fooManager = $this->getContainer()->get('foo_manager');
$barManager = $this->getContainer()->get('bar_manager');

// infinite loop
for (;;) {

    // keep getting unitialized "foo" objects, or quit if none
    while (($foo = $fooManager->findUninitialized()) !== null) {

        // an uninitialized "foo" will need to make a "bar" object
        $bar = $barManager->create();
        $bar->setA('...');

        // save "bar" to database, update() WILL perform a flush()
        $barManager->update($bar);

        // make the association of "bar" to "foo"
        $foo->setBar($bar);

        // save "foo" to database, update() WILL perform a flush()
        $fooManager->update($foo);

    }

    // keep getting unprocessed "foo" objects, or quit if none
    while (($foo = $fooManager->findUnprocessed()) !== null) {

        // process the data from "foo" object "bar"
        process($foo->getBar()->getB());

    }

}

You can see that in the first cycle while, objects $barare created and placed in the database. Another script picks them up and does something with them.

In the second cycle, whileobjects $footry to access their modified "bar" object (note that it is called getB(), we can assume that in another script it is executed separately, it setB()was used to change the state of the object).

, getB(), I, setB() script.

, ( ) , , getB(), , "B", .

, , , , "", $foo->getBar() ( ). , var_dump() , $foo->getBar() , , "bar" .

, - - ( "" script, Doctrine , , ).

, , , . , , Symfony2, "" , ; "", , "", "bar".

, , , $foo->getBar(), . .

+5
1

ChocoDeveloper. refresh($entity) .

:

$em->refresh($entity);
+23

All Articles