PHPackages                             changhorizon/sql-condition - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. changhorizon/sql-condition

ActiveLibrary[Database &amp; ORM](/categories/database)

changhorizon/sql-condition
==========================

A PHP library for building and managing SQL WHERE conditions with a flexible, object-oriented approach.

v0.1.2(4w ago)01MITPHPPHP &gt;=8.3CI passing

Since May 11Pushed 4w agoCompare

[ Source](https://github.com/changhorizon/sql-condition)[ Packagist](https://packagist.org/packages/changhorizon/sql-condition)[ RSS](/packages/changhorizon-sql-condition/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

SQL Condition
=============

[](#sql-condition)

> A PHP library for building and managing SQL WHERE conditions with a flexible, object-oriented approach.

[![License](https://camo.githubusercontent.com/2c7d3443b4a101537b74ee0cd71b1384c9a8c5039b0b32a69ce36d053ca9c986/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368616e67686f72697a6f6e2f73716c2d636f6e646974696f6e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/2c7d3443b4a101537b74ee0cd71b1384c9a8c5039b0b32a69ce36d053ca9c986/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368616e67686f72697a6f6e2f73716c2d636f6e646974696f6e3f7374796c653d666c61742d737175617265)[![Latest Version](https://camo.githubusercontent.com/3eb88df0c16693354765dbbdb0a938a95e7d063174d1c113eceb941cd3b4bf00/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368616e67686f72697a6f6e2f73716c2d636f6e646974696f6e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/3eb88df0c16693354765dbbdb0a938a95e7d063174d1c113eceb941cd3b4bf00/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368616e67686f72697a6f6e2f73716c2d636f6e646974696f6e3f7374796c653d666c61742d737175617265)[![PHP Version](https://camo.githubusercontent.com/0bdfe9619dd802bc7688576ab0e2777de7ed0ccd5d0c9a9935039dc0b8d89b52/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e332d2d382e342d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/0bdfe9619dd802bc7688576ab0e2777de7ed0ccd5d0c9a9935039dc0b8d89b52/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e332d2d382e342d626c75653f7374796c653d666c61742d737175617265)[![Static Analysis](https://camo.githubusercontent.com/1a477f5e7e742a33c1ff5b685167579083a26c342c4e997fe056ea4ef7bea73e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617469635f616e616c797369732d5048505374616e2d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/1a477f5e7e742a33c1ff5b685167579083a26c342c4e997fe056ea4ef7bea73e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617469635f616e616c797369732d5048505374616e2d626c75653f7374796c653d666c61742d737175617265)[![Tests](https://camo.githubusercontent.com/72829871c802983bff15745f71c846973b36a6fea0bb5298dadaeeb690463604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d504850556e69742d627269676874677265656e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/72829871c802983bff15745f71c846973b36a6fea0bb5298dadaeeb690463604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d504850556e69742d627269676874677265656e3f7374796c653d666c61742d737175617265)[![codecov](https://camo.githubusercontent.com/4285563da3340ae917af7973748f0f70a69b2f4538d9812a653b384a4b5c23c1/68747470733a2f2f636f6465636f762e696f2f67682f6368616e67686f72697a6f6e2f73716c2d636f6e646974696f6e2f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/changhorizon/sql-condition)[![CI](https://github.com/changhorizon/sql-condition/actions/workflows/ci.yml/badge.svg?style=flat-square)](https://github.com/changhorizon/sql-condition/actions/workflows/ci.yml/badge.svg?style=flat-square)

Build SQL WHERE clauses programmatically with a clean, type-safe, and composable expression system. Supports 16 SQL operators with named parameter binding for PDO.

✨ 特性
----

[](#-特性)

- 16 SQL operators: comparison (`=`, ``, `>`, `>=`, `getString();  // "name = :value_name"
$eq->getParams();  // [':value_name' => 'John']
```

### Combining with Condition (AND)

[](#combining-with-condition-and)

```
use ChangHorizon\SqlCondition\Condition;
use ChangHorizon\SqlCondition\Expression;

$cond = new Condition([
    Expression::EQ('name', 'John'),
    Expression::GT('age', '18'),
    Expression::IS_NULL('deleted_at'),
]);

$cond->getString();  // "name = :value_name AND age > :value_age AND deleted_at IS NULL"
$cond->getParams();  // [':value_name' => 'John', ':value_age' => '18']
```

### Combining with Condition (OR)

[](#combining-with-condition-or)

```
use ChangHorizon\SqlCondition\Condition;
use ChangHorizon\SqlCondition\Enums\Logic;

$cond = new Condition([
    Expression::EQ('role', 'admin'),
    Expression::EQ('role', 'moderator'),
], Logic::OR);

$cond->getString();  // "role = :value_role OR role = :value_role"
```

### Nested Conditions (Grouping)

[](#nested-conditions-grouping)

```
$adminOrMod = new Condition([
    Expression::EQ('role', 'admin'),
    Expression::EQ('role', 'moderator'),
], Logic::OR);

$cond = new Condition([
    $adminOrMod,
    Expression::IS_TRUE('is_active'),
]);

$cond->getString();  // "(role = :value_role OR role = :value_role) AND is_active IS TRUE"
```

### LIKE with Anchors

[](#like-with-anchors)

```
use ChangHorizon\SqlCondition\Enums\Anchor;
use ChangHorizon\SqlCondition\Expressions\Like;

// Default (both sides): %value%
new Like('title', 'hello')->getParams();       // [':value_title' => '%:hello%']

// Left anchor only: value%
new Like('title', 'hello', Anchor::LEFT)->getParams();   // [':value_title' => ':hello%']

// Right anchor only: %value
new Like('title', 'hello', Anchor::RIGHT)->getParams();  // [':value_title' => '%:hello']
```

### Using with PDO

[](#using-with-pdo)

```
$cond = new Condition([
    Expression::EQ('email', 'user@example.com'),
    Expression::IS_NOT_NULL('verified_at'),
]);

$sql  = 'SELECT * FROM users WHERE ' . $cond->getString();
$stmt = $pdo->prepare($sql);
$stmt->execute($cond->getParams());
```

📐 接口说明
------

[](#-接口说明)

### `ExpressionInterface`

[](#expressioninterface)

MethodReturnsDescription`getString(): string`SQL fragment (e.g. `age > :value_age`)`getParams(): array`Named params for PDO binding`getLogic(): ?Logic``null` for leaf expressions### Static Factory: `Expression::*()`

[](#static-factory-expression)

MethodExpressionSQL Output`EQ($col, $val)`Equal`col = :val``NEQ($col, $val)`NotEqual`col  :val``GT($col, $val)`GreaterThan`col > :val``GTE($col, $val)`GreaterThanOrEqual`col >= :val``LT($col, $val)`LessThan`col < :val``LTE($col, $val)`LessThanOrEqual`col
