Relationships
Minimal example
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\SymfonyForestAdmin\Service\ForestAgent;
$forestAgent->customizeCollection(
'Town',
function (CollectionCustomizer $builder) {
$builder
// Towns belong to 1 country
->addManyToOneRelation(
name: 'country',
foreignCollection: 'Country',
foreignKey: 'country_id'
)
// Towns have 1 mayor
->addOneToOneRelation(
name: 'myMayor',
foreignCollection: 'Mayor',
originKey: 'town_id'
)
// Towns have multiple inhabitants
->addOneToManyRelation(
name: 'inhabitants',
foreignCollection: 'Person',
originKey: 'town_id'
)
// Towns electricity is supplied by power-plants which are shared with other towns.
->addManyToManyRelation(
name: 'energyProviders',
foreignCollection: 'PowerPlant',
throughCollection: 'UtilityContract',
originKey: 'town_id',
foreignKey: 'power_plant_id'
);
->addExternalRelation(
'honoraryCitizen',
[
'schema' => ['firstName' => 'String', 'lastName' => 'String'],
'listRecords' => function ($record) {
$client = new \GuzzleHttp\Client();
$response = $client->get('https://api.example.com/user/' . $record['id']);
return $response->getBody();
}
]
)
}
);Last updated