fix(import): create placeholder category for orphan articles
Build and Deploy / verify (push) Successful in 54s
Build and Deploy / deploy (push) Successful in 28s

This commit is contained in:
2026-09-07 19:46:24 +00:00
parent 7a2b07ca4d
commit 0116fc4116
@@ -213,6 +213,8 @@ class SablogImportCommand extends Command
} }
} }
$this->ensureCategoryExists((int) $row->cid);
$this->upsertWithId(Article::class, (int) $row->articleid, [ $this->upsertWithId(Article::class, (int) $row->articleid, [
'category_id' => (int) $row->cid, 'category_id' => (int) $row->cid,
'user_id' => (int) $row->uid, 'user_id' => (int) $row->uid,
@@ -424,6 +426,32 @@ class SablogImportCommand extends Command
return $model; return $model;
} }
/** @var array<int, bool> */
protected array $ensuredCategories = [];
/**
* 老库里可能存在已删除分类的孤儿文章,为其补一个占位分类以满足外键约束。
*/
protected function ensureCategoryExists(int $cid): void
{
if ($cid <= 0 || isset($this->ensuredCategories[$cid])) {
return;
}
$this->ensuredCategories[$cid] = true;
if (Category::query()->whereKey($cid)->exists()) {
return;
}
$this->upsertWithId(Category::class, $cid, [
'name' => "已删除分类 {$cid}",
'display_order' => 9999,
'articles_count' => 0,
]);
$this->warn("Category #{$cid} missing in source; placeholder created for orphan articles.");
}
protected function countTable($source, string $table, string $key): void protected function countTable($source, string $table, string $key): void
{ {
if ($this->sourceTableExists($source, $table)) { if ($this->sourceTableExists($source, $table)) {