PHPackages                             safronik/db-wrapper - 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. safronik/db-wrapper

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

safronik/db-wrapper
===================

Wrapper for different relation databases drivers such as PDO, Mysqli, WPDB, Drupal, and any other

0.1.2(1y ago)025MITPHPPHP &gt;=8.0

Since Jul 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/safronik/project-db-wrapper)[ Packagist](https://packagist.org/packages/safronik/db-wrapper)[ Docs](https://github.com/Safronik/project-db-wrapper)[ RSS](/packages/safronik-db-wrapper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

safronik/db-warapper
====================

[](#safronikdb-warapper)

 **A PHP library contains database wrapper to simplify it's usage**

About
=====

[](#about)

This package contains wrapper for database, includes drivers for different databases:

- PDO
- mysqli (in development)
- Wordpress
- Joomla (in development)
- Drupal (in development)
- ...

There is a namespace "Extensions" contains extensions for this DB wrapper. And few extensions to ease the programmer life:

- Query builder
- Operations with tables
- Placeholders for input data (works natively, you want see it)
- Server side prepared extensions (works only for PDO)
- SQL Schema (in development)

Installation
============

[](#installation)

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's `composer.json`:

```
composer require safronik/db-warapper
```

or just download files or clone repository (in this case you should bother about autoloader)

Usage
=====

[](#usage)

Creating a connection
---------------------

[](#creating-a-connection)

If you are using PDO (you don't have any ready connection)

```
$db = DB::getInstance(
    new \Safronik\DB\DBConfig([
        'driver'   => 'pdo',
        'username' => 'root',
        'password' => 'root',
        'hostname' => 'localhost', // or could be a container name if you are using Docker
    ])
);
```

Existing PDO connection:

```
global $some_pdo_connection_object; // should be an instanceof \PDO

$db = DB::getInstance(
    new \Safronik\DB\DBConfig([
        'driver'     => 'pdo',
        'connection' => $some_pdo_connection_object,
    ])
);
```

Because it's driver is PDO by default this should work too:

```
global $some_pdo_connection_object; // should be an instanceof \PDO

$db = DB::getInstance(
    new \Safronik\DB\DBConfig([
        'connection' => $some_pdo_connection_object,
    ])
);
```

For WordPress:

```
$global $wpdb;

$db = DB::getInstance(
    new \Safronik\DB\DBConfig([
        'driver'     => 'wpdb',
        'connection' => $wpdb,
    ])
);
```

Raw query
---------

[](#raw-query)

```
$rows_affected = $db->query( 'DELETE FROM some_table LIMIT 10' );
```

```
$query_result = $db
    ->query( 'SELECT * FROM some_table' ) // Query already executed at this point
    ->fetchAll();                         // Fetching the result
```

Query builder
-------------

[](#query-builder)

Builder using a fluid (waterfall) interface

### Select

[](#select)

Methods allowed:

- table
- columns
- join (look for Join description below)
- groupBy (in development)
- having (in development)
- orderBy
- limit
- with (look for CTE description below)
- run

```
$db
    ->select('users')
    ->orderBy('register_date', 'desc')
    ->limit(10)
    ->run();
```

### Insert

[](#insert)

Methods allowed:

- columns
- ignore
- values
- onDuplicateKey
- run

```
$values = [
    'some'    => 'thing',
    'another' => 'stuff',
]

$db
    ->insert( 'some_entity' )
    ->columns( array_keys( $values ) )
    ->values( $values )
    ->onDuplicateKey( 'update', $values )
    ->run();
```

### Update

[](#update)

Methods allowed:

- set
- where
- and
- or
- run

```
$db
    ->update( 'options' )
    ->set( [ 'option_value' => $value ] )
    ->where([
        'option_name' => $option,
        'affiliation' => $group,
    ])
    ->and([ 'something' => 'different'])
    ->or( ['another' => 'example'])
    ->run()
```

### Delete

[](#delete)

Methods allowed:

- where
- and
- or
- orderBy
- limit
- run

```
$db
    ->update( 'options' )
    ->set( [ 'option_value' => $value ] )
    ->where([
        'option_name' => $option,
        'affiliation' => $group,
    ])
    ->run()
```

### Join (only as part of Select statement)

[](#join-only-as-part-of-select-statement)

- Supports left, right and inner joins passed as the second argument
- Join operator supports &lt;=&gt; | != | &gt; | &lt; | &gt;= | &lt;= but it's not certain =D
- All columns from the joined table will be selected if no specified

```
$db
    ->select('users')
    ->join(
        [
            [ 'table_name_or_alias', 'id' ],
            '=', //  | != | > | < | >= | limit(10)
    ->run();
```

### CTE (Common Table Expression)

[](#cte-common-table-expression)

When you call with() you should pass the $db-&gt;cte() inside

So the all 3 methods ( `cte()`, `anchor()` and `recursive()` ) should be called

`cte()` set the name of your common table expression. You can think that it's a table name.

`anchor()` is the root select expression

`recursive()` recursive expression

Any select expression could be any level of difficulty, using joins, orders and other

```
$db
    ->select( 'cte' )
    ->with(
        $db
            ->cte( 'cte' )
            ->anchor( 'SELECT * FROM some_table WHERE id = 1' )
            ->recursive( '
                SELECT some_table.*
                FROM some_table, cte
                WHERE some_table.parent = cte.id'
            )
        )
    )
    ->run();
```

You can also use a query builder in these `anchor` and `recursive` expressions

```
$db
    ->select( 'cte' )
    ->with(
        $db
            ->cte( 'cte' )
            ->anchor(
                $db
                    ->select( $block_table )
                    ->where( [ 'id' => 1 ] )
            )
            ->recursive(
                $db
                    ->select( [ $block_table, 'cte' ])
                    ->columns( '*', $block_table )
                    ->where( [ [ [ $block_table, 'parent' ], '=', [ 'cte', 'id' ] ] ] )
        )
    )
    ->run();
```

Tables operations
-----------------

[](#tables-operations)

### Exists

[](#exists)

Checks if the table exists

```
$db->isTableExists( 'table_name');
```

### Drop

[](#drop)

Drops a table

```
$db->dropTable( 'table_name');
```

### Create

[](#create)

Some day I will add the documentation

### Alter

[](#alter)

Some day I will add the documentation

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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 ~124 days

Total

2

Last Release

540d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c69e7608cd7b2fe530ef65731dc64b353ece983e732764a99d0155beac3275d9?d=identicon)[Safronik](/maintainers/Safronik)

---

Top Contributors

[![safronik](https://avatars.githubusercontent.com/u/16255344?v=4)](https://github.com/safronik "safronik (10 commits)")

---

Tags

schemamigrationdatabase

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/safronik-db-wrapper/health.svg)

```
[![Health](https://phpackages.com/badges/safronik-db-wrapper/health.svg)](https://phpackages.com/packages/safronik-db-wrapper)
```

###  Alternatives

[odan/phinx-migrations-generator

Migration generator for Phinx

235847.8k23](/packages/odan-phinx-migrations-generator)[cybercog/laravel-clickhouse

ClickHouse migrations for Laravel

163166.8k](/packages/cybercog-laravel-clickhouse)[dragon-code/laravel-data-dumper

Adding data from certain tables when executing the `php artisan schema:dump` console command

3418.6k](/packages/dragon-code-laravel-data-dumper)[concretecms/doctrine-xml

Define database structure via XML using Doctrine data types

11100.8k2](/packages/concretecms-doctrine-xml)

PHPackages © 2026

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