vitexsoftware/fluentpdo 问题修复 & 功能扩展

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

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

vitexsoftware/fluentpdo

Composer 安装命令:

composer require vitexsoftware/fluentpdo

包简介

FluentPDO - PHP 8.1+ Edition. A modernized fork with strict typing, union types, and enhanced features. Quick and light PHP library for rapid query building with smart join builder.

README 文档

README

A modernized fork of the original FluentPDO project, updated for PHP 8.1+ with modern type declarations and enhanced features.

FluentPDO is a PHP SQL query builder using PDO. It's a quick and light library featuring a smart join builder, which automatically creates table joins for you.

About This Fork

This is a modernized fork of the original envms/fluentpdo project. Since the original project has been inactive for 3+ years (last commit in 2021), this fork provides:

  • PHP 8.1+ compatibility with modern type declarations
  • Strict typing throughout the codebase
  • Union types and mixed types where appropriate
  • Typed properties for better IDE support
  • Array parameter handling with automatic JSON serialization
  • Enhanced error handling and debugging capabilities
  • Updated dependencies and development tools

Features

  • Easy interface for creating robust queries
  • Supports any database compatible with PDO
  • Ability to build complex SELECT, INSERT, UPDATE & DELETE queries with little code
  • Type hinting for magic methods with code completion in smart IDEs

PHP Version Requirements

This fork requires PHP 8.1 or higher.

What's New in This PHP 8.1+ Edition

  • Modern Type System: Full use of PHP 8.1+ type declarations including union types
  • Strict Types: All files use declare(strict_types=1) for better type safety
  • Array Handling: Automatic JSON serialization of array parameters to prevent SQL errors
  • Enhanced Performance: Better memory usage and performance through strict typing
  • Developer Experience: Improved IDE support with proper type hints and autocomplete
  • Future Ready: Prepared for upcoming PHP versions

Migrating from Original FluentPDO

If you're upgrading from the original FluentPDO (v2.x), this version maintains API compatibility while requiring PHP 8.1+. The main changes are internal improvements and type safety enhancements.

Reference

Sitepoint - Getting Started with FluentPDO

Installation

Composer

The preferred way to install this PHP 8.1+ edition of FluentPDO is via composer.

composer require vitexsoftware/fluentpdo

Or add the following line in your composer.json file:

{
    "require": {
        "vitexsoftware/fluentpdo": "^3.0"
    }
}

Then run composer update and you're done!

Requirements

  • PHP 8.1+ (required)
  • PDO extension (required)
  • Any PDO-compatible database (MySQL, PostgreSQL, SQLite, etc.)

Getting Started

Create a new PDO instance, and pass the instance to FluentPDO:

<?php
declare(strict_types=1);

use Envms\FluentPDO\Query;

$pdo = new PDO('mysql:dbname=fluentdb', 'user', 'password');
$fluent = new Query($pdo);

Then, creating queries is quick and easy:

$query = $fluent->from('comment')
             ->where('article.published_at > ?', $date)
             ->orderBy('published_at DESC')
             ->limit(5);

which would build the query below:

SELECT comment.*
FROM comment
LEFT JOIN article ON article.id = comment.article_id
WHERE article.published_at > ?
ORDER BY article.published_at DESC
LIMIT 5

To get data from the select, all we do is loop through the returned array:

foreach ($query as $row) {
    echo "$row['title']\n";
}

Using the Smart Join Builder

Let's start with a traditional join, below:

$query = $fluent->from('article')
             ->leftJoin('user ON user.id = article.user_id')
             ->select('user.name');

That's pretty verbose, and not very smart. If your tables use proper primary and foreign key names, you can shorten the above to:

$query = $fluent->from('article')
             ->leftJoin('user')
             ->select('user.name');

That's better, but not ideal. However, it would be even easier to not write any joins:

$query = $fluent->from('article')
             ->select('user.name');

Awesome, right? FluentPDO is able to build the join for you, by you prepending the foreign table name to the requested column.

All three snippets above will create the exact same query:

SELECT article.*, user.name 
FROM article 
LEFT JOIN user ON user.id = article.user_id
Close your connection

Finally, it's always a good idea to free resources as soon as they are done with their duties:

$fluent->close();

CRUD Query Examples

SELECT
$query = $fluent->from('article')->where('id', 1)->fetch();
$query = $fluent->from('user', 1)->fetch(); // shorter version if selecting one row by primary key
INSERT
$values = array('title' => 'article 1', 'content' => 'content 1');

$query = $fluent->insertInto('article')->values($values)->execute();
$query = $fluent->insertInto('article', $values)->execute(); // shorter version
UPDATE
use Envms\FluentPDO\Literal;

$set = ['published_at' => new Literal('NOW()')];

$query = $fluent->update('article')->set($set)->where('id', 1)->execute();
$query = $fluent->update('article', $set, 1)->execute(); // shorter version if updating one row by primary key
DELETE
$query = $fluent->deleteFrom('article')->where('id', 1)->execute();
$query = $fluent->deleteFrom('article', 1)->execute(); // shorter version if deleting one row by primary key

Note: INSERT, UPDATE and DELETE queries will only run after you call ->execute()

Modern PHP 8.1+ Features

Array Parameter Handling

This edition automatically handles array parameters by converting them to JSON strings:

// Arrays are automatically converted to JSON
$data = [
    'name' => 'John Doe',
    'tags' => ['php', 'mysql', 'programming'],  // Automatically converts to JSON
    'metadata' => ['created' => '2025-01-01', 'active' => true]
];

$fluent->insertInto('users', $data)->execute();

Strict Type Declarations

All classes use strict typing for better performance and error detection:

<?php
declare(strict_types=1);

// Your code here...

Enhanced IDE Support

With full type declarations, you get better autocomplete and error detection in modern IDEs.

Credits

License

Free for commercial and non-commercial use under the Apache 2.0 or GPL 2.0 licenses.

vitexsoftware/fluentpdo 适用场景与选型建议

vitexsoftware/fluentpdo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 294 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2025-10-21