Re: pear::db, INNO DB and transactions
| From: | Demian Turner | Date: | Fri, 12 Dec 2003 11:37:17 +0000 |
| Subject: | Re: pear::db, INNO DB and transactions | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-9485@lists.php.net to get a copy of this message | ||
hi markus
it's seems there's more than meets the eye in getting transactions to work with dataobject. using a reference to the database resource as your suggest below unfortunately did not fix the problem. in pseudocode, this is what i was trying to do, in MySQL with INNO DB tables:
- autocommit = false
- begin transaction
- create new user dataobject
- insert user dataobject
- create new address dataobject(user's address details in related table)
- insert address dataobject
- if no errors ocurred in either query, commit
- else rollback
this works fine directly in a mysql terminal, and probably in PEAR::DB. but in dataobject it would appear each new dataobject loses track of the transaction.
even in the following code that tries to close the gaps, the problem could not be fixed:
$oUser = & new DataObjects_Usr();
$dbh = & $oUser->getDatabaseConnection();
$dbh->autoCommit(false);
$dbh->query('BEGIN')
// add user
$oUser->setFrom($input->user);
$oUser->user_id = $dbh->nextId('usr');
$oUser->contact_id = $dbh->nextId('contact');
$oUser->passwd = md5($oUser->passwd);
$oUser->date_created = $oUser->last_updated = Base::getTime();
$oUser->created_by = $oUser->updated_by = Session::getUid();
$successUser = $oUser->insert();
// update user's contact details
$oContact = & new DataObjects_Contact();
$dbh = & $oContact->getDatabaseConnection();
$dbh->autoCommit(false);
$oContact->setFrom($input->contact);
$oContact->contact_id = $oUser->contact_id;
$oContact->last_updated = $oContact->last_updated = Base::getTime();
$oContact->created_by = $oContact->updated_by = Session::getUid();
$successContact = $oContact->insert();
if (!($successContact && $successUser)) {
$dbh->query('ROLLBACK');
} else {
// all's well, commit
$dbh->query('COMMIT');
}
anyone got transactions working with dataobjects?
cheers
demian
Demian Turner wrote:calling from DataObject, i used the following code: $oUser = & new DataObjects_Usr(); $db = $oUser->getDatabaseConnection(); $db->autoCommit(false);You should assign the connection object by reference: $db =& $oUser->getDatabaseConnection(); Without the "&", the following call to autoCommit() will only affect the _copy_ of the connection object you've just made, not the one still used by the DataObject. Please test this and post some feedback if this solves the problem. CU Markus