cry/cry-cms-db
Composer 安装命令:
composer require cry/cry-cms-db
包简介
PHP Class for working with MySQL via PDO
README 文档
README
PHP Class for working with MYSQL via PDO.
Write queries or use methods.
Singleton.
Setup
Db::config([ 'host' => 'localhost', 'user' => 'test', 'password' => 'test', 'database' => 'test', ]);
Debug mode
Db::debug(true);
Two ways of queries
Native SQL query (also with placeholders)
Db::sql()->query('query', [])->exec(); Db::sql()->query('query', [])->getOne(); Db::sql()->query('query', [])->getAll();
Via builder
$result = Db::table('table')->getOne(); $result = Db::table('table')->getAll();
Examples
Create table
Db::table('table')->create([ 'id' => 'INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'name' => 'VARCHAR(255)', 'date' => 'DATETIME', ], 'ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COMMENT="TEST"');
Add index
Db::table('table')->index(['id', 'date'], 'UNIQUE');
Truncate table
Db::table('table')->truncate();
Drop table
Db::table('table')->drop();
Get list of table fields
Db::table('table')->fields();
Queries log (debug=true only)
$log = Db::getLog();
Get query instead of execute
replace methods getOne() or getAll() to getSQL()
Insert
Db::table('table')->insert([ 'name' => 'first', 'date' => date('Y-m-d'), ]);
Get autoincrement after insert
$id = Db::lastInsertId();
Get one row - case 1
$one = Db::table('table') ->select(['id', 'name', 'date']) ->where(['id = :id']) ->values(['id' => $id]) ->getOne();
Get one row - case 2
$one = Db::table('table') ->select(['id', 'name', 'date']) ->where(['id' => 1]) ->getOne();
Get list of rows
$all = Db::table('table', 't') ->select(['t.id', 'td.field_1', 'td.field_2']) ->calcRows() ->leftJoin('testData', 'td', 'td.test_id = t.id') ->where(['t.date <= :date']) ->values(['date' => date('Y-m-d')]) ->offset(0) ->limit(5) ->groupBy(['t.id']) ->orderBy(['t.id' => 'DESC']) ->getAll();
Get count of rows (with calcRows only)
$count = Db::getFoundRows();
Update - case 1
Db::table('table')->update([ 'name' => 'NAME2' ], [ 'id' => $id ]);
Update - case 2
Db::table('table')->update([ 'name' => 'NAME3', ], [ 'id = :id', // array of SQL with placeholders ], [ 'id' => $id, ]);
Delete - case 1
Db::table('table')->delete([ 'id' => $id ]);
Delete - case 2
Db::table('table')->delete([ 'date <= :date', // array of SQL with placeholders ], [ 'date' => date('Y-m-d') ]);
New singlenton instance with different connection (to other MySQL, for example)
use CryCMS\Db; class Db2 extends Db { protected static $config; protected static $dbh; protected static $debug = false; protected static $log = []; } Db2::config( [ 'host' => '', 'user' => '', 'password' => '', 'database' => '', ] );
cry/cry-cms-db 适用场景与选型建议
cry/cry-cms-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 213 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cry/cry-cms-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cry/cry-cms-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 213
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-31