PHPackages                             makinacorpus/query-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. makinacorpus/query-builder

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

makinacorpus/query-builder
==========================

Driver agnostic SQL query builder.

1.7.1(3mo ago)7161.0k—7.7%1[14 issues](https://github.com/makinacorpus/php-query-builder/issues)3MITPHPPHP &gt;=8.1CI passing

Since Nov 16Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/makinacorpus/php-query-builder)[ Packagist](https://packagist.org/packages/makinacorpus/query-builder)[ Docs](http://github.com/makinacorpus/php-query-builder)[ RSS](/packages/makinacorpus-query-builder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (8)Versions (47)Used By (3)

Query Builder
=============

[](#query-builder)

[![Coding standards](https://github.com/makinacorpus/php-query-builder/actions/workflows/coding-standards.yml/badge.svg)](https://github.com/makinacorpus/php-query-builder/actions/workflows/coding-standards.yml) [![Static Analysis](https://github.com/makinacorpus/php-query-builder/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/makinacorpus/php-query-builder/actions/workflows/static-analysis.yml) [![Documentation Status](https://camo.githubusercontent.com/2ad9db9ea644b601f91b0f6dc3d9d12906b8c4c7f708cbc6f4006191ea3d58f5/68747470733a2f2f72656164746865646f63732e6f72672f70726f6a656374732f71756572792d6275696c6465722f62616467652f3f76657273696f6e3d737461626c65)](https://query-builder.readthedocs.io/en/stable/?badge=stable)

This is a driver-agnostic SQL query builder with advanced SQL language features.

Per default, it generates (almost) SQL standard compliant queries, almost because some SQL features are simply not implemented by any dialect we support case in which we implement a custom variant.

It is based upon a fluent API, which makes basic SELECT, DELETE, INSERT, UPDATE and MERGE queries easy to write, even if you don't really now SQL.

At any place the user can inject raw SQL in order to escape the query builder own limitations.

This API by itself is only a SQL string generator, in order to use it transparently, a few bridges are provided:

- `doctrine/dbal:^3` bridge and query builder,
- soon a `doctrine/dbal:^4` bridge and query builder,
- `PDO` bridge and query builder,
- Symfony bundle integrating with `doctrine/doctrine-bundle` is available in the [makinacorpus/query-builder-bundle](https://github.com/makinacorpus/query-builder-bundle)package.

Getting started
===============

[](#getting-started)

Read the [documentation on read the docs](https://php-query-builder.readthedocs.io/en/latest/introduction/getting-started.html).

Overview [list of SQL supported features](https://php-query-builder.readthedocs.io/en/latest/introduction/features.html#).

See the [officially supported vendor dialects](https://php-query-builder.readthedocs.io/en/latest/introduction/getting-started.html#standalone-setup).

Non-exhaustive feature list
===========================

[](#non-exhaustive-feature-list)

- **Common Table Expressions**:
    - `WITH "foo" AS (SELECT ...)`
- **VALUES / Constant table expression**:
    - `WITH "foo" ("col1", "col2") AS (SELECT VALUES ...)`
    - `SELECT VALUES ... AS ("col1", "col2") `
    - `JOIN (VALUES ...) AS "foo"`, ...
- **Aggregate function filter**:
    - Standard SQL: `SELECT SUM(foo) FILTER (WHERE )`
    - Emulation for non supporting RDBMS: `SELECT SUM(CASE WHEN  THEN foo END)`
- **Window functions**:
    - `SELECT SUM(foo) OVER (PARTITION BY... ORDER BY...)`
    - `SELECT SUM(foo) OVER (bar) FROM baz WINDOW bar AS (PARTITION BY... ORDER BY...)`
- **RETURNING/OUTPUT**:
    - PostgreSQL: `UPDATE foo SET ... RETURNING foo, bar, baz`
    - SQL Server: `UPDATE foo SET ... OUTPUT DELETED.col1, INSERTED.col1`
- **Complex WHERE condition builder**
- **CASE WHEN ... THEN ... END**
- **IF THEN emulated using CASE ... WHEN**
- **ARRAY expression**:
    - `ARRAY[? ,? ?]`
- **ROW expressions**:
    - Standard SQL: `ROW('foo', 2, ...)`
    - PostgreSQL composite types: `ROW('foo', 2, ...)::some_composite_type`
- **Mixing complex data structures**:
    - `CAST(ROW('foo', CAST(ARRAY[1,2,3] AS bigint[])) AS some_composite_type)`
- **CAST expressions**:
    - Standard SQL: `CAST(expression AS type)`
    - PostgreSQL: `expression::type`
- **LIKE / SIMILAR TO**
    - `foo LIKE '%somevalue%'`
    - `foo SIMILAR TO '%somevalue%'`
- **Update FROM JOIN**
- **INSERT INTO ... VALUES ...**
- **INSERT INTO ... SELECT ...**
- **Arbitrary RAW SQL from user everywhere**
    - The builder doesn't support some X or Y advanced feature or specific dialect, then write your own SQL.
- **Identifier escaping everywgere**
- **User value conversion to SQL**

Building documentation
======================

[](#building-documentation)

Documentation is written using [VitePress](https://vitepress.dev).

For readers:

```
cd ./docs/
nvm use
npm ci
npm run docs:build
 ./.vitepress/dist/index.html
```

For developers:

```
cd ./docs/
nvm use
npm ci
npm run docs:dev
```

Run tests
=========

[](#run-tests)

Most unit tests will work gracefuly by running directly PHPUnit:

```
composer install
vendor/bin/phpunit
```

For running bridges integration tests, it needs a complete environment with some databases up and running, for this purpose a script that uses `docker compose` is provided, yet it might not work gracefuly in all environments:

```
composer install
./run-tests.sh
```

Please understand that the testing environment is currently at the prototype stage, and will remain as-is until github actions CI is configured.

History
=======

[](#history)

This API is an almost complete rewrite, keeping the base design, using modern PHP &gt;= 8.2 features and much more unit tested of the query builder from the `makinacorpus/goat-query` package.

Rationale is that we deeply used `makinacorpus/goat-query` as our primary database access layer for some years, but we are shifting toward using more commonly used tooling for future projects.

Doctrine DBAL query builder can do everything you would want to do, but its API is unintuitive and written code using is hard to read. Moreover, it doesn't implement modern SQL language features since its goal is mostly to provide lowest common denominator for a huge number of RDBMS.

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance58

Moderate activity, may be stable

Popularity40

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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

Every ~20 days

Recently: every ~159 days

Total

41

Last Release

116d ago

Major Versions

0.3.1 → 1.0.02023-12-12

### Community

Maintainers

![](https://www.gravatar.com/avatar/69252826f3a70a19fc5dcefb7ef9d26d465bb300245641abb4dd89d0ec391a66?d=identicon)[pounard](/maintainers/pounard)

![](https://www.gravatar.com/avatar/d21b98752b406528da88850922b1061f39bf72eb2126b413d5c12e275811a40b?d=identicon)[Makina Corpus](/maintainers/Makina%20Corpus)

---

Top Contributors

[![pounard](https://avatars.githubusercontent.com/u/341855?v=4)](https://github.com/pounard "pounard (197 commits)")

---

Tags

symfonymysqlpostgresqlsqlquery builder

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/makinacorpus-query-builder/health.svg)

```
[![Health](https://phpackages.com/badges/makinacorpus-query-builder/health.svg)](https://phpackages.com/packages/makinacorpus-query-builder)
```

###  Alternatives

[cycle/orm

PHP DataMapper ORM and Data Modelling Engine

1.3k835.4k65](/packages/cycle-orm)[usmanhalalit/pixie

A lightweight, expressive, framework agnostic query builder for PHP.

6872.2M15](/packages/usmanhalalit-pixie)[cycle/database

DBAL, schema introspection, migration and pagination

64690.9k31](/packages/cycle-database)[pecee/pixie

Lightweight, fast query-builder for PHP based on Laravel Eloquent but with less overhead.

4128.7k8](/packages/pecee-pixie)

PHPackages © 2026

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