PHPackages                             erodriguezds/active-orm - 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. erodriguezds/active-orm

ActiveLibrary

erodriguezds/active-orm
=======================

Yet another lightweight ActiveRecord ORM for PHP library is responsible for building SQL query strings, Paginate and Hydration via an object oriented PHP interface.

v1.0.1(6y ago)04MITPHPPHP &gt;=7.2.0

Since May 22Pushed 6y agoCompare

[ Source](https://github.com/erodriguezds/ActiveORM)[ Packagist](https://packagist.org/packages/erodriguezds/active-orm)[ RSS](/packages/erodriguezds-active-orm/feed)WikiDiscussions master Synced 3d ago

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

PHP | Query Builder | ActiveRecord ORM
======================================

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

[![Build Status](https://camo.githubusercontent.com/8f5320f600ff1b7d8c31e92e65a9b633718cf69b4fc226c88ef542a1d2888681/68747470733a2f2f7472617669732d63692e6f72672f66676f6c646f6e692f51756572794275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/fgoldoni/QueryBuilder)[![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 and ActiveRecord-based ORM, heavily inspired in Yii2 ORM and Eloquent (Laravel ORM), but much more compact and lightweight!

Features
--------

[](#features)

- A powerful and intuitive Query builder ("Query": the core class of the entire ORM), with the fluent and beatiful syntax you already know from Eloquent or Yii2 ORM:

#### Select Query

[](#select-query)

```
$results = (new Query($myPdo))
    ->select('*') // not necesary
    ->from('users')
    ->where([
        'name' => $submitedName, // translates to "name = ? ", where '?' will be replaced by the SQL-escaped value of $submittedPage
        [ 'age', '>', $submitedAge ], // 3-elements array syntax for 'field', 'operator' and 'value'. The 3rd element will be prepared by PDO
        "age < $submitedAge", //more confortable, but this way, you won't have a PDO prepared parameter
        'id' => [2, 3, 6], // translates to "id IN(2, 3, 6)"
    ], 'OR') // OR's all the previously defined condition. Default is 'AND'
    ->fetchAll(); // you could also "fetchOne()"

foreach($result as $row){
    // ...
}
```

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

[](#getting-started)

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

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

Or, set the PDO globally, so that every new instance of Query uses it:

```
use ActiveORM\Query;
....
$pdo = new \PDO('mysql:dbname=goldoni;host=localhost;charset=utf8', 'root', 'root');
Query::setPdo($pdo);
....
$query = (new Query()) //will get the PDO instance from the global set
    ->select('*')
    ->from('users')
    ->where(...)
```

### Prerequisites

[](#prerequisites)

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

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

```

### Installing

[](#installing)

```
composer require erodriguezds/active-orm
```

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

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 57.1% 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 ~0 days

Total

2

Last Release

2550d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/20822acc6d64f5b8e587402f86b62f718dc10f2112f16708172561bd46958802?d=identicon)[erodriguezds](/maintainers/erodriguezds)

---

Top Contributors

[![dynamonet](https://avatars.githubusercontent.com/u/44091316?v=4)](https://github.com/dynamonet "dynamonet (4 commits)")[![erodriguezds](https://avatars.githubusercontent.com/u/38441170?v=4)](https://github.com/erodriguezds "erodriguezds (3 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/erodriguezds-active-orm/health.svg)

```
[![Health](https://phpackages.com/badges/erodriguezds-active-orm/health.svg)](https://phpackages.com/packages/erodriguezds-active-orm)
```

PHPackages © 2026

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