项目地址
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