PHPackages                             vitodtagliente/pure-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. vitodtagliente/pure-orm

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

vitodtagliente/pure-orm
=======================

The Pure ORM Component

151PHP

Since Jan 21Pushed 4y ago1 watchersCompare

[ Source](https://github.com/vitodtagliente/pure-orm)[ Packagist](https://packagist.org/packages/vitodtagliente/pure-orm)[ RSS](/packages/vitodtagliente-pure-orm/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Pure ORM Component
==================

[](#pure-orm-component)

Object-Relational Mapping (ORM)

How To Connect:
===============

[](#how-to-connect)

1. Instantiate the Database connection giving database settings ```
    $db = new Pure\ORM\Database("mysql", "hostname", "database", "user", "password", array(PDO_attributes));
    ```

    or bind a ready PDO instance ```
    $db = Pure\ORM\Database::bind($pdo);
    ```
2. It is also possible to define the database settings and apply for connection at the first time in which a query is called. This means that no database connection time is lost in pages in which there are no data queries. ```
    Pure\ORM\Database::bind("mysql", "hostname", "database", "user", "password", array(PDO_attributes));
    ```
3. After the step 1 or 2, the database instance can be globally accessed by the singleton pattern: ```
    $db = Pure\ORM\Database::main();
    ```
4. It is possible to change the current database instance: ```
    $db1 = new Pure\ORM\Database(....);
    $db2 = new Pure\ORM\Database(....);

    $current = Pure\ORM\Database::main(); // this refers to $db2
    Pure\ORM\Database::change($db1);
    $current = Pure\ORM\Database::main(); // this refers to $db1
    ```
5. Close the connection by: ```
    $db->close();
    ```

How To execute queries:
-----------------------

[](#how-to-execute-queries)

1. Getting a row: ```
    $data = $db->fetch("SELECT email, username FROM users WHERE username = ?", array("Mario"));
    ```

    Or ```
    $data = $db->select("users", array( 'email', 'username' ), "username = Mario" );
    ```
2. Getting multiple rows: ```
    $data = $db->fetchAll("SELECT id, username FROM users");
    ```

    Or ```
    $data = $db->selectAll("users", array( 'email', 'username' ), $condition);
    ```
3. Insert a row: ```
    $result = $db->insert("users", array( "name" => "Mario", "email" => "mario.rossi@email.com"));
    ```

    $result will be true or false
4. Update existing row: ```
    $result = $db->update("users", "id = 1", array( "name" => "Roberto" ) );
    ```
5. Remove rows: ```
    $result = $db->delete("users", "id = 1");
    ```

In order to **debug** the code, during the development phase, the error reporting should be activated

```
Pure\ORM\Database::main()->error_reporting(true);
```

How To define Models
====================

[](#how-to-define-models)

The Model class let to map Schema and data to objects. First of all, a model class declaration is required. The define function let the developer to specify the model's properties.

```
class User extends Model
{
    public static function define($schema)
    {
        $schema->id();
        $schema->char('username')->unique();
        $schema->char('password');
        $schema->char('email');
        $schema->boolean('active')->default(true);
    }
}
```

Once a model is defined, it is easy to map data and queries with objects.

1. Instantiate the model:

    ```
    $model = new User;
    $model->username = 'mariorossi98';
    $model->password = 'mypassword';
    $model->email = 'mario.rossi@mai.it';

    $model->save();
    ```

    The save method can be used to insert or update sql data.

    The framework will perform this choice by itself.
2. Find models:

    ```
    $model = User::find('id = 1');
    ```
3. Delete data:

    ```
    $model = User::find('id = 1');
    // ....
    $model->erase();
    ```
4. Retrieve multiple models:

    ```
    $models = User::all($condition = null);
    ```
5. Get the table name:

    ```
    $table_name = User::table();
    ```
6. Use the methods 'data' and 'json' to encode the model to a simple reprensetation that should by easily shared.

    ```
    var_dump($model->data());
    /*
    [
      'id' => 1,
      'username' => 'mariorossi98',
      'password' => 'mypassword',
      'email' => 'mario.rossi@mai.it'
      'active' => 1
    ]
    */

    var_dump($model->json());
    /*
    {
      "id":1,
      "username":"mariorossi98",
      "mypassword":"root",
      "email":"mario.rossi@mai.it",
      "active":1
    }
    */
    ```

#### How To specify the model's table name

[](#how-to-specify-the-models-table-name)

If not speficied, the table name will be the model class name + 's'.

For example, User -&gt; 'Users'. Otherwise, is it also possible to specify the table:

```
class User extends Model
{
    public static function table()
    {
        return 'my_custom_table_name';
    }

    public static function define($schema)
    {
        $schema->id();
        $schema->char('username')->unique();
        $schema->char('password');
        $schema->char('email');
        $schema->boolean('active')->default(true);
    }
}
```

#### How To define the model's properties

[](#how-to-define-the-models-properties)

```
- $schema->id($name = 'id') // adds an id property (integer, primary, increments)
- $schema->boolean($name)
- $schema->integer($name)
- $schema->float($name)
- $schema->char($name, $size = 30)
- $schema->text($name)
- $schema->date($name)
- $schema->time($name)
- $schema->datetime($name)
- $schema->timestamps()     // adds two datetime properties: 'created_at', 'updated_at'
```

Properties can be specified by descriptors:

```
- default($value)   // specifies the property's default value
- increments()      // 'auto_increment'
- nullable()        // by default, all properties are not nullable
- primary()         // primary key
- unique()          // unique value
- unsigned()        // valid for numbers
- link(OtherModelClass, OtherModelProperty) // foreign key
```

Property's descriptors can be concatenated like in the examples below:

```
$schema->integer('id')->increments()->primary();
$schema->text('notes')->nullable();
$schema->char('username', 50)->unique();
$schema->boolean('active')->default(true);
```

According to define foreing keys, use the 'link' descriptor, link in this example

```
class Book extends Model
{
    public static function define($schema)
    {
        $schema->id();
        $schema->char('name')->unique();
        $schema->integer('bought_by')->link(User::class, 'id');
    }
}
```

Schema Management
=================

[](#schema-management)

The Schema utility can be used to create, delete and check the existence of a named table into the database.

1. Check if a table exists:

    ```
    $result = Pure\ORM\Schema::exists(User::class);
    ```
2. Delete a table:

    ```
    $result = Pure\ORM\Schema::drop(User::class);
    ```
3. Create a table:

    ```
    $result = Pure\ORM\Schema::create(User::class);
    ```
4. Is also possible to delete all the table entries:

    ```
    $result = Pure\ORM\Schema::clear(User::class);
    ```

It is possible to fill test data at the schema's creation time. To perform this operation, the seed function in the Model should be overriden.

```
class User extends Model
{
    public static function define($schema)
    {
        $schema->id();
        $schema->char('username')->unique();
        $schema->char('password');
        $schema->char('email');
        $schema->boolean('active')->default(true);
    }

    // executed only at the schema creation
    public static function seed()
    {
        $model = new User;
        $model->username = 'test';
        $model->password = 'test';
        $model->email = 'test@mail.com';
        $model->save();

        // ... other models
    }
}
```

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity27

Early-stage or recently created project

 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/61137c8fad7251ed9bcd84692d5ef8153dc8de8c6a09d182b9a1d69854d2a0b2?d=identicon)[vitodtagliente](/maintainers/vitodtagliente)

---

Top Contributors

[![vitodtagliente](https://avatars.githubusercontent.com/u/5153783?v=4)](https://github.com/vitodtagliente "vitodtagliente (28 commits)")

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/vitodtagliente-pure-orm/health.svg)](https://phpackages.com/packages/vitodtagliente-pure-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.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M543](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

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

PHPackages © 2026

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