Aura.SqlMapper_Bundle
DEPRECATED
This package is DEPRECATED and will not be released in a stable version. Please consider using Atlas instead., (*1)
Foreword
Installation
This bundle is installable and autoloadable via Composer as aura/sqlmapper-bundle., (*2)
Quality
, (*3)
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
., (*4)
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., (*5)
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., (*6)
Getting Started
Entity and Factory
$value) {
$this->$field = $value;
}
}
}
class PostFactory extends ObjectFactory
{
public function newObject(array $row = array())
{
return new Post($row);
}
}
?>
Gateway
Mapper
'id',
'title' => 'title',
'body' => 'body',
];
}
}
?>
Usage
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
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., (*7)
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
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., (*8)
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., (*9)
Example :, (*10)
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., (*11)