Wallogit.com
2017 © Pedro Peláez
A drop-in PDO wrapper that provides smooth and pleasant method chaining
A drop-in PDO wrapper that provides smooth and pleasant method chaining., (*1)
PDO is a perfectly fine way to access a database, but it would be a little more pleasant to work with if all the methods could be chained together. Unfortunately, you can't do that with PDO since it uses boolean return values to indicate errors. This just overrides the methods that use return values to indicate errors so they throw exceptions instead., (*2)
composer require royallthefourth/smooth-pdo, (*3)
Now create a SmoothPdo\DataObject from a \PDO and you're all set:, (*4)
new RoyallTheFourth\SmoothPdo\DataObject(\PDO $db)
A typical example from the PDO documentation looks like this:, (*5)
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
Here's how it looks with SmoothPdo\DataObject:, (*6)
<?php
$calories = 150;
$colour = 'red';
$dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour')
->bindParam(':calories', $calories, PDO::PARAM_INT)
->bindParam(':colour', $colour, PDO::PARAM_STR, 12)
->execute();
This library works almost exactly like PDO.
Just beware that these methods now throw exceptions instead of returning false:
* PDO::beginTransaction
* PDO::commit
* PDO::rollback
* PDO::setAttribute
* PDOStatement::bindColumn
* PDOStatement::bindParam
* PDOStatement::bindValue
* PDOStatement::closeCursor
* PDOStatement::execute, (*7)
If you encounter any problems or notice any other divergence from PDO, please don't hesitate file an issue., (*8)