fix(import): create placeholder category for orphan articles
This commit is contained in:
@@ -213,6 +213,8 @@ class SablogImportCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
$this->ensureCategoryExists((int) $row->cid);
|
||||
|
||||
$this->upsertWithId(Article::class, (int) $row->articleid, [
|
||||
'category_id' => (int) $row->cid,
|
||||
'user_id' => (int) $row->uid,
|
||||
@@ -424,6 +426,32 @@ class SablogImportCommand extends Command
|
||||
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
|
||||
{
|
||||
if ($this->sourceTableExists($source, $table)) {
|
||||
|
||||
Reference in New Issue
Block a user