PHP code example of aura / sqlmapper-bundle

1. Go to this page and download the library: Download aura/sqlmapper-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aura / sqlmapper-bundle example snippets



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);
    }
}


use Aura\SqlMapper_Bundle\AbstractGateway;

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

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


use Aura\SqlMapper_Bundle\AbstractMapper;

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

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


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);


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

$mapper->insert($object);


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


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


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


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


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


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

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

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


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


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);


namespace Vendor\Package;

use Aura\SqlMapper_Bundle\AbstractMapper;

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