roolith / database
PHP数据库驱动
1.0.3
2021-09-29 10:09 UTC
Requires
- ext-pdo: *
Requires (Dev)
- fzaninotto/faker: ^1.9
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-08-29 05:37:06 UTC
README
PHP数据库驱动
安装
composer require roolith/database
使用
use Roolith\Store\Database; $db = new Database(); $db->connect([ 'host' => 'host', 'name' => 'dbname', 'user' => 'username', 'pass' => 'password', ]); // Get all users $users = $db->query("SELECT * FROM users")->get(); print_r($users); // Get all usernames $usernames = $db->table('users')->select([ 'field' => 'name', ])->get(); print_r($usernames); // Disconnect $db->disconnect();
选择
$db->query("SELECT * FROM users")->get();
$db->table('users')->select([ 'field' => ['name', 'email'], 'condition' => 'WHERE id > 0', 'limit' => '0, 10', 'orderBy' => 'name', 'groupBy' => 'name', ])->get();
插入
$result = $db->table('users')->insert( ['name' => 'Brannon Bruen', 'email' => 'bschmeler@pacocha.net'] ); print_r($result->success());
当提供的电子邮件 john@email.com
不在 users
表中时插入数据
$result = $db->table('users')->insert( ['name' => 'John doe', 'email' => 'john@email.com'], ['email'] );
响应
$result->affectedRow(); $result->insertedId(); $result->isDuplicate(); $result->success();
更新
$result = $db->table('users')->update( ['name' => 'Habib Hadi', 'email' => 'john@email.com'], ['id' => 1] );
或者
$result = $db->table('users')->update( ['name' => 'Habib Hadi', 'email' => 'john@email.com'], 'id = 1' );
如果没有人使用相同的用户名,则更新用户名
$result = $db->table('users')->update( ['username' => 'johndoe'], ['id' => 4], ['username'] );
响应
$result->affectedRow(); $result->isDuplicate(); $result->success();
删除
$result = $db->table('users')->delete(['id' => 4]);
响应
$result->affectedRow(); $result->success();
连接
$db = new Database(); $db->connect([ 'host' => 'host', 'name' => 'dbname', 'user' => 'username', 'pass' => 'password', ]);
或者
$db = new Database([ 'host' => 'host', 'name' => 'dbname', 'user' => 'username', 'pass' => 'password', ]);
断开连接
$db->disconnect();
其他
使用 LIKE
运算符搜索用户表
$db->table('users')->where('name', '%Hadi%', 'LIKE')->get();
通过 id 1 获取用户
$db->table('users')->find(1);
从用户表中提取名称和电子邮件
$db->table('users')->pluck(['name', 'email']);
获取用户表的总记录数
$db->query("SELECT id FROM users")->count();
分页
$total = $db->query("SELECT id FROM users")->count(); $result = $db->query("SELECT * FROM users")->paginate([ 'perPage' => 5, 'pageUrl' => 'http://domain.com', 'primaryColumn' => 'id', 'pageParam' => 'page', 'total' => $total, ]);
或者
$total = $db->query("SELECT id FROM users")->count(); $result = $db->query("SELECT * FROM users")->paginate([ 'perPage' => 5, // default 20 'total' => $total, ]);
print_r($result->getDetails());
{
"total": 50,
"perPage": 15,
"currentPage": 1,
"lastPage": 4,
"firstPageUrl": "http://domain.com?page=1",
"lastPageUrl": "http://domain.com?page=4",
"nextPageUrl": "http://domain.com?page=2",
"prevPageUrl": null,
"path": "http://domain.com",
"from": 1,
"to": 15,
"data":[
// records
]
}
调试模式
$db->debugMode()->table('users')->find(1);
注意:一旦调试模式激活,它将显示查询字符串!
开发
PHPUnit 9.3.7 by Sebastian Bergmann and contributors.
Database
✔ Should construct with config
✔ Should construct without config
✔ Should connect
✔ Should disconnect
✔ Should allow raw query
✔ Should return first result
✔ Should select
✔ Should insert
✔ Should insert if record not exists
✔ Should update
✔ Should update if record not exists
✔ Should delete
✔ Should get result based on where
✔ Should get result by find
✔ Should pluck by field name
✔ Should paginate
Paginate
✔ Should get count
✔ Should get total
✔ Should get total page
✔ Should get current page
✔ Should get first item
✔ Should get last item
✔ Should get items
✔ Should get first page url
✔ Should get last page url
✔ Should get next page url
✔ Should get prev page url
✔ Should get page numbers
✔ Should get limit
✔ Should get offset
✔ Should get details
Time: 00:00.144, Memory: 6.00 MB
OK (31 tests, 41 assertions)