pivotphp/cycle-orm 问题修复 & 功能扩展

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

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

pivotphp/cycle-orm

Composer 安装命令:

composer require pivotphp/cycle-orm

包简介

Robust and well-tested Cycle ORM integration for PivotPHP microframework with type safety and comprehensive testing

README 文档

README

PHP Version License Latest Stable Version PHPStan Tests

Robust and well-tested Cycle ORM integration for PivotPHP microframework

🚀 Features

  • Seamless Integration: Deep integration with PivotPHP Core
  • Type Safety: Full type safety with PHPStan Level 9
  • Repository Pattern: Built-in repository pattern support
  • Performance Monitoring: Query logging and performance profiling
  • Middleware Support: Transaction and validation middleware
  • Health Checks: Database health monitoring
  • Zero Configuration: Works out of the box with sensible defaults

📦 Installation

composer require pivotphp/cycle-orm

Development Setup

When developing locally with both pivotphp-core and pivotphp-cycle-orm:

  1. Clone both repositories in the same parent directory:
git clone https://github.com/PivotPHP/pivotphp-core.git
git clone https://github.com/PivotPHP/pivotphp-cycle-orm.git
  1. Install dependencies:
cd pivotphp-cycle-orm
composer install

The composer.json is configured to use the local path ../pivotphp-core for development.

Note: The CI/CD pipeline automatically adjusts the composer configuration to use the GitHub repository instead of the local path.

🔧 Quick Start

1. Register the Service Provider

use PivotPHP\Core\Core\Application;
use PivotPHP\Core\CycleORM\CycleServiceProvider;

$app = new Application();
$app->register(new CycleServiceProvider());

2. Configure Database

// config/cycle.php
return [
    'database' => [
        'default' => 'default',
        'databases' => [
            'default' => ['connection' => 'sqlite']
        ],
        'connections' => [
            'sqlite' => [
                'driver' => \Cycle\Database\Driver\SQLite\SQLiteDriver::class,
                'options' => [
                    'connection' => 'sqlite:database.db',
                ]
            ]
        ]
    ]
];

3. Define Entities

use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Column;

#[Entity(repository: UserRepository::class)]
class User
{
    #[Column(type: 'primary')]
    private int $id;

    #[Column(type: 'string')]
    private string $name;

    #[Column(type: 'string', unique: true)]
    private string $email;

    // Getters and setters...
}

4. Use in Routes

$app->get('/users', function (CycleRequest $request) {
    $users = $request->getRepository(User::class)->findAll();

    return $request->response()->json($users);
});

$app->post('/users', function (CycleRequest $request) {
    $user = new User();
    $user->setName($request->input('name'));
    $user->setEmail($request->input('email'));

    $request->persist($user);

    return $request->response()->json($user, 201);
});

🎯 Core Features

Repository Pattern

// Custom repository
class UserRepository extends Repository
{
    public function findByEmail(string $email): ?User
    {
        return $this->findOne(['email' => $email]);
    }

    public function findActive(): array
    {
        return $this->select()
            ->where('active', true)
            ->orderBy('created_at', 'DESC')
            ->fetchAll();
    }
}

Transaction Middleware

use PivotPHP\Core\CycleORM\Middleware\TransactionMiddleware;

// Automatic transaction handling
$app->post('/api/orders',
    new TransactionMiddleware(),
    function (CycleRequest $request) {
        // All database operations are wrapped in a transaction
        $order = new Order();
        $request->persist($order);

        // If an exception occurs, transaction is rolled back
        foreach ($request->input('items') as $item) {
            $orderItem = new OrderItem();
            $request->persist($orderItem);
        }

        return $request->response()->json($order);
    }
);

Query Monitoring

use PivotPHP\Core\CycleORM\Monitoring\QueryLogger;

// Enable query logging
$logger = $app->get(QueryLogger::class);
$logger->enable();

// Get query statistics
$stats = $logger->getStatistics();
// [
//     'total_queries' => 42,
//     'total_time' => 0.123,
//     'queries' => [...]
// ]

Health Checks

use PivotPHP\Core\CycleORM\Health\CycleHealthCheck;

$app->get('/health', function () use ($app) {
    $health = $app->get(CycleHealthCheck::class);
    $status = $health->check();

    return [
        'status' => $status->isHealthy() ? 'healthy' : 'unhealthy',
        'database' => $status->getData()
    ];
});

🛠️ Advanced Usage

Entity Validation Middleware

use PivotPHP\Core\CycleORM\Middleware\EntityValidationMiddleware;

$app->post('/users',
    new EntityValidationMiddleware(User::class, [
        'name' => 'required|string|min:3',
        'email' => 'required|email|unique:users,email'
    ]),
    $handler
);

Performance Profiling

use PivotPHP\Core\CycleORM\Monitoring\PerformanceProfiler;

$profiler = $app->get(PerformanceProfiler::class);
$profiler->startProfiling();

// Your database operations...

$profile = $profiler->stopProfiling();
// [
//     'duration' => 0.456,
//     'memory_peak' => 2097152,
//     'queries_count' => 15
// ]

Custom Commands

// Create entity command
php vendor/bin/pivotphp cycle:entity User

// Run migrations
php vendor/bin/pivotphp cycle:migrate

// Update schema
php vendor/bin/pivotphp cycle:schema

// Check database status
php vendor/bin/pivotphp cycle:status

🧪 Testing

# Run all tests
composer test

# Run specific test suite
composer test:unit
composer test:feature
composer test:integration

# Run with coverage (cross-platform)
composer test-coverage

# Platform-specific alternatives:
# Unix/Linux/macOS
./scripts/test-coverage.sh
# Windows CMD
scripts\test-coverage.bat
# PowerShell
scripts\test-coverage.ps1

Cross-Platform Compatibility

The project includes cross-platform scripts for coverage testing:

  • Primary method: composer test-coverage (works on all platforms)
  • Alternative scripts: Platform-specific scripts in scripts/ directory
  • Windows support: Both CMD and PowerShell scripts included

📚 Documentation

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

📄 License

PivotPHP Cycle ORM is open-sourced software licensed under the MIT license.

🙏 Credits

Built with PivotPHP - The modern PHP microframework

pivotphp/cycle-orm 适用场景与选型建议

pivotphp/cycle-orm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 pivotphp/cycle-orm 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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