2009年4月29日星期三

[fw-mvc] DB transactions and Zend_Db_Table

I mostly use Zend_Db_Table to query and update rows in the database since it tends to simplify things due to being able to define relationships, primary keys, etc. Occasionally, I end up having to make calls to the DB adapter object for things like quoteInto, fetchPairs, etc.
 
Now if I want to lump multiple table updates into a transaction, the ZF docs show how to do this using the Adapter class. For example:
$db->beginTransaction();

try {
    // Attempt to execute one or more queries:
    $db->query(...);
    $db->query(...);
    $db->query(...);

    // If all succeed, commit the transaction and all changes
    // are committed at once.
    $db->commit();

} catch (exception $e) {
    $tableA->getAdapter()->rollback();
    ..........


}
Should I take that to mean that this is the only way to implement transactions? I know I can indirectly access adapter methods with $myTable->getAdapter()->method() calls, but will the following work?
$tableA->getAdapter()->beginTransaction();

try {
    // Attempt to execute one or more queries:
    $rowA = $tableA->fetch(...);
    $rowB = $tableB->fetch(...);
    //do stuff to each row object;
    $rowA->save();
    $rowB->save();

    // If all succeed, commit the transaction and all changes
    // are committed at once.
    $tableA->getAdapter()->commit();
} catch (exception $e) {
    $tableA->getAdapter()->rollback();
    ..........


}

Seth Atkins

没有评论: