ody/database 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ody/database

最新稳定版本:0.2.0

Composer 安装命令:

composer require ody/database

包简介

Database package for ody API framework

README 文档

README

⚠️ IMPORTANT: This repository is automatically generated from the ody repo and is read-only.

A high-performance database integration framework for PHP applications leveraging Swoole's coroutines.

Overview

The ODY Database module provides a connection pool implementation that works with popular PHP ORM solutions. It's designed to maximize performance in Swoole environments by efficiently managing database connections across coroutines.

Features

  • Connection pooling with Swoole coroutine awareness
  • Support for Doctrine ORM, and standalone DBAL
  • Automatic connection binding to coroutines
  • Built-in connection lifecycle management
  • Connection health checks and leak detection
  • Configurable pool size and connection settings

Installation

composer require ody/database

Doctrine ORM

composer require doctrine/orm doctrine/dbal symfony/cache

DBAL

composer require doctrine/dbal

Basic Usage

Configuration

Define your database configuration:

// config/database.php
return [
    'environments' => [
        'local' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'port' => 3306,
            'database' => 'your_database',
            'username' => 'your_username',
            'password' => 'your_password',
            'charset' => 'utf8mb4',
        ],
    ],
    'connection_pool_enabled' => true,
    'pool_size' => 10,
];

Using with Doctrine ORM

use Ody\DB\Doctrine\Facades\ORM;
use App\Entities\User;

// Get entity manager
$entityManager = ORM::entityManager();

// Working with entities
$user = $entityManager->find(User::class, 1);
$entityManager->persist($user);
$entityManager->flush();

Using with Doctrine DBAL

use Ody\DB\Doctrine\Facades\DBAL;

// Execute queries
$users = $this->connection->fetchAllAssociative('SELECT * FROM users WHERE active = ?', [1]);

// Using query builder
$queryBuilder = $this->connection->createQueryBuilder();
$result = $queryBuilder
    ->select('u.*')
    ->from('users', 'u')
    ->where('u.active = :active')
    ->setParameter('active', 1)
    ->executeQuery()
    ->fetchAllAssociative();

Direct Connection Pool Access

use Ody\DB\ConnectionManager;

// Initialize the pool
ConnectionManager::initPool($config);

// Get a connection from the pool
$connection = ConnectionManager::getConnection();

// Use the PDO connection
$stmt = $connection->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([1]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

// No need to return the connection - it's automatically returned when the coroutine ends

Advanced Configuration

The connection pool can be finely tuned with options for:

  • Minimum idle connections
  • Idle timeout
  • Maximum connection lifetime
  • Borrowing timeout
  • Connection health checks
  • Leak detection threshold

For detailed documentation on advanced configuration and usage, refer to the full documentation.

Performance Benefits

  • Connections are reused across requests, eliminating the overhead of establishing new connections
  • Automatic binding to coroutines ensures connection safety in concurrent environments
  • Connection health checks prevent using broken connections
  • Leak detection helps identify and fix connection leaks
  • Configurable pool size matches your application needs and server resources

License

This package is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固