PHPackages                             knj/d2o - 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. knj/d2o

ActiveLibrary

knj/d2o
=======

0.5.0(10y ago)153MITPHP

Since Feb 4Pushed 9y ago1 watchersCompare

[ Source](https://github.com/KNJ/d2o)[ Packagist](https://packagist.org/packages/knj/d2o)[ RSS](/packages/knj-d2o/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (9)Used By (0)

D2O
===

[](#d2o)

[![Packagist Pre Release](https://camo.githubusercontent.com/eeb2feee14a8111ad8e6febc99eaf901e2ad71968b0b8c2f2d815ace1a95aa8a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6b6e6a2f64326f2e737667)](https://packagist.org/packages/knj/d2o)[![Build Status](https://camo.githubusercontent.com/4da285250e0b8c43dca2491a70d907dacde983c8c953f3faba5e47a9feeb7431/68747470733a2f2f7472617669732d63692e6f72672f4b4e4a2f64326f2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/KNJ/d2o)

[![D2O-chan](img/d2o.png)](img/d2o.png)

Installation
------------

[](#installation)

Use Composer.

```
composer require knj/d2o
```

D2O from PDO
------------

[](#d2o-from-pdo)

If D2O, you can use PDO and PDOStatement with only 1 statement using method chaining:

```
$d2o = new Wazly\D2O($dsh, $username, $password);

$sql = 'SELECT * FROM users WHERE id = :id'

$row = $d2o
    ->state($sql)
    ->run([':id' => 3])
    ->pick();
```

otherwise need to `$dbh` as PDO and `$stmt` as PDOStatement and at least 3 statements are required:

```
$dbh = new PDO($dsh, $username, $password);

$sql = 'SELECT * FROM users WHERE id = :id';

$stmt = $dbh->prepare($sql);
$stmt->execute([':id' => 3]);
$row = $stmt->fetch(PDO::FETCH_OBJ);
```

Methods
-------

[](#methods)

### state ( string $statement )

[](#state--string-statement-)

`D2O::state()` is to be called by D2O directly. It is almost the same as `PDO::prepare()` but returns D2O instance itself.

```
$d2o->state($sql); // returns $d2o
```

### bind ( \[ array $parameters \[, string $bind\_type = 'value' \] \] )

[](#bind---array-parameters--string-bind_type--value---)

`D2O::bind()` is to be called after `D2O::state()`. It is similar to `PDOStatement::bindValue()` but returns D2O instance.

```
$d2o->state($sql)
    ->bind([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ]); // returns $d2o
```

### run ( \[ array $parameters \[, string $bind\_type = 'value' \] \] )

[](#run---array-parameters--string-bind_type--value---)

`D2O::run()` is to be called after `D2O::state()`. It is similar to `PDOStatement::execute()` but returns D2O instance.

```
$d2o->state($sql)
    ->bind([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ])
    ->run(); // returns $d2o

$d2o->state($sql)
    ->run([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ]); // the same as the above
```

### pick ( string $fetch\_style )

[](#pick--string-fetch_style-)

`D2O::pick()` is to be called after `D2O::run()`. It is almost the same as `PDOStatement::fetch()`. It does not return the instance but query result.

```
$row = $d2o->state($sql)
    ->run([
        ':role' => $role,
        ':limit' => [1, 'int'],
    ])
    ->pick(); // recommended if the number of rows is supposed to be 1

$result = $d2o->state($sql)
    ->run([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ]); // recommended if the number of rows is supposed to be 2 or more

$row1 = $result->pick();
$row2 = $result->pick();
$row3 = $result->pick();
```

### format ( string $fetch\_style )

[](#format--string-fetch_style-)

`D2O::format()` is to be called after `D2O::run()`. It is almost the same as `PDOStatement::fetchAll()`. It does not return the instance but query result.

```
$rows = $d2o->state($sql)
    ->run([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ])
    ->format();
```

### getStatement()

[](#getstatement)

`D2O::getStatement()` returns PDOStatement object in D2O. To give an example, you can use `PDOStatement::fetchAll()` with it instead of `D2O::format()`.

```
$rows = $d2o->state($sql)->run()->getStatement()->fetchAll();
```

Differences between D2O and PDO
-------------------------------

[](#differences-between-d2o-and-pdo)

### PDOStatement lies inside of D2O

[](#pdostatement-lies-inside-of-d2o)

Using PDO, extra variable is required to contain PDOStatement instance. However, D2O has PDOStatement as its property so that it can provide method chaining.

*Example of sequential insertion*:

```
$d2o->state('INSERT INTO items(name, price) VALUES (:name, :price)')
    ->run(['name' => 'pencil', 'price' => 20])
    ->run(['name' => 'eraser', 'price' => 60])
    ->run(['name' => 'notebook', 'price' => 100]);
```

### Data binding

[](#data-binding)

D2O saves coding when you bind values on placeholders:

```
$d2o->bind([
    'role' => 'editor',  // ':role' => 'editor', PDO::PARAM_STR
    'limit' => 20,       // ':limit' => 20, PDO::PARAM_INT
    'hash' => '6293',    // ':hash' => '6293', PDO::PARAM_STR
    'id' => [36, 'str'], // ':id' => 36, PDO::PARAM_STR
]);
```

- You don't have to use colons
- You don't have to `PDO_PARAM_*` when the value is string, integer, or null
- You can specify data type more concisely, like `'str'` instead of `PDO::PARAM_STR`

And D2O never calls `PDOStatement::bindParam()`.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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 ~19 days

Total

6

Last Release

3657d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/226892?v=4)[KNJ](/maintainers/KNJ)[@KNJ](https://github.com/KNJ)

---

Top Contributors

[![KNJ](https://avatars.githubusercontent.com/u/226892?v=4)](https://github.com/KNJ "KNJ (55 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/knj-d2o/health.svg)

```
[![Health](https://phpackages.com/badges/knj-d2o/health.svg)](https://phpackages.com/packages/knj-d2o)
```

PHPackages © 2026

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