This is very much a work in progress. Use at your own risk.
This bundle is installable and autoloadable via Composer as aura/sqlmapper-bundle.
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.
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.
<?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);
}
}
?><?php
use Aura\SqlMapper_Bundle\AbstractGateway;
class PostGateway extends AbstractGateway
{
public function getTable()
{
return 'posts';
}
public function getPrimaryCol()
{
return 'id';
}
}
?><?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',
];
}
}
?><?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);
?><?php
$object = new Post(array(
'id' => null,
'title' => 'Hello aura',
'body' => 'Some awesome content',
));
$mapper->insert($object);
?><?php
$post = $mapper->fetchObject(
$mapper->select()->where('id = ?', 1)
);
?><?php
$post = $mapper->fetchObjectBy('id', 1);
?><?php
$posts = $mapper->fetchCollection(
$mapper->select()->where('id < ?', 11)
);
?><?php
$posts = $mapper->fetchCollectionBy('id', [1, 2, 3]);
?><?php
$post = $mapper->fetchObjectBy('id', 1)
$post->title = 'Changed the title';
$mapper->update($post);
?><?php
$initial = $mapper->fetchObjectBy('id', 1)
$post = clone $initial;
$post->body = 'Changed the body';
$mapper->update($post, $initial);
?><?php
$post = $mapper->fetchObjectBy('id', 1);
$mapper->delete($post);
?>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);
?>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
}
?>
