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)
-
Install composer:, (*4)
$ curl -s https://getcomposer.org/installer | php
-
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();
}