新增 business-mirror:follow-upstream-columns:先把数据字典刷新到最新,再把上游 视图里新增、当前数据集还没同步的字段追加进镜像定义并立刻建列,下一次常规同步 就会带回数据。只做新增,不删除/不改写已有字段;字段名带 password/secret/token/ cipher/credential 等敏感关键字的一律跳过。已登记到发布 cron(每天 06:40)。 页面侧:config/upstream_mirrors.php 的 auto_follow_columns 用「页面 => 数据集」 登记,FollowedUpstreamColumns 服务把 followed_columns 变成表格列;这类列只展示、 不生成查询条件(后端还没有对应筛选口径)。 数据集自带的数据变动仍然由每分钟的 business-mirror:sync-tables 增量同步。
50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
/**
|
||
* 自动跟随上游新增字段:读取数据集定义里记录的 followed_columns。
|
||
*
|
||
* 字段由 `business-mirror:follow-upstream-columns` 写入(见 config/upstream_mirrors.php
|
||
* 的 auto_follow_columns);页面据此自动补列,镜像行里带上同名键即可直接展示。
|
||
*/
|
||
class FollowedUpstreamColumns
|
||
{
|
||
public function __construct(private readonly BusinessMirrorDatasetRegistry $datasets)
|
||
{
|
||
}
|
||
|
||
/** @return list<string> */
|
||
public function columns(string $datasetKey): array
|
||
{
|
||
$columns = [];
|
||
foreach ((array) $this->datasets->definitionValue($datasetKey, 'followed_columns', []) as $column) {
|
||
$column = trim((string) $column);
|
||
if ($column !== '' && ! str_starts_with($column, '_mirror')) {
|
||
$columns[] = $column;
|
||
}
|
||
}
|
||
|
||
return array_values(array_unique($columns));
|
||
}
|
||
|
||
/**
|
||
* 给页面字段列表补上自动跟随进来的列(字段名与标签都用上游列名)。
|
||
*
|
||
* @param list<array<string,mixed>> $fields
|
||
* @return list<array<string,mixed>>
|
||
*/
|
||
public function appendFields(string $datasetKey, array $fields): array
|
||
{
|
||
$existing = array_map(static fn (array $field): string => (string) ($field['name'] ?? ''), $fields);
|
||
foreach ($this->columns($datasetKey) as $column) {
|
||
if (in_array($column, $existing, true)) {
|
||
continue;
|
||
}
|
||
$fields[] = ['name' => $column, 'label' => $column, 'followed' => true];
|
||
}
|
||
|
||
return $fields;
|
||
}
|
||
}
|