2017 © Pedro Peláez
 

library atlas-foundation

Simple Data Mapper Library

image

fivesqrd/atlas-foundation

Simple Data Mapper Library

  • Monday, May 14, 2018
  • by christianjburger
  • Repository
  • 1 Watchers
  • 2 Stars
  • 41 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Atlas Data Mapper

Atlas is an open source data mapper implementation for PHP., (*1)

Atlas creates barebones models for your project with minimal effort, allowing you to start working with them quickly. Extending or customising functionality is possible, but can wait until it is required., (*2)

The framework offers the following features: - Minimal scaffolding required to create new models - Easily expose business logic query layer - Reduced application wide ripples from schema changes - Automatic read/write routing - Protection against SQL injection attacks - RDBMS abstraction, (*3)

Use Cases

Persisting a new user:, (*4)

$user = new Model\User\Entity();
$user->set('_email', 'user@domain.com');
$user->set('_enabled', true);

/* Save to db */
$id = $this->model(Model\User::class)->save($user);

Fetching an instance of the user entity by primary key:, (*5)

$user = $this->model(Model\User::class)->fetch($id);

Access properties using default getters:, (*6)

$timestamp = $user->get('_lastLogin');

Persisting changes to the user model using default setters:, (*7)

/* Fetch from db */
$user = $this->model(Model\User::class)->fetch($id);

/* Update entity */
$user->set('_lastLogin', time());

/* Save to db */
$this->model(Model\User::class)->save($user);

Querying user model business layer:, (*8)

$users = $this->model(Model\User::class)->query()
    ->isEnabled(true)
    ->hasLoggedSince(strtotime('5 days ago'))
    ->fetch()->all();

foreach ($users as $user) {
    echo $user->get('_email');
}

Optimised queries for simple operations like counts:, (*9)

$count = $this->model(Model\User::class)->named()
    ->withRecentLogIn()
    -fetch()->count();

Extending Model Classes

Access properties using custom getters:, (*10)

$date = $user->getLastLogin('Y-m-d');

Persisting changes using custom setters:, (*11)

/* Fetch from db */
$user = $this->model(Model\User::class)->fetch($id);

/* Update entity */
$user->setEmailAddress('user@domain.com');
$user->setEnabled(true);

/* Save to db */
$this->model(Model\User::class)->save($user);

Using named queries for consistent results:, (*12)

$users = $this->model(Model\User::class)->named()
    ->withRecentLogIn()
    -fetch()->all();

Adding to named queries on the fly:, (*13)

$users = $this->model(Model\User::class)->named()
    ->withRecentLogIn()
    ->isEnabled(true)
    -fetch()->all();

Performing operations on collections:, (*14)

$users = $this->model(Model\User::class)->named()
    ->withRecentLogIn()
    -fetch()->all();

$emails = $users->getAllEmailAddresses();

Implementation

Each model consists of a set of classes. Each class extends a super class, to allow new models to be created with minimal effort., (*15)

Below is an example what a project with 3 models might look like, (*16)

|- Model
   |-- User.php
   |-- User
       |-- Entity.php
       |-- Mapper.php
       |-- Collection.php
       |-- Query.php
       |-- Named.php
       |-- Relation.php
   |-- Customer.php
   |-- Customer
       ...
   |-- Content.php
   |-- Contact
       ...

Sample mapper classs, (*17)

<?php
namespace Application\Model\User;

class Mapper extends \Atlas\Model\Mapper
{
    protected $_alias = 'u';

    protected $_table = 'users';

    protected $_key = 'id';

    protected $_map = array(
        '_id'        => 'id',
        '_email'     => 'email',
        '_password   => 'password',
        '_lastLogin' => 'last_login'
    );

    protected $_readOnly = array('id');
}

Sample entity class, (*18)

<?php
namespace Application\Model\User;

class Entity extends \Atlas\Model\Entity
{
    protected $_id;

    protected $_email;

    protected $_password;

    protected $_lastLogin;
}

Using Canvas

The atlas repo ships with a script to quickly create boilerplate classes when a new model needs to be added. See https://github.com/fivesqrd/atlas-canvas, (*19)

Install and Setup

Install

Via composer, (*20)

cd /myproject
php composer.phar require fivesqrd/atlas:3.0 

Config

Add the following config to your project:, (*21)

$config = array(
    'read' => array(
        'dsn'      => 'mysql:dbname=testdb;host=127.0.0.1',
        'username' => 'username',
        'password' => 'password',
    ),
    'write' => array(
        'dsn'      => 'mysql:dbname=testdb;host=127.0.0.1',
        'username' => 'username',
        'password' => 'password',
    ),
);

Bootstrap from MVC

Atlas can be bootstrapped from within your MVC framework by passing the Proxy class to your controllers/views via a plugin or helper:, (*22)

class MyControllerPlugin
{
    public function model($class) {
        return new Atlas\Proxy(
            new Atlas\Database\Factory($this->_config),
            new Atlas\Database\Resolver($class)
        );
    }
}

A Laravel 5 specific package is available here: https://github.com/fivesqrd/atlas-laravel, (*23)

The Versions

14/05 2018

dev-master

9999999-dev

Simple Data Mapper Library

  Sources   Download

BSD License

by Christian Burger

14/05 2018

v3.2.3

3.2.3.0

Simple Data Mapper Library

  Sources   Download

by Christian Burger

14/05 2018

3.2.x-dev

3.2.9999999.9999999-dev

Simple Data Mapper Library

  Sources   Download

BSD License

by Christian Burger

17/04 2018

v3.2.2

3.2.2.0

Simple Data Mapper Library

  Sources   Download

by Christian Burger

17/04 2018

v3.2.1

3.2.1.0

Simple Data Mapper Library

  Sources   Download

BSD License

by Christian Burger

09/11 2017

v3.2.0

3.2.0.0

Simple Data Mapper Library

  Sources   Download

BSD License

by Christian Burger

10/10 2017

3.1.x-dev

3.1.9999999.9999999-dev

Simple Data Mapper Library

  Sources   Download

BSD License

by Christian Burger

08/03 2017

3.0.x-dev

3.0.9999999.9999999-dev

Simple Data Mapper Library

  Sources   Download

BSD License

by Christian Burger

03/02 2017

2.0.x-dev

2.0.9999999.9999999-dev

Simple Data Mapper Library

  Sources   Download

Five Squared License

The Requires

 

by Christian Burger

30/01 2017

1.0.x-dev

1.0.9999999.9999999-dev

Simple Data Mapper Library

  Sources   Download

Five License

The Requires

 

by Christian Burger

24/01 2017

dev-backlog

dev-backlog

Simple Data Mapper Library

  Sources   Download

Five License

by Christian Burger