定制 jtrw/dao 二次开发

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

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

jtrw/dao

Composer 安装命令:

composer require jtrw/dao

包简介

Data Access Object for fork with DataBases

README 文档

README

Phpunit Codecov Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Data Access Object is tiny wrapper on php PDO. There was add more comfortable methods usage conditions in select query.

Installation

Install via Composer:

composer require jtrw/dao

Requirements

  • PHP >= 7.4
  • PDO extension
  • JSON extension
  • mbstring extension

Quick Start

Basic Setup

<?php
$db = new PDO(
    $GLOBALS['config']['db']['dsn'],
    $GLOBALS['config']['db']['user'],
    $GLOBALS['config']['db']['pass']
);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
$db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); 
$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); 


$res = $db->query('SET NAMES utf8mb4');

if (!$res) {
    throw new Exception('Database connection error');
}

$db = DataAccessObject::factory($db);

API Documentation

Core Methods

Insert

// Insert single record
$id = $db->insert('users', [
    'name' => 'John Doe',
    'email' => 'john@example.com'
]);

// Insert with duplicate key update
$id = $db->insert('users', [
    'name' => 'John Doe',
    'email' => 'john@example.com'
], true);

Update

// Update records
$db->update('users',
    ['name' => 'Jane Doe'],
    ['id' => 1]
);

Delete

// Delete records
$db->delete('users', ['id' => 1]);

Mass Insert

// Insert multiple records
$data = [
    ['name' => 'User 1', 'email' => 'user1@example.com'],
    ['name' => 'User 2', 'email' => 'user2@example.com']
];
$db->massInsert('users', $data);

Select with Conditions

$search = [
       'columnName'     => 5,
       'columnName2&IN' => [1, 2, 3, 4]
       'columnName3&<'  => 7,
       'columnName4&>=' => 3
     ];

$sql = "SELECT * FROM users";
$result = $db->select($sql, $search, [], DataAccessObjectInterface::FETCH_ALL);
$data = $result->toNative(); // Convert to native PHP array

Transaction Support

// Manual transaction handling
$db->begin();
try {
    $db->insert('users', ['name' => 'John']);
    $db->insert('orders', ['user_id' => $db->getInsertID(), 'total' => 100]);
    $db->commit();
} catch (Exception $e) {
    $db->rollback();
    throw $e;
}

Utility Methods

// Get tables list
$tables = $db->getTables();

// Quote table/column names
$quotedTable = $db->quoteTableName('user_data');
$quotedColumn = $db->quoteColumnName('user-name');

// Get database type
$dbType = $db->getDatabaseType(); // mysql, pgsql, etc.

// Check transaction status
if ($db->inTransaction()) {
    // Inside transaction
}

Search Conditions

key value result
- 'column = 5' column = 5
col&<action> 'item' col <action> 'item'
column 5 column = '5'
column null column IS NULL
column&IN 'val1, val2, val3' column IN ('val1', 'val2', 'val3')
column&IN array('val1', 'val2', 'val3') column IN ('val1', 'val2', 'val3')
column&NOT IN 'val1, val2, val3' column NOT IN ('val1', 'val2', 'val3')
column&NOT IN array('val1', 'val2', 'val3') column NOT IN ('val1', 'val2', 'val3')
sql_or array('col1 = 5', 'col2 = 8') ((col1 = 5 ) OR (col2 = 8))
sql_or array(array('col1' => 5), array('col2' => 8, 'col3' => 7)) ((col1 = 5) OR (col2 = 8 AND col3 = 7))
sql_and array('col1 = 5', 'col2 = 8') col1 = 5 AND col2 = 8
sql_and array(array('col1' => 5), array('col2' => 8, 'col3' => 7)) col1 = 5 AND col2 = 8 AND col3 = 7
something&or_sql array('col1 = 5', 'col2 = 8') (col1 = 5 OR col2 = 8)
col&or array('val1', array('col2' => 5, 'col3' => 8)) (col = 'val1' OR col2 = '5' OR col3 = '8')
col&or&>= array(7, array('col2' => 5, 'col3' => 8)) (col >= 7 OR col2 = '5' OR col3 = '8')
col&match 'something' MATCH (col) AGAINST ('something')
col&between array(3) "col" >= '3'
col&between array(1 => 7) "col" <= '7'
col&between array(3, 7) "col" BETWEEN '3' AND '7'
col&between '3 AND 7' "col" BETWEEN 3 AND 7
col&soundex 'val1' SOUNDEX(col) = SOUNDEX('val1')

Env

make install

make start

make stop

Unittest

php ./vendor/phpunit/phpunit/phpunit -c ./tests/phpunit.xml --testdox --stderr --colors

make tests - run all tests with migrations

make run-tests - run all tests without migrations

Troubleshooting

Common Issues

Connection Problems

// Ensure proper PDO configuration
$pdo = new PDO($dsn, $username, $password, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
]);

Transaction Deadlocks

  • Use shorter transactions
  • Always handle exceptions in transactions
  • Consider using SELECT ... FOR UPDATE for critical sections

Performance Tips

  • Use prepared statements for repeated queries
  • Consider using massInsert() for bulk operations
  • Use appropriate indexes on search columns

Supported Databases

  • MySQL/MariaDB: Full support with MySQL-specific features
  • PostgreSQL: Full support with PostgreSQL-specific features
  • SQL Server: Supported via MSSQL driver

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass: make tests
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

jtrw/dao 适用场景与选型建议

jtrw/dao 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.99k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「db」 「dao」 「data access object」 「data base」 「nilBora」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jtrw/dao 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jtrw/dao 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 6.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 11
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-05