2017 © Pedro Peláez
 

library yapm

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

image

victorium/yapm

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

  • Monday, September 5, 2016
  • by jjmutumi
  • Repository
  • 0 Watchers
  • 0 Stars
  • 3 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Yet Another Persistance Mapper (YAPM) for PHP

Introduction

YAPM is a very simple, stupid implementation framework agnostic ORM. It uses PDO database abstractions and currently supports MySQL, PostgreSQL, Sqlite3, Firebird and MS SQL databases. It uses the mysql, pgsql, sqlite, firebird, sqlsrv, mssql PDO drivers., (*1)

Licence

All source files in YAPM are released subject to the terms of the MIT license, as written in the included LICENSE.txt file., (*2)

Installation

Composer

YAPM can be installed with composer (http://getcomposer.org/)., (*3)

  1. Install composer:, (*4)

    $ curl -s https://getcomposer.org/installer | php
    
  2. Require YAPM as a dependency using composer:, (*5)

    $ php composer.phar require victorium/yapm
    

Getting Started

This is a simple example on getting started with YAPM:, (*6)

<?php

define("PATH_TO_YAPM_SRC", dirname(__DIR__));

require PATH_TO_YAPM_SRC . "/bootstrap.php";

use Yapm\Adapter\Sqlite3;
use Yapm\Mapper;

// lowercase class name corresponds to table name
// class public properties correspond to columns of table

class Author {
    public $title;
    public $name;
}

class Contribution {
    public $author_id;
    public $post_id;
}

class Post {
    public $title;
    public $slug;
    public $author_id;
    public $content;

    public function __construct($data = []) {
        foreach ($data as $k => $v) {
            $this->{$k} = $v;
        }
    }
}

function getConfig() {
    return [
        "db" => [
            "adapter" => "sqlite",
            "name" => ":memory:",
            "options" => [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION],
        ]
    ];
}

$mapper = new Mapper(getConfig());

Features

These code snippets demonstrate basic CRUD functionality. Queries can be flexibly built using builder defined in YAQB where necessary., (*7)

Querying

<?php

// fetching multiple objects
$posts = $mapper->all(Post::class)->fetch();
$posts = $mapper->all(Post::class)->where("author_id", 1)->fetch();

// fetching one object
$post = $mapper->get(Post::class, 1)->fetch();
$post = $mapper->get(Post::class)->where("author_id", 1)->where("slug", "the-funny")->fetch();

Inserting

<?php

// inserting one object
$post = new Post;
$post->title = "The start";
$post->slug = "the-start";
$post->content = "All the things start with the beginning";
$post->author_id = 9;
$mapper->save($post);

// inserting multiple objects
$posts = [
    new Post(["title" => "The Start", "author_id" => 9,
        "slug" => "the-start", "content" => "All the things start with the beginning"
    ]),
    new Post(["title" => "The End", "author_id" => 9,
        "slug" => "the-end", "content" => "All the things end with the ending"
    ]),
];
$statuses = [];
foreach ($posts as $post) {
    $statuses[] = $mapper->save($post);
}

Updating

<?php

// updating one object
$post = $mapper->get(Post::class, 1)->fetch();
$post->slug = "new-slug";
$mapper->save($post);

// updating multiple objects with condition (bulk update)
$mapper->update(Post::class)->values(["author_id" => 404])->where("slug", "the-start")->run();

Deleting

<?php

// deleting one object
$post = $mapper->get(Post::class, 1)->fetch();
$mapper->delete($post);

// alternatives that don't require first fetching the object
$mapper->delete(Post::class, 1)->run();
$mapper->delete(Post::class)->where("id", 1)->run();

// deleting multiple objects
$posts = $mapper->all(Post::class)->fetch();
$statuses = [];
foreach ($posts as $post) {
    $statuses[] = $mapper->delete($post);
}

Querying with relationships

<?php

// getting posts which each have on author but can have mulpitle contributors. Contributors are a link to an author.
$posts = $mapper->all(Post::class)->fetch();
$mapper
    ->mapToOne(
        $posts,
        "author_id", "author", "id",
        $mapper->all(Author::class)->where("id", $mapper->getKeyValues($posts, "author_id"))->fetch()
    )->mapToMany(
        $posts,
        "id", "contributors", "post_id",
        $mapper->all(Contribution::class)->where("post_id", $mapper->getKeyValues($posts, "id"))->fetch()
    )->mapToOne(
        $posts,
        "contributors.author_id", "author", "id",
        $mapper->all(Author::class)->where("id", $mapper->getKeyValues($posts, "contributors.author_id"))->fetch()
    );

Transactions

<?php

$post = null;

try {
    $mapper->begin();
    $mapper->delete($post);
    $mapper->commit();
} catch (\Exception $err) {
    $mapper->rollback();
}

The Versions

05/09 2016

2.0.2

2.0.2.0

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

  Sources   Download

MIT

The Requires

 

The Development Requires

mapper another persistance yet

01/09 2016

2.0.1

2.0.1.0

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

  Sources   Download

MIT

The Requires

 

The Development Requires

mapper another persistance yet

01/09 2016

2.0.0

2.0.0.0

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

  Sources   Download

MIT

The Requires

 

The Development Requires

mapper another persistance yet

11/08 2016

dev-master

9999999-dev

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

  Sources   Download

MIT

The Requires

 

The Development Requires

mapper another persistance yet

11/08 2016

2.0.0-alpha

2.0.0.0-alpha

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

  Sources   Download

MIT

The Requires

 

The Development Requires

mapper another persistance yet

08/08 2016

1.0.1

1.0.1.0

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

  Sources   Download

MIT

The Requires

 

The Development Requires

mapper another persistance yet

14/03 2016

1.0.0

1.0.0.0

Yet Another Persistance Mapper for PHP. Simple, Stupid and totally framework independent.

  Sources   Download

MIT

The Requires

 

The Development Requires

mapper another persistance yet