fix(import): null out orphan category references (FK safe)
This commit is contained in:
@@ -254,8 +254,14 @@ class SablogImport extends Command
|
|||||||
$status = (int) $row['visible'] === 1 ? 'published' : 'draft';
|
$status = (int) $row['visible'] === 1 ? 'published' : 'draft';
|
||||||
$dateline = ($ts = (int) $row['dateline']) > 0 ? date('Y-m-d H:i:s', $ts) : now()->toDateTimeString();
|
$dateline = ($ts = (int) $row['dateline']) > 0 ? date('Y-m-d H:i:s', $ts) : now()->toDateTimeString();
|
||||||
|
|
||||||
|
// 老库存在已删除分类的孤儿文章;目标列可空,置 null
|
||||||
|
$categoryId = (int) $row['cid'] ?: null;
|
||||||
|
if ($categoryId !== null && ! DB::table('categories')->where('id', $categoryId)->exists()) {
|
||||||
|
$categoryId = null;
|
||||||
|
}
|
||||||
|
|
||||||
DB::table('posts')->updateOrInsert(['id' => (int) $row['articleid']], [
|
DB::table('posts')->updateOrInsert(['id' => (int) $row['articleid']], [
|
||||||
'category_id' => (int) $row['cid'] ?: null,
|
'category_id' => $categoryId,
|
||||||
'user_id' => (int) $row['uid'] ?: null,
|
'user_id' => (int) $row['uid'] ?: null,
|
||||||
'title' => $row['title'],
|
'title' => $row['title'],
|
||||||
'slug' => $slug,
|
'slug' => $slug,
|
||||||
|
|||||||
Reference in New Issue
Block a user