PHPackages                             met\_mw/sorm - 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. met\_mw/sorm

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

met\_mw/sorm
============

Simple and lightweight ORM library.

v1.0.0(9y ago)01031MITPHPPHP &gt;=5.6.0

Since Feb 25Pushed 9y ago1 watchersCompare

[ Source](https://github.com/met-mw/SORM)[ Packagist](https://packagist.org/packages/met_mw/sorm)[ RSS](/packages/met-mw-sorm/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (1)

[![Build Status](https://camo.githubusercontent.com/88343c8695539ce611dde30e14bf5bffad9ea5cd7cde2720db9529e762b97a07/68747470733a2f2f7472617669732d63692e6f72672f6d65742d6d772f534f524d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/met-mw/SORM)[![Coverage Status](https://camo.githubusercontent.com/c98458b2e9768a23894930322c3e6799a925dbf8e1ab48d4efb5828caee5fc58/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d65742d6d772f534f524d2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/met-mw/SORM?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/3973543cc7b7c92427b2cc0a044218100ccd99af8836e6048b724a2fa15da5ba/68747470733a2f2f706f7365722e707567782e6f72672f6d65745f6d772f736f726d2f762f737461626c65)](https://packagist.org/packages/met_mw/sorm)[![Latest Unstable Version](https://camo.githubusercontent.com/a9cde942fb56cca1389d319382568aa03818f52d4ff8ebb0a78291f8a4c4d4bf/68747470733a2f2f706f7365722e707567782e6f72672f6d65745f6d772f736f726d2f762f756e737461626c65)](https://packagist.org/packages/met_mw/sorm)[![Total Downloads](https://camo.githubusercontent.com/52533f6b6902c5ad6d1561bb1e18c098d66e0ecd3b44327102b3816dc711c538/68747470733a2f2f706f7365722e707567782e6f72672f6d65745f6d772f736f726d2f646f776e6c6f616473)](https://packagist.org/packages/met_mw/sorm)[![License](https://camo.githubusercontent.com/eef0b02ef64ffb45ac1cf44a109dc08411484f4ed31bedb7cc9eae1f5536b978/68747470733a2f2f706f7365722e707567782e6f72672f6d65745f6d772f736f726d2f6c6963656e7365)](https://packagist.org/packages/met_mw/sorm)

SORM - Simple ORM (Object-Relational Mapping)
=============================================

[](#sorm---simple-orm-object-relational-mapping)

Getting started
---------------

[](#getting-started)

> Attention! It's works with MySQL/MariaDB only.

### Install

[](#install)

```
composer require met_mw/sorm

```

### Start

[](#start)

> DataSource::i() - Get data source instance. DataSource::d() - Get current driver.

```
// Add new driver (MySQL)
DataSource::i()->addDriver('mysql', new Mysql('127.0.0.1', 'db_name', 'charset', 'root', 'password'));
// Set current driver (if added only one driver, then it already current driver)
DataSource::i()->setCurrentDriver('mysql');
DataSource::d()->connect();

```

### Models

[](#models)

The model is extended from the base class Entity and specify the database table name.

```
/**
  * FirstModel
  *
  * @property int $id
  * @property string $name
  */
class FirstModel extends Entity
{
    protected $tableName = 'first_model_table_name';
}

/**
  * FirstModelChild
  *
  * @property int $id
  * @property string $name
  * @property int $first_model_id
  */
class FirstModelChild extends Entity
{
    protected $tableName = 'fist_model_child_table_name';
}

```

### Relations

[](#relations)

Relations are realized through the public model methods.

```
/**
  * FirstModel
  *
  * @property int $id
  * @property string $name
  */
class FirstModel extends Entity
{
    protected $tableName = 'first_model_table_name';

    public function getFirstModelChilds()
    {
        /** @var FirstModelChild[] $aFirstModelChilds */
        $aFirstModelChilds = $this->findRelationCache('id', FirstModelChild::cls()); // Search in cache
        if (empty($aFirstModelChilds)) { // If no cache, then load data from data source
            $oFirstModelChilds = DataSource::i()->factory(FirstModelChild::cls()); // Create empty model
            $oFirstModelChilds->getQueryBuilder()
                ->where('first_model_id', '=',  $this->id); // Build search conditions
            $aFirstModelChilds = $oFirstModelChilds->loadAll();

            // Caching data
            foreach ($aFirstModelChilds as $oFirstModelChild) {
                $this->addRelationCache('id', $oFirstModelChild);
                $oFirstModelChild->addRelationCache('first_model_id', $this);
            }
        }

        return $aFirstModelChilds;
    }
}

```

```
/**
  * FirstModelChild
  *
  * @property int $id
  * @property string $name
  * @property int $first_model_id
  */
class FirstModelChild extends Entity
{
    protected $tableName = 'fist_model_child_table_name';

    public function getFirstModel()
    {
        /** @var FirstModel[] $aFirstModels */
        $aFirstModels = $this->findRelationCache('id', FirstModel::cls()); // Search in cache
        if (empty($aFirstModels)) { // If no cache, then load data from data source
            $oFirstModels = DataSource::i()->factory(FirstModel::cls()); // Create empty model
            $oFirstModels->getQueryBuilder()
                ->where('id', '=', $this->first_model_id); // Build search conditions
            $aFirstModels = $oFirstModels->loadAll();

            // Caching data
            foreach ($aFirstModels as $oFirstModel) {
                $this->addRelationCache('first_model_id $oFirstModel);
                $oFirstModel->addRelationCache('id', $this);
            }
        }

        return isset($aFirstModels[0]) ? $aFirstModels[0] : null;
    }
}

```

### Example

[](#example)

#### Create new entity

[](#create-new-entity)

```
/** @var FirstModel $oFirstModel */
$oFirstModel = DataSource::i()->factory(FirstModel::cls()); // Create empty entity
$oFirstModel->name = 'Test name'; // Set entity data
$oFirstModel->save(); // Save entity data into data source

```

#### Load entity

[](#load-entity)

```
/** @var FirstModel $oFirstModel */
$oFirstModel = DataSource::i()->factory(FirstModel::cls(), 1); // Load entity with Pk value 1
echo $oFirstModel->name; // Any use data
$oFirstModel->name = 'New test name'; // Change data
$oFirstModel->save(); // Save entity data into data source

```

#### Using relations

[](#using-relations)

```
/** @var FirstModel $oFirstModel */
$oFirstModel = DataSource::i()->factory(FirstModel::cls(), 1); // Load entity with Pk value 1
$aFirstModelChilds = $oFirstModel->getFirstModelChilds(); // Load related entities

```

#### Free queries

[](#free-queries)

##### Get data as array

[](#get-data-as-array)

```
DataSource::d()->query('select * from `table_name`'); // Execute query
$result = DataSource::d()->fetchAll(); // Fetch all data as array

```

##### Get data as assoc array

[](#get-data-as-assoc-array)

```
DataSource::d()->query('select * from `table_name`'); // Execute query
$result = DataSource::d()->fetchAllAssoc(); // Fetch all data as assoc array

```

##### Get data row as array

[](#get-data-row-as-array)

```
DataSource::d()->query('select * from `table_name`'); // Execute query
$result = DataSource::d()->fetchRow(); // Fetch data row as array

```

##### Get data row as assoc array

[](#get-data-row-as-assoc-array)

```
DataSource::d()->query('select * from `table_name`'); // Execute query
$result = DataSource::d()->fetchRowAssoc(); // Fetch data row as assoc array

```

License
-------

[](#license)

The met-mw/SORM package is open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3413d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3956926?v=4)[Михаил Владимирович](/maintainers/met-mw)[@met-mw](https://github.com/met-mw)

---

Top Contributors

[![met-mw](https://avatars.githubusercontent.com/u/3956926?v=4)](https://github.com/met-mw "met-mw (109 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/met-mw-sorm/health.svg)

```
[![Health](https://phpackages.com/badges/met-mw-sorm/health.svg)](https://phpackages.com/packages/met-mw-sorm)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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