Wallogit.com
2017 © Pedro Peláez
, (*2)
Use Composer., (*3)
composer require knj/d2o
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);
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
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
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
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();
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();
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();
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]);
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
]);
PDO_PARAM_* when the value is string, integer, or null'str' instead of PDO::PARAM_STR
And D2O never calls PDOStatement::bindParam()., (*15)