mavi/dbmanager
Composer 安装命令:
composer require mavi/dbmanager
包简介
Manager for repository tables.
README 文档
README
Manager for repository tables.
Manager is using PDO (PDO is enabled in PHP by default).
Using PHP 8.1.
Init
$dbManager = new DBManager('host', 'db', 'user', 'password');
Selecting data
$result = $dbManager->table('users')
->select('users.id, users.name, users.phone')
// or ->select(['users.id', 'users.name', 'users.phone'])
->where('users.name LIKE ? AND users.id > ?', 'Ja%', 1)
// or ->where(['users.name LIKE ?', 'users.id > ?'], 'Ja%', 1)
->orderBy('users.name DESC, users.id ASC')
// or ->orderBy(['users.name DESC', 'users.id ASC'])
->limit(2, 0)
->rightJoin('orders')
->on('users.id = orders.user_id')
->select('price, date')
->endJoin($dbManager)
->fetchAll();
Joining tables
Tables can be joined composittely:
->innerJoin('orders')
->on('user.id = orders.id_uzivatele')
->select('orders.products, orders.date')
->innerJoin('products')
->on('order.product_id = product.id')
->select('price, vat')
->endJoin($dbManager)
Join Funcions:
->innerJoin(...)
->leftJoin(...)
->rightJoin(...)
->fullJoin(...)
Fetching data
->fetchSingle(); // Returns single value
$result = $dbManager->table('users')->select('id')->where(...)->fetchSingle(); // Returns 'id'
$result = $dbManager->table('users')->select('id, name, phone')->where(...)->fetchSingle(); // Returns 'id'
$result = $dbManager->table('users')->select('id, name, phone')->where(...)->fetchSingle(1); // Returns 'name'
$result = $dbManager->table('users')->select('id, name, phone')->where(...)->fetchSingle(2); // Returns 'phone'
->fetch(); // Returns single row as array
->fetchAll(); // Returns array of rows with values as associative array
->fetchPairs(); // Returns rows associated with unique key, values in a rows are associative array
/*
* fetchPairs example:
* [
* 2 => [id => 2, name => ... ]
* 5 => [id => 5, name => ...]
* ]
*/
Cashing results
Results can be cached. If the same queries are executed, DBManager will load cached results.
$dbManager->enableCashing($numberOfMaxCachedResults);
- Default maximum of cached results is 20.
- If the limit is exausted, first cached result is removed and results shifted.
- Cashing is effective for tables with many records.
Inserting data
$dbManager->beginTransaction();
try {
$id = $dbManager->table('users')->insert(['name' => 'Karl', 'email' => 'karl@example.com'])->getId();
$dbManager->table('orders')->insert(['user_id' => $id, 'price' => 1000, 'currency' => 'CZK', 'date%sql' => 'NOW()']);
$dbManager->commit();
} catch (\Exception $e) {
$dbManager->rollback();
}
Updating data
Updates phone for user which his name begins with Kar* and date of birth is 3.2.1991.
$dbManager->table('users')->where('name LIKE ? AND birth_date = ?', 'Kar%', '1991-02-03')->update(['phone' => '+420 555 555 555']);
- 'Kar%' - string that begins with 'Kar'.
- '%arl' - string that ends with 'arl'.
- '%ar%' - string that contains 'ar'.
mavi/dbmanager 适用场景与选型建议
mavi/dbmanager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mavi/dbmanager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mavi/dbmanager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-19