PHPackages                             noetix/simple-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. noetix/simple-orm

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

noetix/simple-orm
=================

A simple PHP class to access your MySQL records.

13424043PHP

Since Dec 11Pushed 9y ago10 watchersCompare

[ Source](https://github.com/noetix/Simple-ORM)[ Packagist](https://packagist.org/packages/noetix/simple-orm)[ RSS](/packages/noetix-simple-orm/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Simple ORM
==========

[](#simple-orm)

Simple ORM is an object-relational mapper for PHP &amp; MySQL (using mysqli). It provides a simple way to create, retrieve, update &amp; delete records.

This is not intended for large scale projects as it has extra database interaction than necessary. I would suggest [Doctrine](http://www.doctrine-project.org/) for such things.

Simple ORM is a few years old but it has been upgraded over time, therefore **PHP 5.3** is a requirement.

Configuration
-------------

[](#configuration)

To utilise this tool you need to do a few things:

1. Include the class file.
2. Create a `mysqli` object.
3. Tell `SimpleOrm` to use the `mysqli` connection.

For example:

```
// Include the Simple ORM class
include 'SimpleOrm.class.php';

// Connect to the database using mysqli
$conn = new mysqli('host', 'user', 'password');

if ($conn->connect_error)
  die(sprintf('Unable to connect to the database. %s', $conn->connect_error));

// Tell Simple ORM to use the connection you just created.
SimpleOrm::useConnection($conn, 'database');

```

Object/Table Definition
-----------------------

[](#objecttable-definition)

Define an object that relates to a table.

```
class Blog extends SimpleOrm { }

```

Basic Usage
-----------

[](#basic-usage)

Create an entry:

```
$entry = new Blog;
$entry->title = 'Hello';
$entry->body = 'World!';
$entry->save();

```

Retrieve a record by it's primary key:

```
$entry = Blog::retrieveByPK(1);

```

Retrieve a record using a column name:

```
$entry = Blog::retrieveByTitle('Hello', SimpleOrm::FETCH_ONE);

```

Update a record:

```
$entry->body = 'Mars!';
$entry->save();

```

Delete the record:

```
$entry->delete();

```

Class Configuration
===================

[](#class-configuration)

This section will detail how you define your objects and how they relate to your MySQL tables.

A Basic Object
--------------

[](#a-basic-object)

```
class Foo extends SimpleOrm {}

```

Class Naming
------------

[](#class-naming)

The following assumptions will be made for all objects:

1. The database used is the database loaded in the `mysqli` object.
2. The table name is the class name in lower case.
3. The primary key is `id`.

Customisation
-------------

[](#customisation)

You can customise the assumptions listed above using the following static properties:

- database
- table
- pk

For example:

```
class Foo extends SimpleOrm
{
    protected static
      $database = 'test',
      $table = 'foobar',
      $pk = 'fooid';
}

```

Data Manipulation
=================

[](#data-manipulation)

This section will detail how you modify your records/objects.

Creating/Inserting New Records
------------------------------

[](#creatinginserting-new-records)

You can start a new instance &amp; save the object or you can feed it an array.

```
$foo = new Foo;
$foo->title = 'hi!';
$foo->save();

```

or

```
$foo = new Foo(array('title'=>'hi!'));
$foo->save();

```

Updating
--------

[](#updating)

Simply modify any property on the object &amp; use the save() method.

```
$foo->title = 'hi!';
$foo->save();

```

If you want to have some more control over manipulating data you can use set(), get() &amp; isModified().

```
$foo->set('title', 'hi!');
$foo->save();

```

Deleting
--------

[](#deleting)

Use the delete() method.

```
$foo->delete();

```

Data Retrieval
==============

[](#data-retrieval)

This section will detail how you fetch data from mysql and boot your objects.

Using the Primary Key
---------------------

[](#using-the-primary-key)

```
$foo = Foo::retrieveByPK(1);

```

or

```
$foo = new Foo(1);

```

Using a Column Name
-------------------

[](#using-a-column-name)

```
$foo = Foo::retrieveByField('bar', SimpleOrm::FETCH_ONE);

```

By default, the retrieveBy\* method will return an array of objects (SimpleOrm::FETCH\_MANY).

Select All
----------

[](#select-all)

```
$foo = Foo::all();

```

Fetch Constants
---------------

[](#fetch-constants)

`SimpleOrm::FETCH_ONE` will return a single object or null if the record is not found.

`SimpleOrm::FETCH_MANY` will always return an array of hydrated objects.

Populating from an Array (Hydration)
------------------------------------

[](#populating-from-an-array-hydration)

You can pass in an associative array to populate an object. This saves retrieving a record each time from the database.

```
$foo = Foo::hydrate($array);

```

SQL Statements
--------------

[](#sql-statements)

Any SQL statement can be used as long as all the returning data is for the object.

Example

```
$foo = Foo::sql("SELECT * FROM :table WHERE foo = 'bar'");

```

This will return an array of hydrated Foo objects.

If you only want a single entry to be returned you can request this.

```
$foo = Foo::sql("SELECT * FROM :table WHERE foo = 'bar'", SimpleOrm::FETCH_ONE);

```

SQL Tokens
----------

[](#sql-tokens)

The table name &amp; primary key have shortcuts to save you writing the same names over &amp; over in your SQL statements:

- `:database` will be replaced with the database name.
- `:table` will be replaced with the table name.
- `:pk` will be replaced with the primary key field name.

Extra Data &amp; Aggregate Functions
------------------------------------

[](#extra-data--aggregate-functions)

Any extra data that does not belong to object being loaded will have those fields populated in the object:

```
$foo = Foo::sql("SELECT *, 'hi' AS otherData FROM :table WHERE foo = 'bar'", SimpleOrm::FETCH_ONE);

echo $foo->otherData; // returns 'hi'

```

This can be useful if you plan to use aggregate functions within your object &amp; you want to pre-load the data.

Filters
=======

[](#filters)

Input &amp; output filters can be created to alter data going into the database &amp; when it comes out.

To add a filter, you only need to create a method with either filterIn or filterOut as a prefix (e.g. filterIn, filterInHi, filterIn\_hi).

These methods will automatically fire when data is loaded into the object (hydration) or saved into the database (save, update, insert).

Input Filters
-------------

[](#input-filters)

Data being saved to the database can be modified.

For example:

```
class Foo extends SimpleOrm
{
    protected function filterIn_dates ($data)
    {
        $data['updated_at'] = time();

        return $data;
    }
}

```

In the example above, every time the object is saved, the updated\_at field is populated with a current time &amp; date.

Note: You must return the input array otherwise no fields will be updated.

Output Filters
--------------

[](#output-filters)

Any data loaded into the object will be passed through any output filters.

```
class Foo extends SimpleOrm
{
    protected function filterOut ()
    {
        $this->foo = unserialize($this->foo);
    }
}

```

In the example above, each time the object is hydrated, the foo property is unserialized.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community16

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/3e4fe1bd039de663f138b1b0004e1d8c89b141551cd296ba3be89bf92f3d79a8?d=identicon)[noetix](/maintainers/noetix)

---

Top Contributors

[![noetix](https://avatars.githubusercontent.com/u/1192279?v=4)](https://github.com/noetix "noetix (5 commits)")

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/noetix-simple-orm/health.svg)](https://phpackages.com/packages/noetix-simple-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)
