method findRelations documentation in bbn\Db\Languages\Sql
find_relations
function($column, string $db = '')
{
$changed = false;
if ($db && ($db !== $this->getCurrent())) {
$changed = $this->getCurrent();
$this->change($db);
}
$column = $this->colFullName($column);
$bits = explode('.', $column);
if (\count($bits) === 2) {
array_unshift($bits, $db ?: $this->current);
}
if (\count($bits) !== 3) {
return null;
}
$table = $bits[1];
if ($schema = $this->modelize()) {
$refs = [];
$test = function ($key) use ($bits) {
return ($key['ref_db'] === $bits[0]) && ($key['ref_table'] === $bits[1]) && ($key['ref_column'] === $bits[2]);
};
foreach ($schema as $tf => $cfg) {
$t = $this->tableSimpleName($tf);
if ($t !== $table) {
foreach ($cfg['keys'] as $k) {
if ($test($k)) {
foreach ($cfg['keys'] as $k2) {
// Is not the same table
if (!$test($k2)
// Has a reference
&& !empty($k2['ref_column'])
// and refers to a single column
&& (\count($k['columns']) === 1)
// A unique reference
&& (\count($k2['columns']) === 1)
// To a table with a primary
&& isset($schema[$this->tableFullName($k2['ref_table'])]['cols'][$k2['ref_column']])
// which is a sole column
&& (\count($schema[$this->tableFullName($k2['ref_table'])]['cols'][$k2['ref_column']]) === 1)
// We retrieve the key name
&& ($key_name = $schema[$this->tableFullName($k2['ref_table'])]['cols'][$k2['ref_column']][0])
// which is unique
&& !empty($schema[$this->tableFullName($k2['ref_table'])]['keys'][$key_name]['unique'])
) {
if (!isset($refs[$t])) {
$refs[$t] = ['column' => $k['columns'][0], 'refs' => []];
}
$refs[$t]['refs'][$k2['columns'][0]] = $k2['ref_table'].'.'.$k2['ref_column'];
}
}
}
}
}
}
if ($changed) {
$this->change($changed);
}
return $refs;
}
}
find_relations BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2023
BBN Solutions