method importTable documentation in bbn\Appui\Database
Imports a table's structure into the options table.
function(string $table, string $id_db, string $host = '')
{
if (empty($host)) {
$host_id = $this->retrieveHost($id_db);
}
else{
$host_id = bbn\Str::isUid($host) ? $host : $this->hostId($host);
}
if ($host_id && ($id_tables = $this->o->fromCode('tables', $id_db))) {
$engine = $this->o->parent($this->o->getIdParent($host_id));
if (!($id_table = $this->o->fromCode($table, $id_tables))
&& ($id_table = $this->o->add(
[
'id_parent' => $id_tables,
'text' => $table,
'code' => $table,
]
))
) {
$this->o->setCfg($id_table, ['allow_children' => 1]);
if ($id_columns = $this->o->add(
[
'id_parent' => $id_table,
'text' => X::_("Columns"),
'code' => 'columns'
]
)
) {
$this->o->setCfg(
$id_columns,
[
'show_code' => 1,
'show_value' => 1,
'sortable' => 1
]
);
}
if ($id_keys = $this->o->add(
[
'id_parent' => $id_table,
'text' => X::_("Keys"),
'code' => 'keys',
]
)
) {
$this->o->setCfg(
$id_keys,
[
'show_code' => 1,
'show_value' => 1,
'show_alias' => 1,
'allow_children' => 1
]
);
}
}
else{
$id_columns = $this->o->fromCode('columns', $id_table);
$id_keys = $this->o->fromCode('keys', $id_table);
}
$db = $this->o->code($id_db);
if ($id_table
&& $id_columns
&& $id_keys
&& $db
&& ($conn = $this->connection($host_id, $engine['code'], $db))
&& ($m = $conn->modelize($db.'.'.$table))
&& !empty($m['fields'])
) {
$num_cols = 0;
$num_cols_rem = 0;
$fields = [];
$ocols = array_flip($this->o->options($id_columns));
foreach ($m['fields'] as $col => $cfg) {
if ($opt_col = $this->o->option($col, $id_columns)) {
$num_cols += (int)$this->o->set($opt_col['id'], bbn\X::mergeArrays($opt_col, $cfg));
}
elseif ($id = $this->o->add(
bbn\X::mergeArrays(
$cfg,
[
'id_parent' => $id_columns,
'text' => $col,
'code' => $col,
'num' => $cfg['position']
]
)
)
) {
$num_cols++;
$opt_col = $cfg;
$opt_col['id'] = $id;
}
if ($opt_col) {
$fields[$col] = $opt_col['id'];
}
if (isset($ocols[$col])) {
unset($ocols[$col]);
}
}
if (!empty($ocols)) {
foreach ($ocols as $col => $id) {
if (bbn\Str::isUid($id)) {
$num_cols_rem += (int)$this->o->remove($id);
}
}
}
$num_keys = 0;
$num_keys_rem = 0;
$okeys = array_flip($this->o->options($id_keys));
foreach ($m['keys'] as $key => $cfg) {
$cols = $cfg['columns'] ?? [];
unset($cfg['columns']);
if (isset($cfg['ref_db'], $cfg['ref_table'], $cfg['ref_column'])
&& ($id_alias = $this->columnId($cfg['ref_column'], $cfg['ref_table'], $cfg['ref_db']))
) {
$cfg['id_alias'] = $id_alias;
unset($cfg['ref_db'], $cfg['ref_table'], $cfg['ref_column']);
}
if ($opt_key = $this->o->option($key, $id_keys)) {
$num_keys += (int)$this->o->set($opt_key['id'], bbn\X::mergeArrays($opt_key, $cfg));
}
elseif ($id = $this->o->add(
bbn\X::mergeArrays(
$cfg, [
'id_parent' => $id_keys,
'text' => $key,
'code' => $key
]
)
)
) {
$this->o->setCfg(
$id, [
'show_code' => 1,
'show_alias' => 1
]
);
$num_keys++;
$opt_key = $cfg;
$opt_key['id'] = $id;
}
if (isset($okeys[$key])) {
unset($okeys[$key]);
}
if ($opt_key && $cols) {
foreach ($cols as $col){
if (isset($fields[$col])) {
if ($opt = $this->o->option($col, $opt_key['id'])) {
$this->o->set(
$opt['id'], bbn\X::mergeArrays(
$opt, [
'id_alias' => $fields[$col]
]
)
);
}
else{
$tmp = [
'id_parent' => $opt_key['id'],
'id_alias' => $fields[$col],
'code' => $col,
'text' => $col
];
if ($this->o->add($tmp)) {
$opt = $tmp;
}
}
}
}
}
}
if (!empty($okeys)) {
foreach (array_values($okeys) as $id) {
if (bbn\Str::isUid($id)) {
$children = $this->o->items($id);
foreach ($children as $cid) {
$num_keys_rem += (int)$this->o->removeFull($cid);
}
$num_keys_rem += (int)$this->o->remove($id);
}
}
}
return [
'columns' => $num_cols,
'keys' => $num_keys,
'columns_removed' => $num_cols_rem,
'keys_removed' => $num_keys_rem
];
}
}
return null;
}
Imports a table's structure into the options table. 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