simbiat/db-binder
Composer 安装命令:
composer require simbiat/db-binder
包简介
Set of classes for database pool and helper.
README 文档
README
Class to help with binding to PDOStatement objects. Mostly syntax sugar, but not only.
Binding specific values
Most of the functions are aimed at binding one value and follow the usage pattern like
\Simbiat\Database\Bind::function_name(\PDOStatement $sql, string $binding, mixed $value);
where function_name is name of the function (listed below), $sql is respective \PDOStatement object (normally result of PDO's prepare statement), $binding is a string representing the named parameter to bind to, and $value is the value to attempt to bind. $value can be of any type, casting will be attempted in respective functions.
Generic binders
Some binders are just a way to replace use of \PDO::PARAM_* variables to allow use of "words" representing the types:
bindString- binds a value as string.bindInteger- binds a value as integer.bindBoolean- binds a value as boolean.bindBinary- binds a value as binary with automatic length calculation (as required byPDO).bindNull- binds a value as null (or rather it ignores the value entirely).
Custom binders
There are a bunch of binders that provide extra flavor:
bindYear,bindDate,bindTimeandbindDateTime- if you have\Simbiat\SandClockclass in your project will use itsformat()method to attempt conversion of the value provided to year (Y), date (Y-m-d), time (H:i:s.u) or datetime (Y-m-d H:i:s.u) respectively (DB engines normally recognize these formats as respective data types). Otherwise, will just treat the value as a string.bindBytes,bindBits- if you have\Simbiat\CuteBytesclass in your project will use itsbytes()method to convert the value to bytes or bits respectively. Otherwise, will treat the value as a string.bindLike- same asbindString, but will wrap the string in%symbols. Useful forLIKEqueries.bindMatch- same asbindString, but with extra processing and sanitization to allow use of the value inMATCHclause inFULLTEXTsearch.MATCHhas very specific rules and some characters in it mean specific things, so this will attempt to "prepare" the string to a format that will not break the search.
Binding multiple values
Quite often you do not have just one value to bind. For these cases you can use this:
\Simbiat\Database\Bind::bindMultiple(\PDOStatement $sql, array $bindings = []);
This will go through the array of bindings (format explained below), skip those bindings that are not present in the prepared query (works only for named identifiers) and then bind the values using the methods described earlier. Strings will be run through mb_scrub before binding.
Array format
$bindings = [ #Every key is the name of the parameter identifier that we are binding to ':table' => [ #The first element is the value that we are binding. For "in" expected to be an array. If not an array, will wrap it in one $table, #The second element is the data type of the element that we want to bind as. For `IN` we should use "in". More details on the types below. 'in', #The third element is semi-required for "in", and is the data type that will be applied to all array elements. If omitted, the value will be treated as a `string` 'string' ], ':schema' => [ $schema, 'string' ] ];
Data types mapping
The second element in the $bindings array expects certain values, which are used to map to respective functions as below:
public const array BINDING_HANDLERS = [ 'year' => 'bindYear', 'date' => 'bindDate', 'time' => 'bindTime', 'datetime' => 'bindDateTime', 'timestamp' => 'bindDateTime', 'bool' => 'bindBoolean', 'boolean' => 'bindBoolean', 'null' => 'bindNull', 'int' => 'bindInteger', 'integer' => 'bindInteger', 'number' => 'bindInteger', 'limit' => 'bindInteger', 'offset' => 'bindInteger', 'str' => 'bindString', 'string' => 'bindString', 'text' => 'bindString', 'float' => 'bindString', 'varchar' => 'bindString', 'varchar2' => 'bindString', 'bytes' => 'bindBytes', 'bits' => 'bindBits', 'match' => 'bindMatch', 'like' => 'bindLike', 'lob' => 'bindBinary', 'large' => 'bindBinary', 'object' => 'bindBinary', 'blob' => 'bindBinary', ];
If a value is not found in the mappings and is an integer, it will be considered to be a \PDO::PARAM_* constant and used directly in bindValue(). If it's not integer, the first element will be cast into string before being passed to bindValue().
Binding for IN clause
\Simbiat\Database\Bind::unpackIN(string $sql, array $bindings);
Can be used to "unpack" a $bindings array and prepare each value for future binding by "cloning" the original parameter identifier. Since this requires changing of the original SQL, function requires passing it a string (by reference), and thus before you have prepared it using prepare(). It also currently requires the list of all bindings to be passed (by reference) because they also need to be modified and because I simply do not see a good way of not doing that.
The query is expected to have the IN clause in it, and $bindings is expected to have a format same as for bindMultiple(). Only named identifiers are supported because otherwise the function can break the order, especially since + operator is used instead of array_merge. Here's a practical example:
$query = 'SELECT COUNT(*) as `count` FROM `information_schema`.`TABLES` WHERE `TABLE_NAME` IN(:table) AND `TABLE_SCHEMA` IN(:schema);'; $table = ['table', 'table2']; $schema = 'schema'; $bindings = [ ':table' => [ $table, is_string($table) ? 'string' : 'in', 'string' ], ':schema' => [ $schema, is_string($schema) ? 'string' : 'in', 'string' ] ]; \Simbiat\Database\Bind::unpackIN($sql, $bindings); echo $sql;
This will output the following query:
SELECT COUNT(*) as `count` FROM `information_schema`.`TABLES` WHERE `TABLE_NAME` IN(:table_0, :table_1) AND `TABLE_SCHEMA` IN(:schema);
and bindings would be:
[ ':schema' => [ 'schema', 'string', 'string' ], ':table_0' => [ 'table', 'string' ], ':table_1' => [ 'table2', 'string' ] ];
After the bindings are processed and PDO's prepare() is run, you can use the bindMultiple() method as explained earlier.
simbiat/db-binder 适用场景与选型建议
simbiat/db-binder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「php」 「pdo」 「bind」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 simbiat/db-binder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simbiat/db-binder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 simbiat/db-binder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
PHP module for MySql database
VV database abstraction layer with query builder and DB structure models
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: AGPL-3.0-or-later
- 更新时间: 2025-04-22