2017 © Pedro Peláez
 

php-library simpleorm

Simple php orm.

image

twodudes/simpleorm

Simple php orm.

  • Tuesday, January 24, 2017
  • by megawilddaddy
  • Repository
  • 2 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Simpleorm

Super simple ORM on top of PDO. Based on the data mapper pattern., (*1)

Installation

Installation with composer:, (*2)

composer require twodudes/simpleorm

Quickstart

The model

use Simpleorm\Mapper\Pdo\PdoMapper; /** * @property int $id * @property string $name {"default": "John"} * * @method static PdoMapper getMapper() {"table": "test"} */ class User extends AbstractModel { }

Fields are defined as doc properties, because this is * convenient * easily recognizeable by the IDE * you dont have the unnecessary boilerplate code with getters and setters, (*3)

The mapper

The mapper once again is defined in the doc comments section. Here we specify, that our user will be handled with the PdoMysqlMapper, (*4)

@method static PdoMapper getMapper() {"table": "test"}

To get the mapper, simply use, (*5)

User::getMapper();

We use a mix of active record and data mapper aproach to managing your entities, therefore you can use it like this:, (*6)

$user = User::getMapper()->fetch(1);
$user->name = 'NewName';
$user->save();

If you want your custom logic and methods, just extend the desired mapper and you are good to go:, (*7)

class UserMapper extends PdoMysqlMapper 
{
    public function fetchComplicatedReport($someParams)
    {
        $stmt = $this->getConnection()->prepare('...');
        $stmt->execute();

        return $stmt->fetchAll();
    }
}

Use this mapper in your model, (*8)

/**
 * @property int    $id
 *
 * @method static UserMapper getMapper() {"table": "test"}
 */
class User extends AbstractModel
{
}

And in your service/controller:, (*9)

$report = User::getMapper()->fetchComplicatedReport();
The collection

By default the fetchAll mapper method will return a collection. It is more convenient to work with, than just plain array, and has a lot of usefull methods., (*10)

$users = User::getMapper()->fetchAll(['country' => 'Cyprus']);

For more details, check the code, (*11)

What else?
Writing own mappers

You can write your own mapper for any storage you want. Just implement the MapperInterface, (*12)

MysqlPdo mapper connection settings
PdoMysqlMapper::setConnectionConfig(array(
    'user' => 'user',
    'password' => 'password',
    'host' => '127.0.0.1',
    'port' => '3306',
    'db'   => 'simpleorm_test'
));
Getters and setters

If you need setters and getters - you can define them explicitly, they will be used before the magic __get __set., (*13)

Mapper decorators

You can add decorators to mappers, for caching purposes, for example, (*14)

Model state

Models can be clean (unchanged, when fetched from the storage) and dirty (something is changed). You can always access the models cleanData and data, to see what changed., (*15)

Final word

As you see, it's super simple, yet effective. If you need an ORM, but dont want to mess with giants like Doctrine, i hope Simpleorm will server you for good., (*16)

The Versions

24/01 2017

dev-master

9999999-dev

Simple php orm.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Vasily Sokolov

orm php pdo query builder pdo orm mysql orm