Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

What does $autoBegin mean?

In Phalcon\Mvc\Model\Transaction\Manager, there is a method get with a parameter $autoBegin, when should I use true/false?



84.5k

When $autoBegin is true it will start the transaction immediately aka. $connection->begin(), when it's false you have to manually begin the transaction after getting the transaction.



1.2k
edited Jul '14
    $order = Orders::findFirstById($id);
    $order->status = 1;
    $trans = $di->get('transactionManager')->get();
    try {
      $trans->begin() // Is `manually begin` like this?  
      $order->setTransaction($trans);
      if($order->update()) {
          $trans->rollback('order save error');
      }
      $log = new Log();
      $log->setTransaction($trans);
      if ($log->create()) {
          $trans->rollback('log save error');
      }

      $trans->commit();
    } catch (Exception $e) {
       //do some other work.
    }

When I use $trans->begin(), the order will not gonna be updated, why? Do I make mistakes?

In this case, $trans->commit() do not work.



84.5k
Accepted
answer
edited Jul '14

You don't have to call manually begin since this is the default behavior:

$order = Orders::findFirstById($id);
$order->status = 1;
$trans = $di->get('transactionManager')->get();
try {

    $order->setTransaction($trans);
    if($order->update()) {
        $trans->rollback('order save error');
    }

    $log = new Log();
    $log->setTransaction($trans);
    if ($log->create()) {
        $trans->rollback('log save error');
    }

    $trans->commit();
} catch (Exception $e) {
    //do some other work.
}

http://docs.phalconphp.com/en/latest/reference/models.html#isolated-transactions



1.2k
edited Jul '14

Thank you for your reply. It's working when I use default behavior. I just want to figure out the difference between default behavior and manually behavior. And when should I use begin manually.