2017 © Pedro Peláez
 

library d2o

image

knj/d2o

  • Saturday, September 24, 2016
  • by KNJ
  • Repository
  • 1 Watchers
  • 1 Stars
  • 42 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

D2O

Packagist Pre Release Build Status, (*1)

D2O-chan, (*2)

Installation

Use Composer., (*3)

composer require knj/d2o

D2O from PDO

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

$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:, (*5)

$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

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., (*6)

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

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., (*7)

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

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., (*8)

$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 )

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., (*9)

$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 )

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., (*10)

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

getStatement()

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

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

Differences between D2O and PDO

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., (*12)

Example of sequential insertion:, (*13)

$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

D2O saves coding when you bind values on placeholders:, (*14)

$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()., (*15)

The Versions

24/09 2016

dev-dev

dev-dev

  Sources   Download

MIT

The Development Requires

by Avatar KNJ

10/05 2016

dev-master

9999999-dev

  Sources   Download

MIT

The Development Requires

by Avatar KNJ

10/05 2016

0.5.0

0.5.0.0

  Sources   Download

MIT

The Development Requires

by Avatar KNJ

22/02 2016

0.4.0

0.4.0.0

  Sources   Download

MIT

The Development Requires

by Avatar KNJ

22/02 2016

dev-image

dev-image

  Sources   Download

MIT

The Development Requires

by Avatar KNJ

11/02 2016

0.3.0

0.3.0.0

  Sources   Download

MIT

The Development Requires

by Avatar KNJ

09/02 2016

0.2.1

0.2.1.0

  Sources   Download

MIT

The Development Requires

by Avatar KNJ

06/02 2016

0.2.0

0.2.0.0

  Sources   Download

MIT

by Avatar KNJ

04/02 2016

0.1.0

0.1.0.0

  Sources   Download

MIT

by Avatar KNJ