We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

problems with transactions

hi,

I have this sample code in my system:

<?php
$table1 = new table1();
$table1->name($name);

$table2 = new table2();
$table2->setStuff($stuff);

$conn = $this->di->get('my_connection');

$conn->begin();

$success = true;

if ($success){
    if (!$table1->save()) {
        $success = false;
    }
}

if ($success){
    if (!$table2->save()) {
        $success = false;
    }
}

if ($success){
    $conn->commit();
} else {
    $conn->rollback();
}

I force a error in table2 for test the system and rollback, but the table1 is created in tem database.

Why?

In the rollback, should not undo the save?

what the correct way to do?

sorry for my english

hello, if you look on documentation, the correct usage is using transaction manager : https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Transaction_Manager.html