webservco / database
A PHP component/library. PDO for MySQL and MariaDB, with a connection that opens on first use, query helpers and transactions.
Requires
- php: ^7.4
- webservco/data: ^0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v2.3.0.74
- v2.3.0
- v2.1.0.74
- v2.1.0
- v2.0.1.74
- v2.0.1
- v2.0.0.74
- v2.0.0
- v1.2.0.74
- v1.2.0
- v1.1.2.74
- v1.1.2
- v1.1.1
- v1.0.0
- v0.x-dev
- v0.9.2
- v0.9.1
- v0.9.0.74
- v0.9.0
- v0.8.0.74
- v0.8.0
- v0.1.6.74
- v0.1.6
- v0.1.5.74
- v0.1.5
- v0.1.4
- v0.1.3.74
- v0.1.3
- v0.1.2.74
- v0.1.2
- v0.1.1.74
- v0.1.1
- v0.1.0.74
- v0.1.0
- v0.0.6.74
- v0.0.6
- v0.0.5.74
- v0.0.5
- v0.0.4.74
- v0.0.4
- v0.0.3.74
- v0.0.3
- v0.0.2.74
- v0.0.2
- v0.0.1
This package is auto-updated.
Last update: 2026-10-11 16:42:21 UTC
README
A PHP component/library. Please see webservco/sandbox for an example project using these types of components.
PDO for MySQL and MariaDB, with a connection that opens on first use, query helpers and transactions.
- Requires:
ext-pdowith the MySQL driver, a MySQL or MariaDB server - Related:
webservco/data(to read typed values from result rows)
Installation
composer require webservco/database
Usage
Create the container in a factory. The webservco/sandbox project does it in a module's local container factory, using values from the configuration.
use WebServCo\Database\Factory\PDOContainerMySQLFactory; $pdoContainerFactory = new PDOContainerMySQLFactory( $configurationGetter->getString('DB_HOST'), $configurationGetter->getInt('DB_PORT'), $configurationGetter->getString('DB_NAME'), $configurationGetter->getString('DB_USER'), $configurationGetter->getString('DB_PASSWORD'), ); // \WebServCo\Database\Contract\PDOContainerInterface $pdoContainer = $pdoContainerFactory->createPDOContainer();
Inject the container (not the PDO object) into storage classes and query through it:
$stmt = $pdoContainer->getPDOService()->prepareStatement( $pdoContainer->getPDO(), 'SELECT id, name FROM item WHERE user_id = ? LIMIT ?', ); $stmt->execute([$userId, 50]); while ($row = $pdoContainer->getPDOService()->fetchAssoc($stmt)) { yield $this->hydrateItem($row); }
Transactions
use WebServCo\Database\Service\TransactionalDatabaseService; // \WebServCo\Database\Contract\TransactionalDatabaseServiceInterface $transactionalDatabaseService = new TransactionalDatabaseService($pdoContainer); $transactionalDatabaseService->beginTransaction(); // ... queries ... $transactionalDatabaseService->commitTransaction();
Contracts
| Interface | Implementation | Purpose |
|---|---|---|
Contract\PDOContainerInterface |
Container\PDOContainer |
getPDO() (shared, opened on first call), createPDO() (new connection), getPDOService(). |
Contract\PDOContainerFactoryInterface |
Factory\PDOContainerMySQLFactory |
Creates the container from connection details. |
Contract\PDOServiceInterface |
Service\PDOService |
Prepare, fetch, error info, last insert id, placeholders. |
Contract\TransactionalDatabaseServiceInterface |
Service\TransactionalDatabaseService |
Begin, commit (rolls back if the commit fails) and roll back. |
Contract\PDOFactoryInterface |
Factory\PDOFactory |
Creates a PDO object with the settings below. |
Namespaces are relative to WebServCo\Database\.
Configuration
The component takes plain values and doesn't care what the keys are called. The sandbox project uses:
[Database] DB_HOST = db DB_PORT = 3306 DB_USER = db DB_PASSWORD = db DB_NAME = db
Notes
Every connection uses utf8mb4, throws exceptions on errors, fetches associative arrays and uses real prepared statements (no emulation). Only the mysql driver is accepted.
Because the container opens the connection on first use, a long-running script doesn't hold an idle connection that the server may close ("MySQL server has gone away").
fetchAssoc() returns [] when there are no more rows. generatePlaceholdersString($values) returns ?,?,? for an IN (...) list. isRecoverableError() is true for a deadlock or a lock wait timeout. Retry those.
Development
# Start project. ddev start # Install dependencies. ddev c u # Run all validations. ddev c c # Run unit tests. ddev c test:dox