PHPackages                             stule-ru/modernpdo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. stule-ru/modernpdo

ActiveLibrary[Database &amp; ORM](/categories/database)

stule-ru/modernpdo
==================

The source code of the simple PDO library.

v3.0.0(2y ago)4442MITPHPPHP &gt;=8.1

Since Apr 13Pushed 2y ago1 watchersCompare

[ Source](https://github.com/StulE-ru/ModernPDO)[ Packagist](https://packagist.org/packages/stule-ru/modernpdo)[ RSS](/packages/stule-ru-modernpdo/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (4)Versions (6)Used By (0)

stule-ru/modernpdo
==================

[](#stule-rumodernpdo)

[![Downloads](https://camo.githubusercontent.com/42f58a69ff0c0556438613b0bc1a3a5450c1652ed2e7ab58441ab3a6a3823cd0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7374756c652d72752f6d6f6465726e70646f)](https://camo.githubusercontent.com/42f58a69ff0c0556438613b0bc1a3a5450c1652ed2e7ab58441ab3a6a3823cd0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7374756c652d72752f6d6f6465726e70646f)[![Release](https://camo.githubusercontent.com/8be80bf0b56f9a29a87381f0239539a089de00cbed7fa89bca73baee999341db/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f5374756c452d72752f4d6f6465726e50444f)](https://camo.githubusercontent.com/8be80bf0b56f9a29a87381f0239539a089de00cbed7fa89bca73baee999341db/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f5374756c452d72752f4d6f6465726e50444f)[![Code Coverage Badge](./.github/badge.svg)](./.github/badge.svg)[![Forks](https://camo.githubusercontent.com/e1408ee19da69c430826295516caf47512c2c66d66055f58263293a24092f53d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f5374756c452d72752f4d6f6465726e50444f)](https://camo.githubusercontent.com/e1408ee19da69c430826295516caf47512c2c66d66055f58263293a24092f53d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f5374756c452d72752f4d6f6465726e50444f)[![Stars](https://camo.githubusercontent.com/91f30d846300cdfcd2c9afd296efb676e0efbc8ce005e99f1e077d7ad61e8c24/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f5374756c452d72752f4d6f6465726e50444f)](https://camo.githubusercontent.com/91f30d846300cdfcd2c9afd296efb676e0efbc8ce005e99f1e077d7ad61e8c24/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f5374756c452d72752f4d6f6465726e50444f)[![License](https://camo.githubusercontent.com/f8ee82170fc5377b13d682ffb00abc0e34c83ecd3abc0dc93f7d14d1aee606e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f5374756c452d72752f4d6f6465726e50444f)](https://camo.githubusercontent.com/f8ee82170fc5377b13d682ffb00abc0e34c83ecd3abc0dc93f7d14d1aee606e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f5374756c452d72752f4d6f6465726e50444f)

This repository contains code of ModernPDO, the library for working with databases using PDO.
The library is guaranteed to support the following databases: [MySQL](https://www.mysql.com/), [MariaDB](https://mariadb.org/), [PostgreSQL](https://www.postgresql.org/), [SQLite3](https://www.sqlite.org/index.html) and custom PDO.

📝 Table of Contents
-------------------

[](#-table-of-contents)

- [About](#about)
- [Getting Started](#getting_started)
- [Usage](#usage)
- [Tests](#tests)
- [Built Using](#built_using)
- [Authors](#authors)

🧐 About
-----------------------------------------

[](#-about-)

This repository contains code of ModernPDO, a library for working with databases using PDO. There are basic tools for working with databases.
For example, here you can see classes for working with insert, delete, select etc.
You can use the library instead of PDO which makes project development easier.

🏁 Getting Started
-------------------------------------------------------------

[](#-getting-started-)

### Installing

[](#installing)

The library uses composer autoloader for including files, so you need to install [Composer](https://getcomposer.org/). If you've never used Composer read [the manual](https://getcomposer.org/doc/00-intro.md). You can install via command line or composer.json.

#### Using command line

[](#using-command-line)

1. Run the command `composer require stule-ru/modernpdo` in the console.

#### Using composer.json

[](#using-composerjson)

1. Add a require `"stule-ru/modernpdo": "^3.0.0"` to your composer.json.

```
...
   "require": {
        ...
        "stule-ru/modernpdo": "^3.0.0"
        ...
   }
...

```

2. Run the command `composer install` in the console.

🎈 Usage
-----------------------------------------

[](#-usage-)

- [Structure](#usage_structure)
- [Initialization](#usage_initialization)
- [Queries](#usage_queries)
- [CRUD](#usage_crud)
- [Table](#usage_table)
- [Transaction](#usage_transaction)

### About Structure

[](#about-structure-)

> ***NOTE:***
> If you want to just use the library and not think about the implementation, [install](getting_started) and [use](usage_initialization)
> Everyone else, welcome to hell =)

#### About Source Code

[](#about-source-code)

The source code contains:

- The general classes ([ModernPDO.php](/src/ModernPDO.php), [Factory.php](/src/Factory.php), [Escaper.php](/src/Escaper.php), etc.)
- The specific DBMS classes (they are located in [/src/Drivers/\*](/src/Drivers/))

This allows **append** code rather than **rewrite** ([MySQLDriver](/src/Drivers/MySQLDriver.php), [PostgreSQLDriver](/src/Drivers/PostgreSQLDriver.php), [SQLite3Driver](/src/Drivers/SQLite3Driver.php) extend [ModernPDO](/src/ModernPDO.php) and etc.)

So, if you need to, you can extend all general classes to achieve your goals.

The general classes:

- [**Actions/\***](/src/Actions) - query builders like [Select](/src/Actions/Select.php), [Update](/src/Actions/Update.php) and etc.
- [**Conditions/\***](/src/Conditions) - condition builders for 'select' like [Between](/src/Conditions/Between.php), [In](/src/Conditions/In.php) and etc.
- [**Fields/\***](/src/Fields) - field builders for 'create/update table' like [IntField](/src/Fields/IntField.php), [TextField](/src/Fields/TextField.php) and etc.
- [**Functions/\***](/src/Functions) - aggregate and scalar function builders like [Count](/src/Functions/Aggregate/Count.php), [Upper](/src/Functions/Scalar/String/Upper.php) and etc.
- [**Keys/\***](/src/Keys) - key builders for 'create/update table' like [PrimaryKey](/src/Keys/PrimaryKey.php), [UniqueKey](/src/Keys/UniqueKey.php) and etc.
- [**Traits\***](/src/Traits) - traits with shared methods like where(), columns() and etc.
- [**ModernPDO**](/src/ModernPDO.php) - base class for working with database (methods exec(), query(), select() and etc.)
- [**Escaper**](/src/Escaper.php) - base class for escaping values.
- [**Factory**](/src/Factory.php) - base class for making new actions, transactions and etc.
- [**Statement**](/src/Statement.php) - base class for working with database response.
- [**Transaction**](/src/Transaction.php) - base class for working with transactions.

The drivers:

- [MariaDB](/src/Drivers/MariaDBDriver.php) - driver for working with [MariaDB](https://mariadb.org/).
- [MySQL](/src/Drivers/MySQLDriver.php) - driver for working with [MySQL](https://www.mysql.com/).
- [PostgreSQL](/src/Drivers/PostgreSQLDriver.php) - driver for working with [PostgreSQL](https://www.postgresql.org/).
- [SQLite3](/src/Drivers/SQLite3Driver.php) - driver for working with [SQLite3](https://www.sqlite.org/index.html).

#### About Tests

[](#about-tests)

There are 2 types of tests: [**Integration**](/tests/Integration) and [**Unit**](/tests/Unit).

### Initialization Examples

[](#initialization-examples-)

```
use ModernPDO\ModernPDO;
use ModernPDO\Drivers\MySQLDriver;
use ModernPDO\Drivers\MariaDBDriver;
use ModernPDO\Drivers\PostgreSQLDriver;
use ModernPDO\Drivers\SQLite3Driver;

// Initiolize by PDO
$mpdo = new ModernPDO(
    pdo: $pdo,
);

// Initiolize MySQL
$mpdo = new MySQLDriver(
    host: $host,
    database: $database,
    username: $username,
    password: $password,
    charset: $charset,
    //port: $port,
);

// Initiolize MariaDB
$mpdo = new MariaDBDriver(
    host: $host,
    database: $database,
    username: $username,
    password: $password,
    charset: $charset,
    //port: $port,
);

// Initiolize PostgreSQL
$mpdo = new PostgreSQLDriver(
    host: $host,
    database: $database,
    username: $username,
    password: $password,
    //port: $port,
);

// Initiolize SQLite3
$mpdo = new SQLite3Driver(
    mode: $mode,
);
```

### Queries Examples

[](#queries-examples-)

```
// Row query

$mpdo->exec('CREATE TABLE table_name (id int, name varchar(32));');

// Prepared queries

$stmt = $mpdo->query("SELECT * FROM table_name", []);

// Check query status
if ($stmt->status()) {

    // Get counts
    $stmt->rowCount();
    $stmt->columnCount();

    $stmt->fetchColumn($column); // Fetch cell
    $stmt->fetchObject(); // Fetch row as object
    $stmt->fetch(); // Fetch row as array
    $stmt->fetchAll(); // Fetch all rows as array
}
```

### CRUD Examples

[](#crud-examples-)

```
//
// Insert example
//

// INSERT INTO table (id, name) VALUES (10, 'test'), (11, 'test')
$mpdo->insert('table')->columns([
    'id', 'name',
])->values([
    [10, 'test'],
    [11, 'test'],
])->execute();

// INSERT INTO table VALUES (12, 'test')
$mpdo->insert('table')->values([
    [12, 'test'],
])->execute();

//
// Select examples
//

// SELECT * FROM table
$mpdo->select('table')->rows();

// SELECT * FROM table WHERE id=10 LIMIT 1
$mpdo->select('table')->where('id', 10)->row();
// SELECT * FROM table WHERE id=10 AND name='test' LIMIT 1
$mpdo->select('table')->where('id', 10)->and('name', 'test')->row();

// SELECT id, name FROM table
$mpdo->select('table')->columns(['id', 'name'])->rows();

// SELECT COUNT(*) FROM table
$mpdo->select('table')->columns([new Count()])->cell();

// SELECT SUM(amount) FROM table WHERE id BETWEEN 10 AND 50
$mpdo->select('table')->columns([new Sum('amount')])->where('id', new Between(10, 50))->cell();

// SELECT table.id AS id, table.name AS name, join_table.lastname AS lastname FROM table INNER JOIN join_table ON table.id=join_table.id
$mpdo->select('table')->columns([
    'id' => 'table.id',
    'name' => 'table.name',
    'lastname' => 'join_table.lastname',
])->innerJoin('join_table')->on('table.id', 'join_table.id')->rows();

// SELECT * FROM table ORDER BY id ASC
$mpdo->select('table')->orderBy('id')->rows();

// SELECT * FROM table LIMIT 1 OFFSET 10
$mpdo->select('table')->limit(1, 10)->row();

//
// Update example
//

// UPDATE table SET name='Mr. Gorski' WHERE id=10
$mpdo->update('table')->set(['name' => 'Mr. Gorski'])->where('id', 10)->execute();

//
// Delete example
//

// DELETE FROM table WHERE id NOT IN (10, 11, 20)
$mpdo->delete('table')->where('id', new NotIn([10, 11, 20]))->execute();
```

### Table Examples

[](#table-examples-)

```
//
// Create Table
//

// CREATE TABLE IF NOT EXISTS table (id INT NOT NULL, email TEXT NOT NULL, name VARCHAR(32) NOT NULL)
$mpdo->createTable('table')->checkIfExists()->fields([
    new IntField('id'),
    new TextField('email'),
    new VarcharField('name', 32),
])->execute();

// CREATE TABLE IF NOT EXISTS table (id INT UNSIGNED NULL DEFAULT 100)
$mpdo->createTable('table')->checkIfExists()->fields([
    new IntField('id', unsigned: true, canBeNull: true, default: 100),
])->execute();

// CREATE TABLE IF NOT EXISTS table (id INT NOT NULL, PRIMARY KEY (id))
$mpdo->createTable('table')->checkIfExists()->fields([
    new IntField('id'),
])->keys([
    new PrimaryKey('id'),
])->execute();

//
// Update Table
//

// ALTER TABLE table RENAME TO new_table
$mpdo->alterTable('table')->rename('new_table')->execute();

// ALTER TABLE table ADD COLUMN amount INT NOT NULL
$mpdo->alterTable('table')->addColumns([
    new IntField('amount'),
])->execute();

// ALTER TABLE table RENAME COLUMN column TO new_column
$mpdo->alterTable('table')->renameColumns([
    'column' => 'new_column',
])->execute();

// ALTER TABLE table DROP COLUMN column
$mpdo->alterTable('table')->dropColumns([
    'column',
])->execute();

//
// Drop Table
//

// DROP TABLE IF EXISTS table
$mpdo->dropTable('table')->checkIfExists()->execute();
```

### Transaction Example

[](#transaction-example-)

```
$transaction = $mpdo->transaction();

$transaction->begin();

try {
    if (!$transaction->isActive()) {
        // Your code...
    }

    // Your code...

    $transaction->commit();
} catch (\Throwable $ex) {
    $transaction->rollBack();

    throw $ex;
}
```

🔧 Running the tests
-----------------------------------------------------

[](#-running-the-tests-)

There are many tools for testing: Integration/Unit tests (PHPUnit), PHPStan, PSalm and CSFixer.
If you want to start them you need to run [composer scripts](#tests_composer_scripts) in terminal

> ***NOTE:*** You can not start integration tests

### Levels

[](#levels)

- PHPStan: Level 9
- PSalm: Level 1

### Composer scripts

[](#composer-scripts-)

#### Tests

[](#tests)

- `composer tests` - runs all PHPUnit tests (do not run it)
- `composer i-tests` - runs integration PHPUnit tests (do not run it)
- `composer u-tests` - runs unit PHPUnit tests
- `composer ca-tests` - defines all tests coverage (do not run it)
- `composer ica-tests` - defines integration tests coverage (do not run it)
- `composer uca-tests` - defines unit tests coverage

#### PHPStan

[](#phpstan)

- `composer stan` - writes errors to phpstan-report.xml
- `composer stan-bl` - writes errors to phpstan-baseline.neon

#### PSalm

[](#psalm)

- `composer salm` - writes errors to psalm-report.xml
- `composer salm-bl` - writes errors to psalm-baseline.xml

#### Coding standards fixer

[](#coding-standards-fixer)

- `composer csfix` - fixes coding standards in all PHP files

⛏️ Built Using
------------------------------------------------------

[](#️-built-using-)

#### Languages

[](#languages)

- [PHP](https://www.php.net/) - version &gt;= 8.1

#### Utilities

[](#utilities)

- [PHPStan](https://github.com/phpstan/phpstan) - static analyzer
- [PSalm](https://github.com/vimeo/psalm) - static analyzer
- [PHPUnit](https://github.com/sebastianbergmann/phpunit) - testing framework
- [PHP-CS-Fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer) - coding standards fixer

✍️ Authors
----------------------------------------------

[](#️-authors-)

- [@StulE-ru](https://github.com/StulE-ru) - Developer
- [@deff-dev](https://github.com/deff-dev) - DevOps

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~47 days

Total

4

Last Release

984d ago

Major Versions

v1.2.1 → v2.0.02023-08-17

v2.0.0 → v3.0.02023-09-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/da230f4f207a0e301194a006b99a3ee30a3add72927c30d13687617438aa7c9f?d=identicon)[StulE-ru](/maintainers/StulE-ru)

---

Top Contributors

[![stul337](https://avatars.githubusercontent.com/u/58220335?v=4)](https://github.com/stul337 "stul337 (247 commits)")[![deff-dev](https://avatars.githubusercontent.com/u/44276186?v=4)](https://github.com/deff-dev "deff-dev (14 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

modern-phpphpphp-databasephp-dbphp-libraryphp-mariadbphp-mysqlphp-pdophp-pgsqlphp-postgresqlphp-sqlitephp-sqlite3php8phppdomodern-phpphp-databasephp mysqlphp-mariadb

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/stule-ru-modernpdo/health.svg)

```
[![Health](https://phpackages.com/badges/stule-ru-modernpdo/health.svg)](https://phpackages.com/packages/stule-ru-modernpdo)
```

###  Alternatives

[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k22.9k](/packages/clouddueling-mysqldump-php)[eftec/pdoone

Minimaist procedural PDO wrapper library

1105.9k9](/packages/eftec-pdoone)[popphp/pop-db

Pop Db Component for Pop PHP Framework

1814.6k11](/packages/popphp-pop-db)[riverside/php-orm

PHP ORM micro-library and query builder

111.2k](/packages/riverside-php-orm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
