2009年5月5日星期二

Re: [fw-mvc] FW: DB transactions and Zend_Db_Table

If you're using MySQL and InnoDB, then yes, your code should work. When you call $db->beginTransaction(), it executes the query "BEGIN TRANSACTION", which is understood by the InnoDB storage engine. Commits and rollbacks should work, too.

-Hector


On Tue, May 5, 2009 at 3:09 PM, Seth Atkins <satkins@nortel.com> wrote:
I don't want to bug, but is there no one on the list that knows the answer to this question? I haven't received any responses.
 
 
--Seth
 


From: Atkins, Seth (RICH1:5278)
Sent: Wednesday, April 29, 2009 6:23 PM
To: fw-mvc@lists.zend.com
Subject: 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

没有评论: