PHPackages                             roi/sql-clause-builder - 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. roi/sql-clause-builder

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

roi/sql-clause-builder
======================

Build Sql clause from php arrays

1.0.0(5y ago)04[1 PRs](https://github.com/Roisholto/sqlClauseBuilder/pulls)PHPCI passing

Since May 12Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Roisholto/sqlClauseBuilder)[ Packagist](https://packagist.org/packages/roi/sql-clause-builder)[ RSS](/packages/roi-sql-clause-builder/feed)WikiDiscussions master Synced 6d ago

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

SqlClauseBuilder
================

[](#sqlclausebuilder)

Build SQL clause from an array.

##### Installation

[](#installation)

To add the package to your dependencies, you should have composer installed on your machine. Run the command below in your project directory

`composer require roi/sql-clause-builder`

###### Namespace

[](#namespace)

You can access the class within this namespace `\Roi\SqlClauseBuilder`

##### Usage

[](#usage)

In order to use the package you must include the auto-generated `autoload.php` file by composer usually in the location `{project-root}/vendor` at the top of your script.

If you are using a PHP framework like Laravel, Slim etc, you can skip the above because it would be automatically added.

`SqlClauseBuilder` requires you to first define an associative array of parameters that is acceptable. See sample below

```
$searchable_params = [
    'name'=>[
        'col'=> 'merchants.name',
        'factors'=>[
            'like',
            'equalto'
        ],
        'bind_as'=>PDO::PARAM_STR
    ],
    'type'=>[
        'col'=> 'merchants.type',
        'factors'=>[
            'equalto'
        ],
        'bind_as'=>PDO::PARAM_STR
    ],
] ;
```

Notice that each entry in the array above is an associative array with 3 defined keys, which are

- `col`, a string which implies to the corresponding column in the table
- `factors`, an array of SQL operations that can be performed on the specified column
- `bind_as`, the explicit data type for the parameter. See  for a list options.

Please note that the keys are required.

Next, create an instance of `SqlClauseBuilder` with the defined searchable columns passed as the argument.

Note that the `$searchable_params` is the only argument required by the constructor

```
$clausebuilder = new \Roi\SqlClauseBuilder\SqlClauseBuilder($searchable_params) ;
```

Then you could easily fit this in a PDO prepared statement, binding the parameters ;

Next call the `build` method, passing the array you want built into a SQL clause

```
$data = ['name'=>['data'=>['Bob%'],'factor'=>'like'] ] ;
$clause = $clausebuilder->build($data) ; // returns ['clause'=> 'merchants.name LIKE ?', 'binds'=>[ ['Bob%',PDO::PARAM_STR] ] ]
```

The build method returns an array with structure like below

```
[
    'clause'=> 'merchants.name LIKE ?',
    'binds'=>[
        [
            'Bob%',PDO::PARAM_STR
        ]
    ]
]
```

You can use the result in your query as demonstrated below

```
$query = $Pdo->prepare("SELECT * FROM table WHERE $clause['clause'] ") ;
foreach($clause['binds'] as $key=>$val){
  $query->bindParam($val[0], $val[1]) ;
}
$query->execute() ;
```

### WHAT IS COVERED

[](#what-is-covered)

The following operators are supported

- like,
- equalto
- notequalto
- greaterthan
- lesserthan
- greaterthanorequalto
- lesserthanorequalto
- between
- notbetween
- isnull
- notisnull

### TODO

[](#todo)

- add support for more operators
- provide a method that allows users add operators

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance53

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1832d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/12684628?v=4)[Soetan Ayobami](/maintainers/roisholto)[@Roisholto](https://github.com/Roisholto)

---

Top Contributors

[![Roisholto](https://avatars.githubusercontent.com/u/12684628?v=4)](https://github.com/Roisholto "Roisholto (14 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/roi-sql-clause-builder/health.svg)

```
[![Health](https://phpackages.com/badges/roi-sql-clause-builder/health.svg)](https://phpackages.com/packages/roi-sql-clause-builder)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
