PHPackages                             goldoni/php7.2-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. goldoni/php7.2-query-builder

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

goldoni/php7.2-query-builder
============================

This PHP library is responsible for building SQL query strings, Paginate and Hydration via an object oriented PHP interface.

1.3.0(7y ago)5141MITPHPPHP &gt;=7.2.0

Since Oct 31Pushed 7y agoCompare

[ Source](https://github.com/fgoldoni/QueryBuilder)[ Packagist](https://packagist.org/packages/goldoni/php7.2-query-builder)[ RSS](/packages/goldoni-php72-query-builder/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (5)Versions (10)Used By (0)

PHP | Query Builder
===================

[](#php---query-builder)

[![Build Status](https://camo.githubusercontent.com/8f5320f600ff1b7d8c31e92e65a9b633718cf69b4fc226c88ef542a1d2888681/68747470733a2f2f7472617669732d63692e6f72672f66676f6c646f6e692f51756572794275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/fgoldoni/QueryBuilder)[![Coverage Status](https://camo.githubusercontent.com/0053d023531574d2c57bb7c54ff369aaa4eae27eddc377a29dce51f25a02928e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f66676f6c646f6e692f51756572794275696c6465722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/fgoldoni/QueryBuilder?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8a35784f253180549c21963f609d7bc75a3024e042a86f7994cd0519d2265a37/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f66676f6c646f6e692f51756572794275696c6465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/fgoldoni/QueryBuilder/?branch=master)[![Code Intelligence Status](https://camo.githubusercontent.com/5ac25c1a9db2c8819b05925022ad89737be601d86768c4bd6d539013f1a97414/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f66676f6c646f6e692f51756572794275696c6465722f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)
[![](https://camo.githubusercontent.com/74b56716c676feadda1374a74a5b62dfc506b25202db1abb4f682f64a1287da1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e322d626c75652e7376673f7374796c653d706c6173746963)](https://camo.githubusercontent.com/74b56716c676feadda1374a74a5b62dfc506b25202db1abb4f682f64a1287da1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e322d626c75652e7376673f7374796c653d706c6173746963)
PHP Query builder, Paginate and Hydratation using PDO

Features
--------

[](#features)

- Allows you to perform complex queries with little code

    - [select](https://github.com/fgoldoni/QueryBuilder#select)
    - [insert](https://github.com/fgoldoni/QueryBuilder#insert)
    - [update](https://github.com/fgoldoni/QueryBuilder#update)
    - [delete](https://github.com/fgoldoni/QueryBuilder#delete)
    - [where](https://github.com/fgoldoni/QueryBuilder#where)
    - [params](https://github.com/fgoldoni/QueryBuilder#params)
    - [count](https://github.com/fgoldoni/QueryBuilder#count)
    - [orderBy](https://github.com/fgoldoni/QueryBuilder#orderBy)
    - [groupBy](https://github.com/fgoldoni/QueryBuilder#groupBy)
    - [joins](https://github.com/fgoldoni/QueryBuilder#joins)
    - [limit](https://github.com/fgoldoni/QueryBuilder#limit)
    - [fetchAll](https://github.com/fgoldoni/QueryBuilder#fetchAll)
    - [fetch](https://github.com/fgoldoni/QueryBuilder#fetch)
    - [fetchOrFail](https://github.com/fgoldoni/QueryBuilder#fetchOrFail)
    - [execute](https://github.com/fgoldoni/QueryBuilder#execute)
- **Pagination**

    - [paginate](https://github.com/fgoldoni/QueryBuilder#paginate)
- **Hydration** Ability to return a collection of objects:

    - [into(Demo::class)](https://github.com/fgoldoni/QueryBuilder#Hydration)

Getting Started
---------------

[](#getting-started)

Create a new PDO instance, and pass the instance to Query:

```
use Goldoni\Builder\Query;
....
$pdo = new \PDO('mysql:dbname=goldoni;host=localhost;charset=utf8', 'root', 'root');
$query = (new Query($pdo))
```

### Prerequisites

[](#prerequisites)

What things you need to install the software and how to install them

```
"require": {
    "php": ">=7.2.0"
}

```

### Installing

[](#installing)

```
composer require goldoni/php7.2-query-builder
```

Examples
--------

[](#examples)

#### select

[](#select)

```
$query = (new Query())->from('users')->select('first_name');
```

build the query below

```
SELECT first_name FROM users

```

#### insert

[](#insert)

```
$query = (new Query($this->pdo))
            ->insert(
                'users',
                [
                    'first_name' => ':first_name',
                    'last_name'  => ':last_name',
                    'email'      => ':email',
                    'mobile'     => ':mobile',
                    'phone'      => ':phone',
                ]
            );

$query1 = (new Query($this->pdo))
            ->insert('users')
            ->value([
                'first_name' => ':first_name',
                'last_name'  => ':last_name',
                'email'      => ':email',
                'phone'      => ':phone'
            ]);
```

build the query below

```
INSERT INTO users (first_name, last_name, email, mobile, phone) VALUES (:first_name, :last_name, :email, :mobile, :phone)

```

insert with -&gt;execute()

```
$data = [
    'id' => 12,
    'first_name' => 'Joe3',
    'last_name'  => 'Doe3',
    'email'      => 'joe@contact.de',
    'phone'      => '+0172222222'
];

$query = (new Query($this->pdo))
    ->insert(
        'users',
        [
            'id' => ':id',
            'first_name' => ':first_name',
            'last_name'  => ':last_name',
            'email'      => ':email',
            'phone'      => ':phone'
        ]
    )->params($data)
    ->execute();
```

#### update

[](#update)

```
$data = [
    'first_name' => 'Joe3',
    'last_name'  => 'Doe3',
    'email'      => 'joe@contact.de'
];
$query = (new Query($this->pdo))
        ->update(
            'users',
            [
                'first_name' => ':first_name',
                'last_name'  => ':last_name',
                'email'      => ':email'
            ],
            2
        )
        ->params($data);

$query1 = (new Query($this->pdo))
            ->update('users')
            ->set([
                'first_name' => ':first_name',
                'last_name'  => ':last_name',
                'email'      => ':email'
            ])
            ->where('id = :id')
            ->params($data);
```

build the query below

```
UPDATE users SET first_name = :first_name, last_name = :last_name, email = :email WHERE (id = :id)

```

update with -&gt;execute()

```
$data = [
    'first_name' => 'Joe',
    'last_name'  => 'Doe',
    'email'      => 'joe@contact.de',
    'phone'      => '+0172222222'
];
$query = (new Query($this->pdo))
    ->update(
        'users',
        [
            'first_name' => ':first_name',
            'last_name'  => ':last_name',
            'email'      => ':email',
            'phone'      => ':phone'
        ],
        4
    )
    ->params($data)
    ->execute();
```

#### delete

[](#delete)

```
$query = (new Query($this->pdo))->delete('users', 2);

$query1 = (new Query($this->pdo))->delete('users')->where('id = :id')->params(['id' => 12]);
```

build the query below

```
DELETE FROM users WHERE (id = :id)

```

delete with -&gt;execute()

```
(new Query($this->pdo))->delete('users', 2)->execute();
```

#### where

[](#where)

```
$query = (new Query())
            ->from('users', 'u')
            ->where('first_name = :first_name OR email = :email', 'phone = :phone');

$query2 = (new Query())
            ->from('users', 'u')
            ->where('first_name = :first_name OR email = :email')
            ->where('mobile = :mobile');
```

build the query below

```
SELECT * FROM users as u WHERE (first_name = :first_name OR email = :email) AND (phone = :phone)

SELECT * FROM users as u WHERE (first_name = :first_name OR email = :email) AND (mobile = :mobile)

```

#### params

[](#params)

```
$user = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('id = :id')
            ->params(['id' => 1]);
```

#### count

[](#count)

```
$usersCount = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('u.id < :number')
            ->params([
                'number' => 5
            ])
            ->count();
```

#### orderBy

[](#orderby)

```
$query = (new Query($this->pdo))
            ->from('users', 'u')
            ->orderBy('u.id', 'DESC');
```

build the query below

```
SELECT * FROM users as u ORDER BY u.id DESC

```

#### groupBy

[](#groupby)

```
$query = (new Query())
            ->select('u.first_name', 'COUNT(id)')
            ->from('users', 'u')
            ->groupBy('u.updated_at');
```

build the query below

```
SELECT u.first_name, COUNT(id) FROM users as u GROUP BY u.updated_at

```

#### joins

[](#joins)

```
 $query = (new Query())
            ->from('users', 'u')
            ->select('first_name')
            ->join('posts as p', 'u.id = p.user_id');

 $query = (new Query())
            ->from('users', 'u')
            ->select('first_name')
            ->join('posts as p2', 'u.id = p2.user_id', 'inner');
```

build the query below

```
SELECT first_name FROM users as u LEFT JOIN posts as p ON u.id = p.user_id

SELECT first_name FROM users as u INNER JOIN posts as p2 ON u.id = p2.user_id

```

#### limit

[](#limit)

```
$query = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('first_name = :first_name OR email = :email')
            ->limit(5);
```

build the query below

```
SELECT * FROM users as u WHERE (first_name = :first_name OR email = :email) LIMIT 0, 5

```

#### fetchAll

[](#fetchall)

```
$query = (new Query($this->pdo))->from('users')->select('first_name')->fetchAll();
```

#### fetch

[](#fetch)

```
$user = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('id = :id')
            ->params(['id' => 1])
            ->fetch();
```

#### fetchOrFail

[](#fetchorfail)

```
$user = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('id = :id')
            ->params(['id' => 100])
            ->fetchOrFail();
```

#### execute

[](#execute)

```
$query = (new Query($this->pdo))
            ->insert(
                'users',
                [
                    'first_name' => ':first_name',
                    'last_name'  => ':last_name',
                    'email'      => ':email',
                    'mobile'     => ':mobile',
                    'phone'      => ':phone',
                ]
            )
            ->execute();
```

#### paginate

[](#paginate)

```
    $paginate = (new Query($this->pdo))
        ->from('users', 'u')
        ->into(Demo::class)
        ->paginate(5, 1);

    $paginate->getNbPages();
    $paginate->haveToPaginate();
    $paginate->hasPreviousPage();
    $paginate->getPreviousPage();
    $paginate->hasNextPage();
    $paginate->getNextPage();
    $paginate->getCurrentPageOffsetStart();
    $paginate->getCurrentPageOffsetEnd();
    $paginate->getIterator(); // return collections of objects
```

Hydration
---------

[](#hydration)

```
use Goldoni\Builder\Entities\Demo;
...
$demos = (new Query($this->pdo))
            ->from('users', 'u')
            ->into(Demo::class)
            ->fetchAll();

$demo1 = $demos[0];

// get_class($demo1) === Demo::class

echo $demo1->firstName;

$demo2 = (new Query($this->pdo))
            ->from('users', 'u')
            ->where('id = :id')
            ->into(Demo::class)
            ->params(['id' => 2])
            ->fetch();

// get_class($demo2) === Demo::class

echo $demo2->firstName;
```

```
var_dump($demo1 instanceof Demo::class); // TRUE
var_dump($demo2 instanceof Demo::class); // TRUE

```

Unit Test
---------

[](#unit-test)

```
./vendor/bin/phpunit
```

Versioning
----------

[](#versioning)

We use 1.3.0

Authors
-------

[](#authors)

- **Goldoni Fouotsa** - *Initial work*

License
-------

[](#license)

This project is licensed under the MIT License

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity63

Established project with proven stability

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 ~1 days

Total

9

Last Release

2793d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/23716281?v=4)[Goldoni](/maintainers/fgoldoni)[@fgoldoni](https://github.com/fgoldoni)

---

Tags

hydrationpaginatequery-builder

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M719](/packages/sylius-sylius)[friendsofsymfony/elastica-bundle

Elasticsearch PHP integration for your Symfony project using Elastica

1.3k17.7M49](/packages/friendsofsymfony-elastica-bundle)[kimai/kimai

Kimai - Time Tracking

4.8k8.7k1](/packages/kimai-kimai)[ray/media-query

PHP interface-based SQL framework

11254.8k3](/packages/ray-media-query)[petkopara/crud-generator-bundle

Symfony3 bundle for CRUD generation with pagination, filtering, sorting, page size, bulk delete and bootstrap3 markup. This Generator supports Doctrine association mapping.

7058.1k](/packages/petkopara-crud-generator-bundle)[netgen/layouts-core

Netgen Layouts enables you to build and manage complex web pages in a simpler way and with less coding. This is the core of Netgen Layouts, its heart and soul.

3692.1k16](/packages/netgen-layouts-core)

PHPackages © 2026

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