PHPackages                             aomega/pharel - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. aomega/pharel

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aomega/pharel
=============

A Relational Algebra for PHP, based on rails/arel

111PHP

Since Jun 20Pushed 10y ago1 watchersCompare

[ Source](https://github.com/aomega08/pharel)[ Packagist](https://packagist.org/packages/aomega/pharel)[ RSS](/packages/aomega-pharel/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Pharel
======

[](#pharel)

Pharel is A Relational Algebra library for PHP. It is not simply inspired by [Arel](https://github.com/rails/arel), it is actually a Class-by-Class port of Arel to PHP made by a Rails lover constrained to work with PHP.

Description
-----------

[](#description)

Pharel is just an Abstract Syntax Tree with a node per each SQL statement.

With all probabilities, it's not very confortable for everyday querying, as methods are quite verbose, but it lets you write your own ORM forgetting the complexity of query generation and database compatibility. You can still use it raw in your application, for input escaping or if you need to support many different DBMS.

Why should you bother?
----------------------

[](#why-should-you-bother)

- Generates the 4 CRUD queries
- Prevents SQL injection through server escaping (if used correctly)
- Can handle arbitrarly large and complex queries (joins in joins in unions in joins in nested queries)
- Support all your favorite databases custom syntaxes:
    - MySQL
    - PostgreSQL
    - SQLite
    - Oracle, MSSQL and others to come

Requirements
------------

[](#requirements)

Due to use of traits in place of modules, Pharel requires PHP &gt;= 5.4 to work, which also ships with the magnificent array shorthand syntax.

I could not resist the pulse to make a gem -- ehm, a package. So you can use [Composer](https://github.com/composer/composer) to install and use the library. Put

```
{
    "require-dev": {
        "aomega/pharel": "0.*"
    }
}

```

in your composer.json file and run `composer install` or `php composer.phar install`, depending on your installation method.

If you don't have composer, or do not want to use it, you can just import the lib directory. Be sure to have a PSR-0 compliant autoloader (class `Pharel\Nodes\Test_Me` is in file `./Pharel/Nodes/Test/Me.php`), and enjoy.

Examples
--------

[](#examples)

```
$users = new Pharel\Table("users");
$query = $users->project(Pharel::sql("*"));

echo $query->to_sql();
// => SELECT * FROM `users`
```

```
$users = new Pharel\Table("users");
$query = $users->project($users["id"]);

echo $query->to_sql();
// => SELECT `users`.`id` FROM `users`
```

```
$users = new Pharel\Table("users");
$query = $users->project($users["age"]->average());

echo $query->to_sql();
// => SELECT AVG(`users`.`age`) AS avg_id FROM `users`
```

```
$users = new Pharel\Table("users");
$query = $users->project(Pharel::sql("*"))->where($users["name"]->eq("amy"));

echo $query->to_sql();
// => SELECT * FROM `users` WHERE `users`.`name` = 'amy';
```

```
$posts = new Pharel\Table("posts");
$users = new Pharel\Table("users");

$query = $posts
    ->project(Pharel::sql("*"))
    ->join("users")->on($posts["user_id"]->eq($users["id"])
    ->take(1);

echo $query->to_sql();
// => SELECT * FROM `posts` JOIN `users` ON `posts`.`user_id` = `users`.`id` LIMIT 1
```

```
$users = new Pharel\Table("users");

$users->where($users["age"]->eq(10))              // WHERE `users`.`age` = 10
$users->where($users["age"]->not_eq(10))          // WHERE `users`.`age` != 10
$users->where($users["age"]->gt(10))              // WHERE `users`.`age` > 10
$users->where($users["age"]->gteq(10))            // WHERE `users`.`age` >= 10
$users->where($users["age"]->lt(10))              // WHERE `users`.`age` < 10
$users->where($users["age"]->lteq(10))            // WHERE `users`.`age` where($users["age"]->in([ 3, 10, 26 ]))   // WHERE `users`.`age` IN (3, 10, 26)
```

Status
------

[](#status)

It is only partially tested and you can probably find bugs easily. You should probably not use this on production systems, but please, contribute.

License
-------

[](#license)

Pharel is released under the terms of the [MIT License](https://github.com/aomega08/pharel/blob/master/LICENSE).

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/7397f22f005b292df4b2670ff8fe83a9bb18edf8001958e2ad3a2b9c99c7ae45?d=identicon)[francescoboffa](/maintainers/francescoboffa)

---

Top Contributors

[![francescobbo](https://avatars.githubusercontent.com/u/854304?v=4)](https://github.com/francescobbo "francescobbo (51 commits)")

### Embed Badge

![Health badge](/badges/aomega-pharel/health.svg)

```
[![Health](https://phpackages.com/badges/aomega-pharel/health.svg)](https://phpackages.com/packages/aomega-pharel)
```

###  Alternatives

[symfony/polyfill-intl-idn

Symfony polyfill for intl's idn\_to\_ascii and idn\_to\_utf8 functions

3.4k774.6M90](/packages/symfony-polyfill-intl-idn)[thecodingmachine/safe

PHP core functions that throw exceptions instead of returning FALSE on error

2.5k95.7M455](/packages/thecodingmachine-safe)[php-cs-fixer/shim

A tool to automatically fix PHP code style

386.3M324](/packages/php-cs-fixer-shim)

PHPackages © 2026

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