g4/data-repository

repository php library

2.0.1 2023-11-13 07:39 UTC

This package is auto-updated.

Last update: 2024-10-13 09:35:23 UTC


README

data-repository - repository library for php.

Install

Install through composer package manager. Find it on packagist.

composer require g4/data-repository

Dependencies:

Usage

Check data-mapper docs for details - data-mapper

Check russian-doll docs for details - russian-doll

Check identity-map docs for details - identity-map

use G4\DataMapper\Builder;
use G4\DataMapper\Common\Identity;
use G4\DataMapper\Common\MappingInterface;
use G4\DataMapper\Engine\MySQL\MySQLAdapter;
use G4\DataMapper\Engine\MySQL\MySQLClientFactory;
use G4\DataRepository\DataRepositoryFactory;
use G4\IdentityMap\IdentityMap;
use G4\Mcache\McacheFactory;
use G4\RussianDoll\Key;
use G4\RussianDoll\RussianDoll;

// Create instance

$dataRepository = (new DataRepositoryFactory(
            Builder::create()->adapter(new MySQLAdapter(new MySQLClientFactory([]))),
            new RussianDoll(McacheFactory::createInstance('__driver_name__', [], '__prefix__')),
            new IdentityMap()
        ))->create();


// Read flow with: data-mapper's mysql engine, russian-doll and identity-map 

$identity = new Identity();
$identity
    ->field('__field_name__')
    ->equal('__field_value__');

$response = $dataRepository
    ->setDatasetName('__table_name__')
    ->setIdentity($identity)
    ->setIdentityMapKey('__table_name__', '__field_name__', '__field_value__')
    ->setRussianDollKey(new Key('__table_name__', '__field_name__', '__field_value__'))
    ->select();


// Write flow (insert, update, upsert, delete) with: data-mapper's mysql engine, russian-doll, and identity-map 

$identity = new Identity();
$identity
    ->field('__field_name__')
    ->equal('__field_value__');

$this->repository
    ->setDatasetName('__table_name__')
    ->setMapping(new Mapp())) // must implement mapping
    ->insert();

$dataRepository
    ->setDatasetName('__table_name__')
    ->setIdentity($identity)
    ->setIdentityMapKey('__table_name__', '__field_name__', '__field_value__')
    ->setRussianDollKey(new Key('__table_name__', '__field_name__', '__field_value__'))
    ->setMapping(new Mapp())) // must implement mapping
    ->update();

$this->repository
    ->setDatasetName('__table_name__')
    ->setIdentity($identity)
    ->setIdentityMapKey('__table_name__', '__field_name__', '__field_value__')
    ->setRussianDollKey(new Key('__table_name__', '__field_name__', '__field_value__'))
    ->setMapping(new Mapp())) // must implement mapping
    ->upsert();

$this->repository
    ->setDatasetName('__table_name__')
    ->setIdentity($identity)
    ->setIdentityMapKey('__table_name__', '__field_name__', '__field_value__')
    ->setRussianDollKey(new Key('__table_name__', '__field_name__', '__field_value__'))
    ->delete();
   
 $this->repository
    ->setDatasetName('__table_name__')
    ->setIdentityMapKey('__table_name__')
    ->setRussianDollKey(new Key('__table_name__'))
    ->query('SELECT * FROM __table_name__');  
   
 $this->repository
    ->setDatasetName('__table_name__')
    ->setIdentityMapKey('__table_name__', '__field_name__', '__field_value__')
    ->setRussianDollKey(new Key('__table_name__', '__field_name__', '__field_value__'))
    ->command('DELETE FROM __table_name__ WHERE __field_name__ = __field_value__');
 

Development

Install dependencies

$ composer install

Run tests

$ composer unit-test

License

(The MIT License) see LICENSE file for details...