analogueorm / analogue

Analogue ORM : Data Mapper ORM for Laravel/PHP
MIT License
634 stars 51 forks source link
data-mapper database laravel orm php

(this project is looking for a new maintainer)

Analogue ORM

Latest Stable Version Latest Unstable Version License Build Status StyleCI

Analogue is a flexible, easy-to-use ORM for PHP. It is a transposition of the Eloquent ORM that ships with Laravel framework using a Data Mapper pattern instead of the original Active Record approach. it overcomes some of Eloquent's architectural limitations by using a strict separation of concerns; for example, you can use Value Objects or Single-table-inheritance, which are hard/impossible to implement correctly using the native ORM.

As a Laravel package, it integrates flawlessly inside the framework, and provides a more powerfull peristance layer, allowing to build enterprise-grade applications while retaining a simple and enjoyable development experience.

Installation

composer require analogue/orm

See Configuration for more information.

Concept

The concept is simple; your model layer is defined using 2 classes : one Entity, which can be any PHP class or extends the base Analogue\ORM\Entity class which provides magic getters and setters, and one EntityMap which defines relationships, castings, table name, database column names.

Take this simple domain model :

use Analogue\ORM\Entity;
use Illuminate\Support\Collection;

class Blog extends Entity
{
    public function __construct()
    {
        $this->posts = new Collection;
    }

    public function addPost(Post $post)
    {
        $this->posts->push($post);
    }
}

class Post extends Entity
{

}

We can instruct Analogue how these objects are related using these classes :

use Analogue\ORM\EntityMap;

class BlogMap extends EntityMap
{
    public function posts(Blog $blog)
    {
        return $this->hasMany($blog, Post::class);
    }
}

class PostMap extends EntityMap
{
    public function blog(Post $post)
    {
        return $this->belongsTo($post, Blog::class);
    }
}

Now we can create related instance of or object and persist them to the database :

$blog = new Blog;
$blog->title = "My first blog";

$post = new Post; 
$post->title->"My first post";

$blog->addPost($post);

// Only the blog instance need to explicitely stored; Analogue takes care of synchronizing
// related objects behinds the scene. 

mapper(Blog::class)->store($blog);

Once our objects are persisted into the database, we can query them using the fluent query builder :

$blog = mapper(Blog::class)->first();

echo $blog->posts->first()->title; // 'My first post'

Documentation

Check the Documentation for more details.

Features

Changelog

Version 5.6

Version 5.5

Version 5.4

Version 5.3

Version 5.1

Version 5.0

Version 2.1.3

Version 2.1

Version 2.0

Documentation

Check the wiki for full documentation.

Licence

This package is licensed under the MIT License.