PHP code example of g4 / data-repository

1. Go to this page and download the library: Download g4/data-repository 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/ */

    

g4 / data-repository example snippets



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