niikunihiro / poulsen
此包已被废弃且不再维护。未建议替代包。
PHP的简单查询构建器
dev-master
2015-06-14 17:39 UTC
Requires
- php: >=5.3.3
- ccampbell/chromephp: 4.*
Requires (Dev)
- mockery/mockery: 0.9.4
- phpunit/phpunit: 4.6.*
This package is auto-updated.
Last update: 2022-02-01 12:47:27 UTC
README
PHP 5.3 运行的查询构建器
要求
- PHP5.3以上
支持
- MySQL
安装
通过Composer安装
{
"require": "niikunihiro/poulsen": "dev-master"
}
用法
在src/Config/database.php中设置访问信息
return array( 'default' => 'mysql', 'connections' => array( 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'username', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ), ), );
基本
生成Builder类实例,通过table()方法指定目标表。
require_once __DIR__ .'/vendor/autoload.php'; use Poulsen\Query\Builder; $DB = new Builder; $query = $DB->table('users');
或者,在实例化时指定表
$query = new Builder('users');
指定了表的Builder类实例后,通过指定获取、插入、更新、删除的方法进行操作。Builder类的方法可以通过链式方法直观地调用。
获取数据
以数组形式获取数据
数组的每个元素都是对象。
$users = $DB->table('users')->get();
获取单条数据
获取的单条数据是对象。
$user = $DB->table('users')->first();
指定列以获取数据
$users = $DB->table('users')->select('id', 'name', 'email AS mail_address')->first();
指定条件
$result = $DB->table('users')->where('name', '=', 'niikunihiro')->get();
指定LIKE条件
$users = $DB->table('users')->where('name', 'LIKE', 'nii%')->get();
指定多个条件
$users = $DB->table('users') ->where('name', 'LIKE', 'nii%') ->orWhere('name', '<>', 'poulsen') ->get();
指定更复杂的条件
将匿名函数传递给where()方法的参数可以创建括号内的条件
$user = $DB->table('users') ->where('name', '=', 'poulsen') ->orWhere(function($query) { $query->whereIn('role_id', array(1, 2, 3)); $query->where('updated_at', '>', '2015-05-08 11:00:00'); }) ->get();
上面的例子会构建以下SQL
SELECT * FROM users WHERE 1 AND name = 'poulsen' OR (role_id IN(1, 2, 3) AND updated_at > '2015-05-08 11:00:00')
表连接
进行表连接时,请使用join()方法。
join()方法的第一参数指定要连接的表名。第二至第四参数指定ON子句的条件。
$comments = $DB->table('articles') ->select('articles.title', 'comments.body AS comment') ->join('comments', 'article_id', '=', 'id') ->get();
如果想要使用外部连接,请将join()方法的第五参数指定为LEFT或RIGHT。
$comments = $DB->table('articles') ->select('articles.title', 'comments.body AS comment') ->join('comments', 'article_id', '=', 'id', 'RIGHT') ->get();
记录插入
通过values()方法设置数据,然后通过insert()方法插入记录。
insert()方法返回插入记录的ID
$now = with(new DateTime)->format('Y-m-d H:i:s'); $id = $DB->table('users') ->values('username', 'poulsen') ->values('email', 'poulsen@example.com') ->values('password', '$2y$10$/L1ScTA7H7KTfS0Josftk.qPeAygzOZOB1GClEkTlMzmmuFw7/Yxa') ->values('created_at', $now) ->values('updated_at', $now) ->insert();
with()函数可用于构造器方法链的快捷方式。
记录更新
通过set()方法设置数据,然后通过update()方法更新记录。
$now = with(new DateTime)->format('Y-m-d H:i:s'); $DB->table('users') ->set('rank', 3) ->set('updated_at', $now) ->update();
指定条件时,请使用where()方法等。
$DB->table('users') ->set('rank', 1) ->set('updated_at', $now) ->where('id', '=', 32) ->update();
记录删除
删除记录时,请使用delete()方法。
※如果不通过where()方法等指定条件,则不会执行删除操作,并返回false。
$DB->table('users') ->where('id', '=', 32) ->delete();
查询日志
为了检查查询日志,内部使用ChromePhp。
如果想要停止日志输出,请将Poulsen\Query\Builder类对象常量ENVIRON的值设置为production。
namespace Poulsen\Query; class Builder { /** 環境 'production'の場合はログを出力しない */ const ENVIRON = 'develop';