5   Repository

5.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.

5.2   Creating Metadata

As an example speaks louder than words:

use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Serializer\SerializerFactoryInterface;
use CCMBenchmark\Ting\Repository\MetadataInitializer;

class SampleRepository extends Repository implements MetadataInitializer
{
    public static function initMetadata(SerializerFactoryInterface $serializerFactory, array $options = [])
    {
        $metadata = new Metadata($serializerFactory);

        $metadata->setEntity(Sample\Model\City::class);
        $metadata->setConnectionName('main');
        $metadata->setDatabase('world');
        $metadata->setTable('t_city_cit');

        $metadata
            ->addField([
                'primary'       => true,
                'autoincrement' => true,
                'fieldName'     => 'id',
                'columnName'    => 'cit_id',
                'type'          => 'int'
            ])
            ->addField([
                'fieldName'  => 'name',
                'columnName' => 'cit_name',
                'type'       => 'string'
            ]);

        return $metadata
    }
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
    • double
    • 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::class
]);
The boolean 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\Boolean
  • CCMBenchmark\Ting\Driver\Mysqli\Serializer\Boolean

5.3   QueryBuilder

We can need chaining queries, a QueryBuilder can be used. Ting provide it through aura/sqlquery.

$repository->getQueryBuilder(Repository::QUERY_SELECT);

or

$repository->getQueryBuilder(Repository::QUERY_INSERT);

or

$repository->getQueryBuilder(Repository::QUERY_UPDATE);

or

$repository->getQueryBuilder(Repository::QUERY_DELETE);

Documentation available on https://github.com/auraphp/Aura.SqlQuery

5.4   Queries

The Repository provides several methods to perform common queries easily.

5.4.1   Find an object by it’s primary key

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

5.4.2   All items

$repository->getAll()

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

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

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

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