What is the best way to handle transactions if I need to output a sequence currval from an insert?
Im wondering if calling the following SQL through MDB2 will ensure that the currval result is the product of the INSERTS. Im just a little foggy with how MDB2 and PostgreSQL are working here- I've seen documentation on using the MDB2 methods such as commit etc... but I'd rather do it in SQL if possible.
This code works, I'm just wondering if it will work correctly with a bunch of concurrent connections.
$mdb2->query(
'BEGIN TRANSACTION;
INSERT INTO sales VALUES (nextval(\'sales_sales_id_seq\'));
INSERT INTO transaction (sales_id) SELECT currval(\'sales_sales_id_seq\');');
$transaction_id_res =$mdb2->query('SELECT currval(\'transaction_transaction_id_seq\') AS transaction_id;');
$mdb2>query('COMMIT TRANSACTION;');
$transaction_id_row = $transaction_id_res->fetchRow();
$transaction_id = $transaction_id_row['transaction_id'];