简体   繁体   中英

Doctrine multiple flush commit once

I do a lot of search without success. I would like to understand the flush() process in this situation.

//Edit updateOptions
public function updateOptions($values){
    setOption('country',$values['country']);
    setOption('city',$values['city']);
    setOption('adress',$values['adress']);
    setOption('CP',$values['CP']);
    setOption('country_code',$values['country_code']);
}


function setOption($name, $value){

     $em = $this->getEntityManager('option.manager');

     $option = $this->getOption($name); //Entity Options
     $option->setValue($value);

     $em->persist($option);
     $em->flush();
}

When I look the mysql.log or the profiler, i found this:

START TRANSACTION
UPDATE options SET value = 'France' WHERE name = 'country';
COMMIT
START TRANSACTION
SAVEPOINT DOCTRINE2_SAVEPOINT_2
UPDATE options SET value = 'Paris' WHERE name = 'city';
RELEASE SAVEPOINT DOCTRINE2_SAVEPOINT_2
SAVEPOINT DOCTRINE2_SAVEPOINT_2
UPDATE options SET value = 'Rue de Rivoli' WHERE name = 'adress';
RELEASE SAVEPOINT DOCTRINE2_SAVEPOINT_2
SAVEPOINT DOCTRINE2_SAVEPOINT_2
UPDATE options SET value = '75001' WHERE name = 'CP';
RELEASE SAVEPOINT DOCTRINE2_SAVEPOINT_2
SAVEPOINT DOCTRINE2_SAVEPOINT_2
UPDATE options SET value = '33' WHERE name = 'country_code';
RELEASE SAVEPOINT DOCTRINE2_SAVEPOINT_2
ROLLBACK

Only the first one is updated/committed, I get it, but i don't see why the next are rolled back?

This situation also occur if I use setOption() inside a loop for example.

A help would be great. Thanks in advance.

Don not call persist() and flush() each time. I assume that could be the reason. Since you don not explicit tell EM to start a transaction it probably tries to guess it by combination of persist() and flush()

try following:

extend your updateOptions method

public function updateOptions ()
{
    $em = $this->getEntityManager( 'option.manager' );

    $em->beginTransaction();

    setOption( 'country', 'France' );
    setOption( 'city', 'Paris' );
    setOption( 'adress', 'Rue de Rivoli' );
    setOption( 'CP', '75001 ' );
    setOption( 'country_code', '33' );

    $em->flush(); //just notif EM there're records to UPDATE

    //$success = false; 

    try
    {
        $em->commit();
        //$success = true;
    }
    catch ( \Exception $ex )
    {
        $em->rollback();
        // my fav is followinn: in DEV re-throw exception so you can inspect all in symfony-debug-bar
        // in prod just additional emergency log (monolog + swiftmailer) so you get an email

        if( $this->get( 'kernel' )->getEnvironment() !== 'prod' )
        {
            throw $ex;
        }
        $this->get( 'logger' )->addEmergency( 'Oh nooo! Not again :/' );

    }

 //return $success;
}

function setOption($name, $value){

     $option = $this->getOption($name); //Entity Options
     $option->setValue($value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM