定制 cry/cry-cms-db 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 cry/cry-cms-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 213
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-31