Skip to content

4. Entity Relations

Bogdans edited this page May 20, 2018 · 2 revisions

Relations are implemented as simple position pointers, e.g. integer value which contains the position of record in the related table.

<?php
use DataManagement\Model\EntityRelationship\Table;
use DataManagement\Model\EntityRelationship\TableIterator;

$instructionFile = '/project-root/my-table-instruction.php';
$table = Table::newFromInstructionsFile($instructionFile);

$instructionFileRelation = '/project-root/my-table-instruction.php';
$tableRelation = Table::newFromInstructionsFile($instructionFileRelation);

# define record
$record = [
    'ID' => 5,
    'Profit' => 51.21,
    'ProductType' => 3,
    'ProductTypeReference' => 0
];
# find related record
$tableRelation->iterate(function($relation, TableIterator $iterator) use (&$record) {
    if ($relation['ID'] === $record['ProductType']) {
        $record['ProductTypeReference'] = $iterator->position() - 1;
        return Table::OPERATION_READ_STOP;
    }
});
# create
$table->create($record);

In the above example we link product type id with actual reference to relation table.

Now it's very easy (AND fast) to read the related record

<?php
use DataManagement\Model\EntityRelationship\Table;

$instructionFile = '/project-root/my-table-instruction.php';
$table = Table::newFromInstructionsFile($instructionFile);

$instructionFileRelation = '/project-root/my-table-instruction.php';
$tableRelation = Table::newFromInstructionsFile($instructionFileRelation);

# read the record
$record = $table->read(function($record) { 
    if ($record['ID'] === 5) { 
        return Table::OPERATION_READ_INCLUDE_AND_STOP; 
    } 
})[0];

# read the related record with 1 (!) operation
$iterator = $tableRelation->newIterator();
$iterator->jump($record['ProductTypeReference']);
$productTypeRecord = $iterator->read();
Clone this wiki locally