Skip to content

Instantly share code, notes, and snippets.

@KristianH
Last active November 30, 2017 11:25
Show Gist options
  • Save KristianH/e3af1586cf3fcd1db6cd3c6cba8fd731 to your computer and use it in GitHub Desktop.
Save KristianH/e3af1586cf3fcd1db6cd3c6cba8fd731 to your computer and use it in GitHub Desktop.
test mysql transactions in a oxid eShop v6.0.x
<?php
/**
* place this gist in a file in folder source/ of a oxid-esales v6.0.0
*/
use OxidEsales\Eshop\Core\DatabaseProvider;
require_once dirname(__FILE__) . "/bootstrap.php";
// tldr: 1. uncommitted MySQL transactions, will not commit queries with warning.
// 2. auto_increment will always be set (even in a rollback)
//NOTE: the INSERT INTO queries will produce a warning:
// Warning: Incorrect integer value: '' for column 'id' at row 1
$tablename = 'd3testlog_'.date('Y_m_d_his');
DatabaseProvider::getDb()->execute("CREATE TABLE {$tablename} ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY) ");
DatabaseProvider::getDb()->startTransaction();
//1 - will be backrolled
DatabaseProvider::getDb()->execute("INSERT INTO {$tablename} VALUES ('') ");
//2 - will be backrolled
DatabaseProvider::getDb()->execute("INSERT INTO {$tablename} VALUES ('') ");
DatabaseProvider::getDb()->rollbackTransaction();
//3 - will be written
DatabaseProvider::getDb()->execute("INSERT INTO {$tablename} VALUES ('') ");
DatabaseProvider::getDb()->startTransaction();
//4 - will be written
DatabaseProvider::getDb()->execute("INSERT INTO {$tablename} VALUES ('') ");
DatabaseProvider::getDb()->commitTransaction();
//5 - will be written
DatabaseProvider::getDb()->execute("INSERT INTO {$tablename} VALUES ('') ");
DatabaseProvider::getDb()->startTransaction();
//6 - will NOT written in table
DatabaseProvider::getDb()->execute("INSERT INTO {$tablename} VALUES ('') ");
//Result:
// [id]
// 3
// 4
// 5
// auto_increment of id is 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment