PHPackages                             asko/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. [Database &amp; ORM](/categories/database)
4. /
5. asko/orm

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

asko/orm
========

A object relational mapper with a query builder and out of box support for MySQL databases.

v1.3.1(1y ago)11MITPHPPHP &gt;=8.4CI passing

Since Jun 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/askonomm/orm)[ Packagist](https://packagist.org/packages/asko/orm)[ RSS](/packages/asko-orm/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (4)Versions (8)Used By (0)

ORM
===

[](#orm)

[![codecov](https://camo.githubusercontent.com/2c1b2252be5d25dc551e8defa8f6628931b278b86dacfed12b16420fc3802849/68747470733a2f2f636f6465636f762e696f2f67682f61736b6f6e6f6d6d2f6f726d2f67726170682f62616467652e7376673f746f6b656e3d495a5445554956444647)](https://codecov.io/gh/askonomm/orm)

A object relational mapper with a query builder and out of box support for MySQL databases.

Installation
------------

[](#installation)

```
composer require asko/orm

```

Set-up
------

[](#set-up)

To start with create your base model class that your actual data models will be extending, like so:

```
use Asko\Orm\BaseModel;
use Asko\Orm\Drivers\MysqlDriver;

/**
 * @template T
 * @extends BaseModel
 */
class Model extends BaseModel
{
  public function __construct()
  {
    parent::__construct(new MysqlDriver(
      host: "",
      name: "",
      user: "",
      password: "",
      port: 3006
    ));
  }
}
```

And then you can create all your data models like this:

```
use Asko\Orm\Column;

/**
 * @extends Model
 */
class User extends Model
{
  protected static string $_table = "users";
  protected static string $_identifier = "id";

  #[Column]
  public int $id;

  #[Column]
  public string $name;

  #[Column]
  public string $email;
}
```

And woalaa, you have an ORM mapping data classes to tables in the database all with full type support (works especially well with PHPStan).

Note that the `$_identifier` should match the name of the primary key column, which in the above case is `id`, and the `$_table` should match the name of the database table, naturally. The other properties here represent the columns of the table, these will be populated by the ORM automatically when querying data and have to have the `Column` attribute.

Querying
--------

[](#querying)

You can query for data using the numerous query builder methods built into ORM.

An example query looks like this:

```
$user = (new User)
  ->query()
  ->where('id', '=', 1)
  ->first();
```

Although because we use the primary key identifier to search for the user, the above could be simplified as:

```
$user = (new User)->find(1);
```

### All query methods

[](#all-query-methods)

#### `where`

[](#where)

Where clause to filter the results.

Usage:

```
(new User)->query()->where('id', '=', 1);
// or
(new User)->query()->where('id', '>', 1);
```

#### `andWhere`

[](#andwhere)

Same as `where` but with `AND` operator.

Usage:

```
(new User)->query()->where('id', '=', 1)->andWhere('email', '=', 'john@smith.com');
```

#### `orWhere`

[](#orwhere)

Same as `where` but with `OR` operator.

Usage:

```
(new User)->query()->where('id', '=', 1)->orWhere('email', '=', 'john@smith.com');
```

#### `orderBy`

[](#orderby)

Order the results by a column.

Usage:

```
(new User)->query()->orderBy('id', 'asc');
```

#### `limit`

[](#limit)

Limit the number of results.

Usage:

```
(new User)->query()->limit(10);
```

#### `offset`

[](#offset)

Offset the results.

Usage:

```
(new User)->query()->offset(10);
```

#### `join`

[](#join)

Join another table.

Usage:

```
(new User)->query()->join('posts', 'posts.user_id', '=', 'users.id');
```

#### `leftJoin`

[](#leftjoin)

Join another table with a left join.

Usage:

```
(new User)->query()->leftJoin('posts', 'posts.user_id', '=', 'users.id');
```

#### `rightJoin`

[](#rightjoin)

Join another table with a right join.

Usage:

```
(new User)->query()->rightJoin('posts', 'posts.user_id', '=', 'users.id');
```

#### `innerJoin`

[](#innerjoin)

Join another table with an inner join.

Usage:

```
(new User)->query()->innerJoin('posts', 'posts.user_id', '=', 'users.id');
```

#### `outerJoin`

[](#outerjoin)

Join another table with an outer join.

Usage:

```
(new User)->query()->outerJoin('posts', 'posts.user_id', '=', 'users.id');
```

#### `raw`

[](#raw)

Add a raw SQL to the query.

Usage:

```
(new User)->query()->raw('WHERE id = ?', [1]);
```

#### `get`

[](#get)

Get all the results.

Usage:

```
(new User)->query()->get();
```

#### `first`

[](#first)

Get the first result.

Usage:

```
(new User)->query()->first();
```

#### `last`

[](#last)

Get the last result.

Usage:

```
(new User)->query()->last();
```

Creating
--------

[](#creating)

To create a new record in the database, you can do the following:

```
$user = new User;
$user->name = "John Smith";
$user->email = "john@smith.com"
$user->store();
```

Updating
--------

[](#updating)

To update a record in the database, you can do the following:

```
$user = (new User)->find(1);
$user->name = "John Doe";
$user->store();
```

Deleting
--------

[](#deleting)

To delete a record in the database, you can do the following:

```
$user = (new User)->find(1);
$user->delete();
```

Creating a connection driver
----------------------------

[](#creating-a-connection-driver)

ORM comes with the MySQL driver already built in, but if you wish to extend the ORM to support other databases, you can do so by creating a new driver class that implements the `ConnectionDriver` interface, and if you need to also build a new query builder due to syntax differences with the MySQL query builder, you can do so by creating a new query builder class that implements the `QueryBuilder` interface.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance50

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

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

Recently: every ~82 days

Total

7

Last Release

365d ago

PHP version history (2 changes)v1.0PHP &gt;=8.2

v1.3.1PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/1610a37b5e2e2ae8c28051865e6a61fcebb450c3908aedf0a8501bd6967f9da8?d=identicon)[asko](/maintainers/asko)

---

Top Contributors

[![askonomm](https://avatars.githubusercontent.com/u/84135165?v=4)](https://github.com/askonomm "askonomm (24 commits)")

---

Tags

activerecordmysqlormphpquery-builder

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  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)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

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

Reliese Components for Laravel Framework code generation.

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

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

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