Skip to content

labmin/database-manager ​

MySQL/MariaDB database access layer (^0.1) with ? parameterized queries, transactions (with deadlock retry), automatic audit logging, table restrictions, and query observers.

bash
composer require labmin/database-manager:^0.1

Bootstrap (DatabaseManager::connect) ​

Configured once per request in the host LIMS (in lims-demo, admin/lib/db Utilities/sqlLib.php):

php
use Labmin\DatabaseManager\DatabaseManager;

DatabaseManager::connect($host, $user, $pass, $dbName)
    ->driver('pdo')                               // 'pdo' or 'mysqli'
    ->audit(new AppUserContext(), exclude: ['tracker'])
    ->restrict(['users' => ['DELETE', 'DROP']])
    ->observe(new DevToolsSessionLog())
    ->ignoreCallerFiles('sqlLib.php');

Connections are lazy — nothing opens until the first query runs.

Query execution ​

Get the singleton via DatabaseManager::getInstance() (or inject DatabaseManager through LimsRouter — in modules, prefer extending LIMS\Platform\Repository):

php
$db = DatabaseManager::getInstance();

$rows     = $db->execute('SELECT * FROM orders WHERE status = ?', ['open']);
$newId    = $db->execute('INSERT INTO orders (title) VALUES (?)', ['Batch A']);
$affected = $db->execute('UPDATE orders SET status = ? WHERE id = ?', ['closed', $newId]);
StatementReturn value of execute()
INSERTNew auto-increment ID
UPDATE, DELETENumber of affected rows
SELECT / otherArrayIterator over associative rows ($row['column'])

Transactions ​

php
// Manual (managed automatically per request by LimsRouter)
$db->beginTransaction();
$db->inTransaction(); // bool
$db->commit();
$db->rollback();

// All-or-nothing batch with automatic retry (up to 3 attempts on deadlock / lock wait)
$db->runTransaction([
    "INSERT INTO samples (name) VALUES ('a')",
    'UPDATE batches SET count = count + 1 WHERE id = 7',
]);

Audit trail, restrictions, observers & exceptions ​

  • Audit trail (->audit()): Mirrors every INSERT, UPDATE, DELETE, and DROP into <db>_audit.<table> with SQL, bound values, user info (UserContext), and caller file/line. Inside a transaction, audit rows write only after commit and are discarded on rollback.
  • Table restrictions (->restrict()): Blocks disallowed verbs per table before SQL reaches MySQL, throwing RestrictedTableException.
  • Query observers (->observe()): Implement QueryObserver::onQuery(QueryEvent $event) for timing/DevTools logging. Observer exceptions never break queries.
  • Exceptions (Labmin\DatabaseManager\Exception\): DatabaseException (base), ConnectionException, QueryException, RestrictedTableException, AuditException.

Labmin LIMS Module Developer Documentation