Powerful and thin wrapper of PDO
- Fluent Query Builder
- Supports POPO(Plain Old Php Object) entity class
- Supports SQL JOIN
- PSR-3 Logger
class UserEntity
{
public $ID;
public $deleted;
public $user_name;
public $nickname;
public $email;
}
use Stk2k\PowerPDO\PowerPDO;
$dsn = 'mysql:dbname=mydatabase;host=localhost';
$user = 'myuser';
$password = 'mypass';
$ppdo = new PowerPDO(new PDO($dsn, $user, $password));
or
$ppdo = PowerPDO::make($dsn, $user, $password);
use Stk2k\PowerPDO\PowerPDO;
$dsn = 'sqlite:/path/to/dbfile_of_sqlite';
$ppdo = new PowerPDO(new PDO($dsn));
or
$ppdo = PowerPDO::make($dsn);
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// monolog
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
$ppdo = new PowerPDO(new PDO($dsn), $log);
or
$ppdo = PowerPDO::make($dsn)->logger($log);
// array style
$users = PowerPDO::make($dsn)
->select("ID, user_name, nickname, email")
->from("users")
->where("deleted = 0")
->getAll();
foreach($users as $u){
$uid = $u['ID'];
$name = $u['user_name'];
echo "[$uid]$name";
}
// entity style
$users = PowerPDO::make($dsn)
->select("ID, user_name, nickname, email")
->from("users")
->where("deleted = 0")
->getAll(UserEntity::class);
foreach($users as $u){
$uid = $u->ID;
$name = $u->user_name;
echo "[$uid]$name";
}
$users = PowerPDO::make($dsn)
->select("ID, user_name, nickname, email")
->from("users")
->where("nickname LIKE :nickname")
->bind(['nickname' => '%Bi%'])
->getAll();
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$users = PowerPDO::make($dsn)
->count()
->from("users")
->where("deleted = 0")
->get();
use Stk2k\PowerPDO\Transaction;
use Stk2k\PowerPDO\exception\TransactionException;
try{
$tr = new Transaction($pdo);
$tr->begin();
// execute UPDATE/DELETE/INSERT SQL here
$tr->commit();
}
catch(TransactionException $ex){
$tr->rollback();
}
// array style
PowerPDO::make($dsn)
->insert()
->into("users", "ID, user_name, nickname, email")
->values(
"ID" => "123",
"user_name" => "hanako",
"nickname" => "hana",
"email" => "hanako@sample.com"
)
->execute();
// entity style
$new_user = new UserEntity();
$new_user->ID = 123;
$new_user->user_name = 'hanako';
$new_user->nickname = 'hana';
$new_user->email = 'hanako@sample.com';
PowerPDO::make($dsn)
->insert()
->into("users")
->values($new_user)
->execute();
// literal style
PowerPDO::make($dsn)
->update("users")
->set("user_name", "hanako2")
->set("email", "hanako2@sample.com")
->where("ID = :ID", ['ID'=>1])
->execute();
// array style
PowerPDO::make($dsn)
->update("users")
->values([
'user_name' => 'hanako2',
'email' => 'hanako2@sample.com',
])
->where("ID = :ID", ['ID'=>1])
->execute();
// entity style
$new_user = new UserEntity();
$new_user->user_name = 'hanako2';
$new_user->nickname = 'hana2';
$new_user->email = 'hanako2@sample.com';
PowerPDO::make($dsn)
->update("users")
->values($new_user)
->where("ID = :ID", ['ID'=>1])
->execute();
use Stk2k\PowerPDO\PowerPDO;
// literal style
PowerPDO::make($dsn)
->delete()
->from("users")
->where("email = 'hanako2@sample.com'")
->execute();
// placeholder style
PowerPDO::make($dsn)
->delete()
->from("users")
->where("email = :email",['email' => 'hanako2@sample.com'])
->execute();
PHP 7.3 or later
The recommended way to install stk2k/power-pdo is through Composer.
composer require stk2k/power-pdo
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
This library is licensed under the MIT license.
This software is no warranty.
We are not responsible for any results caused by the use of this software.
Please use the responsibility of the your self.