PHPackages                             reneschmidt/simpleorm - 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. reneschmidt/simpleorm

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

reneschmidt/simpleorm
=====================

Simple, no-frills ORM class.

v0.4.1(10y ago)31311GPL-3.0PHPPHP &gt;=5.3.0

Since Feb 25Pushed 8y ago3 watchersCompare

[ Source](https://github.com/rene-s/SimpleOrm)[ Packagist](https://packagist.org/packages/reneschmidt/simpleorm)[ Docs](https://reneschmidt.de/wiki/index.php/page/view/SimpleOrm,Start)[ RSS](/packages/reneschmidt-simpleorm/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (8)Used By (0)

SimpleOrm
=========

[](#simpleorm)

Immature. Do not use.

This is a no-frills simple [ORM](https://en.wikipedia.org/wiki/Object-relational_mapping) class for PHP/sqlite and MySQL. Project goals are to

- provide ORM functionality
- be as simple and small as possible
- be clean.

It is **not** a goal to be compatible with other DBMS than sqlite and MySQL (at the moment) or to implement feature X that other ORM Y already has. It might not even fit into the traditional ORM paradigm.

German Web Application Developer Available for Hire!
----------------------------------------------------

[](#german-web-application-developer-available-for-hire)

No marketing skills whatsoever, but low rates, nearly 20 years of experience, and german work attitude.

Get in touch now:

[![Build Status](https://camo.githubusercontent.com/e199790923d8f327a4003bbcff4bae78ac54db7ba39677c8d3d16dc663f5562f/68747470733a2f2f7472617669732d63692e6f72672f72656e652d732f53696d706c654f726d2e737667)](https://travis-ci.org/rene-s/SimpleOrm)[![License](https://camo.githubusercontent.com/e1ab470d208017042b16e7dacec17c908fbf363b37409f1624e5b78692a01874/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4c47504c2d626c75652e737667)](https://opensource.org/licenses/LGPL-3.0)

Requirements
============

[](#requirements)

PHP 5.3 + php-sqlite

Download
========

[](#download)

- [Source can be found and downloaded from Github](https://github.com/rene-s/SimpleOrm)

Author
======

[](#author)

Me:

1.
2. [I am available for hire](mailto:rene+_gth@sdo.sh)

Licence
=======

[](#licence)

GPL v3 or commercial licence :) from [rene+\_gth@sdo.sh](mailto:rene+_gth@sdo.sh). Do not use this in your closed source project without paying me. I don't like that.

How to use
==========

[](#how-to-use)

First, SimpleOrm supports sqlite and MySQL at the moment. Secondly, SimpleOrm expects every table to have a numeric PK and it must be given as the first field.

*Set variables*

```
// example Sqlite memory database
$dsn = 'sqlite::memory:';

// OR: example Sqlite file database
$dsn = 'sqlite:/tmp/db.sqlite';

// OR: example MySQL database on localhost
$dsn = 'mysql:host=localhost;port=3306;dbname=testdb';
$dsn = 'mysql:unix_socket=/tmp/mysql.sock;dbname=testdb';

// For MySQL, also define user name and password. **NOT** used for Sqlite.
$user = 'root';
$pass = 'root';

// Set up DB connection
$simpleDb = SimpleDb::getInst($dsn, $user, $pass);

// You need to provide your own implementation of SimpleDbConfig (here we use SampleDbConfig)
$sampleDbConfig = SampleDbConfig::getInst($simpleDb);

// Setup will create the database and the tables according to your SampleDbConfig implementation
// Obviously you want this to execute only during installation of the app.
$sampleDbConfig->setUp();

```

*Provide database setup class*

This is not SimpleOrm-specific. You can do this any way you want. I would recommend a similar setup like in this package: SampleDbConfig. The setUp() method in it creates required tables. Be sure to execute this setup only when the database does not exist yet.

*Create model class for each table. Example:*

Let's assume you have a table like this:

```
CREATE TABLE sample (
 "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT null,
 "some_name" TEXT NOT null,
 "bitmask" INTEGER NOT null DEFAULT (0)
);

```

Then create an appropriate model class like this:

```
/**
 * Sample Model instance.
 *
 * Define correct type hinting like this:
 *
 * @method Sample findOneBy($field, $value, $fetchMode = \PDO::FETCH_OBJ)
 * @method Sample[] findBy($field, $value, $fetchMode = \PDO::FETCH_OBJ)
 * @method Sample[] findByQuery($query, array $values, $fetchMode = \PDO::FETCH_OBJ)
 * @method Sample[] collectRecords(\PDOStatement $sth, $fetchMode = \PDO::FETCH_OBJ)
 */
class Sample extends SimpleOrm
{
 /**
  * Array with table fields
  *
  * @var array
  */
 protected $payload = array(
   "id" => null, // first field is primary key
   "some_name" => null,
   "bitmask" => null
 );

 /**
  * @var string
  */
 protected static $table = 'sample';
}

```

That's it.

*How to use*

There are different methods of creating new records:

```
$sample = new Sample(array("some_name" => "abc", "bitmask" => 0));
$sample->save();

$sample = Sample::getInst(array("some_name" => "abc", "bitmask" => 0));
$sample->save();

$sample = new Sample();
$sample->set("bitmask", 0);
$sample->set("some_name", "abc");
$sample->save();

```

How to retrieve records:

```
$sample = Sample::getInst()->findOneBy("some_name", "abc"); // returns record of type "Sample"
print($sample->get("some_name")); // prints "abc"

$samples = Sample::getInst()->findBy("some_name", "abc"); // returns array with "Sample" items
$samples = Sample::getInst()->findBy("some_name", "abc", \PDO\FETCH_ASSOC); // returns array with "Sample" array

foreach($samples AS $sample) {
    print($sample->get("some_name")); // prints "abc"
}

$samples = Sample::getInst()->findByQuery("SELECT * FROM sample WHERE some_name = ?", ["abc"]);

foreach($samples AS $sample) {
    print($sample->get("some_name")); // prints "abc"
}

// apply filter
$samples = Sample::getInst()->setFilter(function($inst) {
    $inst['some_name'] = $inst['some_name'] . 'x'; // apply filter to array
    return $inst;
})->findByQuery("SELECT * FROM sample WHERE some_name = ?", ["abc"]);

foreach($samples AS $sample) {
    print($sample->get("some_name")); // prints "abcx"
}

```

How to update and delete records:

```
$sample = Sample::getInst()->findOneBy("some_name", "abc"); // returns record of type "Sample"
print($sample->get("some_name")); // prints "abc"

$sample->set("some_name", "def");
$sample->save(); // record now has value "def" for "some_name"

print($sample->get("some_name")); // prints "def"

$sample->del(); // record is deleted now.

```

Full example:

```
