rain1/condition-builder 问题修复 & 功能扩展

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

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

rain1/condition-builder

Composer 安装命令:

composer require rain1/condition-builder

包简介

Condition Builder for logical expressions

README 文档

README

Build Status

This package allows you to build query conditions for PDO-like interfaces.

This is NOT an ORM: it's a condition builder. Let see the example:

Usage

<?php

require_once "vendor/autoload.php";

use rain1\ConditionBuilder\ConditionBuilder;
use rain1\ConditionBuilder\Operator\IsEqual;
use rain1\ConditionBuilder\Operator\IsLess;

$condition = new ConditionBuilder(ConditionBuilder::MODE_AND);

$condition->append(
	new IsEqual("id", 3), // id = ? AND
	new IsLess("priority", 100), // priority < ? AND
	new IsEqual("myCol", [1,2,3]), // myCol IN (?,?,?)
	new IsEqual("anotherCol", null), // it will be ignored because null value
	(new ConditionBuilder(ConditionBuilder::MODE_OR))
		->append(
			new IsEqual("col1", 3), // col1 = ? OR
			new IsEqual("someflag", 0) // someflag = ?
		)
);

$query =  "SELECT * FROM myTable WHERE $condition";

echo $query."\n";
print_r($condition()); // shortcut of $condition->values() it returns an array with all values

//$res = $pdo->prepare($query)->execute($condition->values());

This will produce the following output:


SELECT * FROM myTable WHERE (id = ? AND priority < ? AND myCol IN (?,?,?) AND (col1 = ? OR someflag = ?))
Array
(
    [0] => 3
    [1] => 100
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 3
    [6] => 0
)

Where is the magic?

Well, let suppose we want to write a function that filter rows in a MySQL table. Generally, if we don't want use some magic framework or ORM, we have to do a lot of if conditions and we have to concatenate the query and push params values.

That's the ConditionBuilder internal job.

<?php
function filterUser(array $filters = []) : array
{
    $defaults = [
        'id'                     => null,
        'banned'                 => null,
        'last_login_range_start' => null,
        'last_login_range_end'   => null,
        'email'                  => null,
    ];
    $filters  = $filters + $defaults;

    $condition = new ConditionBuilder(ConditionBuilder::MODE_AND);

    $condition->append(
        new IsEqual('id', $filters['id']),
        new IsEqual('email', $filters['email']),
        new IsEqual('banned', $filters['banned']),
        new IsBetween("last_login", $filters["last_login_range_start"], $filters["last_login_range_end"])
    );
    
    return YourMysqlLibrary::query("SELECT * FROM user WHERE $condition", $condition());
}

$rows = filterUser([
    'id' => [1,2,3]
]); // users with id 1,2 or 3 

$rows = filterUser([
    'last_login_range_start' => date("Y-m-d 00:00:00", "yesterday"),
    'banned' => 0
]); // users not banned and logged yesterday or today

$rows = filterUser([
    'last_login_range_start' => "2021-01-01",
    'last_login_range_end' => "2021-01-31",
    'banned' => 1
]); // users actually banned and last seen in Jan 2021

$rows = filterUser([
    'email' => "user@example.org"
]); // user with specified email
// and so on

So you can easily build a lot of searches with a minimal function.

Of course, it is a simple condition helper, so your query can be more complex. You can use more than one ConditionBuilder and you can append them to another.

Beaware

If the ConditionBuilder is empty (it means all filters are null), the result will be:

  • (TRUE), if the mode is ConditionBuilder::MODE_AND
  • (FALSE), if the mode is ConditionBuilder::MODE_OR

So be carefull especially if you use it with DELETE statements.

rain1/condition-builder 适用场景与选型建议

rain1/condition-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.46k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 09 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 rain1/condition-builder 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-16