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

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

itvisionsy/simple-orm
=====================

A simple PHP class to access your MySQL records.

v1.0.0(9y ago)241656MITHTMLPHP &gt;=5.4

Since Mar 26Pushed 9y ago3 watchersCompare

[ Source](https://github.com/itvisionsy/php-simple-orm)[ Packagist](https://packagist.org/packages/itvisionsy/simple-orm)[ RSS](/packages/itvisionsy-simple-orm/feed)WikiDiscussions master Synced 4w ago

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

![PHP](https://camo.githubusercontent.com/32ea39bffdf5bab4bf166d71cf72d750e3a45c3b9018c77b14131dc681bb9353/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e342b2d3446354239332e737667)[![Packagist](https://camo.githubusercontent.com/a6e7d38bc15dbcee5dd428efb26e096173ca801de8c08f2c7645aa1a30e260d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6974766973696f6e73792f73696d706c652d6f726d2e737667)](https://packagist.org/packages/itvisionsy/simple-orm)[![Build Status](https://camo.githubusercontent.com/37819bd7e99ac93a18c0acea7f705335114f76fecf8b12a9b428548daa69bbe8/68747470733a2f2f7472617669732d63692e6f72672f6974766973696f6e73792f7068702d73696d706c652d6f726d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/itvisionsy/php-simple-orm)![license](https://camo.githubusercontent.com/e6863d269098037fccdaf69e0231afd79248a54e6bdf0b6e17beee2ceb748868/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6974766973696f6e73792f6c61726176656c2d6d6f64756c65732e737667)

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

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 86.8% 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

3383d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/11ac511b34acf3f7e9a138d3b1a0a868303b376ab991bb176e8ea474857a0e57?d=identicon)[mhh1422](/maintainers/mhh1422)

---

Top Contributors

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

---

Tags

databasemysqlormpdophpphpdatabaseormmysqlpdo

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k23.1k](/packages/clouddueling-mysqldump-php)[riverside/php-orm

PHP ORM micro-library and query builder

111.3k](/packages/riverside-php-orm)[ramadan/easy-model

A Laravel package for enjoyably managing database queries.

111.6k](/packages/ramadan-easy-model)

PHPackages © 2026

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