項目地址
https://github.com/envms/fluentpdo
創建PDO實例
$dsn = 'mysql:host=localhost;dbname=test;charset=utf8';
$username = 'username';
$password = 'password';
$pdo = new PDO($dsn, $username, $password);
創建FPDO實例
$fPdo = new FluentPDO($pdo);
數據庫操作
查詢
$query = $fpdo->from('article')->where('id', 1);
// or shortly if you select one row by primary key
$query = $fpdo->from('user', 1);
插入
$values = array('title' => 'article 1', 'content' => 'content 1');
$query = $fpdo->insertInto('article')->values($values)->execute();
// or shortly
$query = $fpdo->insertInto('article', $values)->execute();
更新
$set = array('published_at' => new FluentLiteral('NOW()'));
$query = $fpdo->update('article')->set($set)->where('id', 1)->execute();
// or shortly if you update one row by primary key
$query = $fpdo->update('article', $set, 1)->execute();
刪除
$query = $fpdo->deleteFrom('article')->where('id', 1)->execute();
// or shortly if you delete one row by primary key
$query = $fpdo->deleteFrom('article', 1)->execute();
注意:
INSERT
,UPDATE
andDELETE
將會在->execute()
之後執行
高級一點的
leftjoin
$query = $fpdo->from('article')
->leftJoin('user ON user.id = article.user_id')
->select('user.name');
如果有主鍵、外鍵等,可以簡化成
$query = $fpdo->from('article')->leftJoin('user')->select('user.name');
或者
$query = $fpdo->from('article')->select('user.name');
以上三條命令均等價於
SELECT article.*, user.name
FROM article
LEFT JOIN user ON user.id = article.user_id