#35809 [Opn->Fbk]: PDO problem with insert transaction with prepared query and currval - Postgres
ID: 35809
Updated by: tony2001@php.net
Reported By: edrozenberg at pobox dot com
-Status: Open
+Status: Feedback
Bug Type: PDO related
Operating System: WindowsXP SP2
PHP Version: 5.1.1
New Comment:
Please try using this CVS snapshot:
http://snaps.php.net/php5.1-latest.tar.gz
For Windows:
http://snaps.php.net/win32/php5.1-win32-latest.zip
Previous Comments:
------------------------------------------------------------------------
[2005-12-26 18:20:02] edrozenberg at pobox dot com
Description:
------------
Created a Database class containing an insert function.
The function takes an array of statements and values
and executes each one within a transaction.
I test it using two statements. The 2nd statement uses
currval('mysequence') to insert an ID that is the same
as the one generated by the first statement.
Problem is the inserts do not take place, and there is
also no exception raised so it looks like the transaction
committed just fine.
Reproduce code:
---------------
Below is the function from the Database.php class:
function insert($inserts)
{
$this->dbh->beginTransaction();
try {
foreach ($inserts as $insert) {
$stmt = $this->dbh->prepare($insert['query']);
$stmt->execute($insert['values']);
}
} catch (Exception $e) {
$this->dbh->rollBack();
echo "Failed: " . $e->getMessage();
return 0;
}
$this->dbh->commit();
return 1;
}
======================================================
I call the Database.php class from here:
<?php
require ('./lib/Database.php');
$db = new Database(NULL);
$inserts[] = array("query"=>'insert into t_company (name, url,
description) values (?, ?, ?)',
"values"=>array('IBM','www.ibm.com','The IBM Company'));
$inserts[] = array("query"=>'insert into t_doc (company_id,
doctype, private_flag) values (?, ?, ?)',
"values"=>array('currval(\'t_company_company_id_seq\')','patent','f'));
$db->insert($inserts)
?>
Expected result:
----------------
The company IBM should be inserted into table t_company.
A row should be inserted into the table t_doc with the
column company_id pointing to the IBM row in t_company.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=35809&edit=1
Thread (4 messages)