2   Repository

2.1   About the repository

The repository is the link between your entity object and your database, through the Query object.

It also bear the responsibility to intialize Metadata related to your entity, to learn how to map your database object.

2.2   Creating Metadata

As an example speaks louder than words:

public static function initMetadata(SerializerFactoryInterface $serializerFactory, array $options = [])
{
    $metadata = new Metadata($serializerFactory);

    $metadata->setEntity('sample\src\model\City');
    $metadata->setConnectionName('main');
    $metadata->setDatabase('world');
    $metadata->setTable('t_city_cit');

    $metadata->addField(array(
        'primary'       => true,
        'autoincrement' => true,
        'fieldName'     => 'id',
        'columnName'    => 'cit_id',
        'type'          => 'int'
    ));

    $metadata->addField(array(
        'fieldName'  => 'name',
        'columnName' => 'cit_name',
        'type'       => 'string'
    ));
Some details:
  • fieldName is the name of the property of your entity
  • columnName is the name of the column in your database
  • type is the type of your variable; among one of the following :
    • int
    • string
    • bool
    • datetime
    • json

The type bool, datetime and json are a bit special because they are dependent of database so they can be serialized / deserialized with some options:

addField([
    'fieldName'          => 'tags',
    'columnName'         => 'tags_name',
    'type'               => 'json',
    'serializer_options' => [
        'unserialize' => ['assoc' => true]
    ]
]);

Please take a look at the source code of each Serializer to see available options.

You can inject your own serializer:

addField([
    'fieldName'  => 'tags',
    'columnName' => 'tags_name',
    'type'       => 'json',
    'serializer' => '\Bouh\Awesome\Serializer'
]);
The bool type is considered a complex type, because the way they are stored in the database differs depending on each RDBMS
  • CCMBenchmark\Ting\Driver\Pgsql\Serializer\Bool
  • CCMBenchmark\Ting\Driver\Mysqli\Serializer\Bool

2.3   Queries

Le Repository fournit plusieurs méthodes afin de faciliter les requêtes simples.

2.3.1   Find an object by it’s primary key

$repository->get(['id' => 3])

2.3.2   All items

$repository->getAll()

2.3.3   An object according to one or more properties of the model

$repository->getOneBy(['propertyName' => 'Sylvain']);

2.3.4   An object collection, according to one or more properties of the model

$repository->getBy(['propertyName' => 'Sylvain']);