aura/sqlmapper-bundle

This package is abandoned and no longer maintained. The author suggests using the atlas/orm package instead.

A SQL DataMapper implementation.

0.0.1 2015-04-01 14:29 UTC

This package is auto-updated.

Last update: 2022-02-01 12:43:23 UTC


README

DEPRECATED

This package is DEPRECATED and will not be released in a stable version. Please consider using Atlas instead.

Foreword

Installation

This bundle is installable and autoloadable via Composer as aura/sqlmapper-bundle.

Quality

Scrutinizer Code Quality Code Coverage Build Status

To run the unit tests at the command line, issue composer install and then phpunit at the package root. This requires Composer to be available as composer, and PHPUnit to be available as phpunit.

This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Community

To ask questions, provide feedback, or otherwise communicate with the Aura community, please join our Google Group, follow @auraphp on Twitter, or chat with us on #auraphp on Freenode.

Getting Started

Entity and Factory

<?php
use Aura\SqlMapper_Bundle\ObjectFactory;

class Post
{
    public $id;
    public $title;
    public $body;

    public function __construct(array $data = array())
    {
        foreach ($data as $field => $value) {
            $this->$field = $value;
        }
    }
}

class PostFactory extends ObjectFactory
{
    public function newObject(array $row = array())
    {
        return new Post($row);
    }
}
?>

Gateway

<?php
use Aura\SqlMapper_Bundle\AbstractGateway;

class PostGateway extends AbstractGateway
{
    public function getTable()
    {
        return 'posts';
    }

    public function getPrimaryCol()
    {
        return 'id';
    }
}
?>

Mapper

<?php
use Aura\SqlMapper_Bundle\AbstractMapper;

class PostMapper extends AbstractMapper
{
    public function getIdentityField()
    {
        return 'id';
    }

    public function getColsFields()
    {
        return [
            'id'    => 'id',
            'title' => 'title',
            'body'  => 'body',
        ];
    }
}
?>

Usage

<?php
use Aura\Sql\ConnectionLocator;
use Aura\Sql\ExtendedPdo;
use Aura\SqlMapper_Bundle\Query\ConnectedQueryFactory;
use Aura\SqlMapper_Bundle\Filter;
use Aura\SqlQuery\QueryFactory;
use Aura\Sql\Profiler;

$profiler = new Profiler();
$connection_locator = new ConnectionLocator(function () use ($profiler) {
    $pdo = new ExtendedPdo('sqlite::memory:');
    $pdo->setProfiler($profiler);
    return $pdo;
});

$query = new ConnectedQueryFactory(new QueryFactory('sqlite'));

$gateway_filter = new Filter();
$gateway = new PostGateway($connection_locator, $query, $gateway_filter);

$object_factory = new PostFactory();
$mapper_filter = new Filter();
$mapper = new PostMapper($gateway, $object_factory, $mapper_filter);
?>

Insert

<?php
$object = new Post(array(
    'id' => null,
    'title' => 'Hello aura',
    'body' => 'Some awesome content',
));

$mapper->insert($object);
?>

Note: the Mapper insert method assumes the primay column returned by getPrimaryCol is autogenerated by the database unless your concrete implementation of AbstractMapper overrides the isAutoPrimary method and returns a boolean false method. You will need to create to implement isAutoPrimary if you want to insert rows which contain values for the primary column.

fetchObject

<?php
$post = $mapper->fetchObject(
    $mapper->select()->where('id = ?', 1)
);
?>

fetchObjectBy

<?php
$post = $mapper->fetchObjectBy('id', 1);
?>

fetchCollection

<?php
$posts = $mapper->fetchCollection(
    $mapper->select()->where('id < ?', 11)
);
?>

fetchCollectionBy

<?php
$posts = $mapper->fetchCollectionBy('id', [1, 2, 3]);
?>

Update

<?php
$post = $mapper->fetchObjectBy('id', 1)
$post->title = 'Changed the title';
$mapper->update($post);
?>

Update only changes

<?php
$initial = $mapper->fetchObjectBy('id', 1)

$post = clone $initial;
$post->body = 'Changed the body';

$mapper->update($post, $initial);
?>

Delete

<?php
$post = $mapper->fetchObjectBy('id', 1);
$mapper->delete($post);
?>

Object and Collection Factory

By default the mapper returns standard class objects. You can change this behaviour when creating the mapper, by extending ObjectFactory or by implmenting ObjectFactoryInterface.

<?php
use Aura\SqlMapper_Bundle\ObjectFactoryInterface;
use Aura\SqlMapper_Bundle\Filter;

class PostFactory implements ObjectFactoryInterface
{
    public function newObject(array $row = array())
    {
        return new Post($row);
    }

    public function newCollection(array $rows = array())
    {
        $coll = array();
        foreach ($rows as $row) {
            $coll[] = $this->newObject($row);
        }
        return $coll;
    }
}

$object_factory new PostFactory();
$mapper_filter = new Filter();
$mapper = new PostMapper($gateway, $object_factory, $mapper_filter);
?>

Override identity field

By default, mapper assumes a public property as the identity field (or one that appears public via the magic __set() method). If the individual object uses a different property name, or uses a method instead, override setIdentityValue method to provide setter functionality.

Example :

<?php
namespace Vendor\Package;

use Aura\SqlMapper_Bundle\AbstractMapper;

class PostMapper extends AbstractMapper
{
    public function setIdentityValue($object, $value)
    {
        $object->setId($value);
    }
    // more code
}
?>

Filters

Filters can be used to alter the values during insert and update operations. Filter provides two methods which can be overridden, forInsert($subject) and forUpdate($subject). $subject may be passed as either an object or an array, so your code should be prepared to handle both.