diff --git a/app/Models/ItemMaster/ItemSection.php b/app/Models/ItemMaster/ItemSection.php index 09b7984..af9f260 100644 --- a/app/Models/ItemMaster/ItemSection.php +++ b/app/Models/ItemMaster/ItemSection.php @@ -57,19 +57,18 @@ public function scopeNonTemplates($query) ]; /** - * 섹션에 연결된 필드 목록 (entity_relationships 기반) + * 관련 엔티티(fields, bomItems) 로드 + * + * entity_relationships 기반이므로 일반 Eloquent load() 대신 사용 + * + * @return $this */ - public function fields() + public function loadRelatedEntities(): self { - return $this->linkedFields(); - } + $this->setRelation('fields', $this->linkedFields()->orderBy('order_no')->get()); + $this->setRelation('bomItems', $this->linkedBomItems()->orderBy('id')->get()); - /** - * 섹션에 연결된 BOM 항목 목록 (entity_relationships 기반) - */ - public function bomItems() - { - return $this->linkedBomItems(); + return $this; } /** diff --git a/app/Services/ItemMaster/ItemBomItemService.php b/app/Services/ItemMaster/ItemBomItemService.php index 18e4602..0f36e70 100644 --- a/app/Services/ItemMaster/ItemBomItemService.php +++ b/app/Services/ItemMaster/ItemBomItemService.php @@ -89,13 +89,14 @@ public function store(int $sectionId, array $data): ItemBomItem // 섹션-BOM 관계 생성 EntityRelationship::link( + $tenantId, EntityRelationship::TYPE_SECTION, $sectionId, EntityRelationship::TYPE_BOM, $bomItem->id, - $tenantId, - $data['group_id'] ?? 1, - 0 + 0, + null, + $data['group_id'] ?? 1 ); return $bomItem; diff --git a/app/Services/ItemMaster/ItemFieldService.php b/app/Services/ItemMaster/ItemFieldService.php index 318c4ec..b618b94 100644 --- a/app/Services/ItemMaster/ItemFieldService.php +++ b/app/Services/ItemMaster/ItemFieldService.php @@ -161,13 +161,14 @@ public function store(int $sectionId, array $data): ItemField // 섹션-필드 관계 생성 EntityRelationship::link( + $tenantId, EntityRelationship::TYPE_SECTION, $sectionId, EntityRelationship::TYPE_FIELD, $field->id, - $tenantId, - $data['group_id'] ?? 1, - ($maxOrder ?? -1) + 1 + ($maxOrder ?? -1) + 1, + null, + $data['group_id'] ?? 1 ); return $field; diff --git a/app/Services/ItemMaster/ItemMasterService.php b/app/Services/ItemMaster/ItemMasterService.php index 67f9b5b..b70394f 100644 --- a/app/Services/ItemMaster/ItemMasterService.php +++ b/app/Services/ItemMaster/ItemMasterService.php @@ -52,10 +52,12 @@ public function init(): array // 3. 모든 독립 섹션 (재사용 가능 목록) $sections = ItemSection::where('tenant_id', $tenantId) - ->with(['fields', 'bomItems']) ->orderBy('created_at', 'desc') ->get(); + // entity_relationships 기반이므로 각 섹션별로 관련 엔티티 로드 + $sections->each(fn ($section) => $section->loadRelatedEntities()); + // 4. 커스텀 탭 (컬럼 설정 포함) $customTabs = CustomTab::with('columnSetting') ->where('tenant_id', $tenantId) @@ -88,8 +90,7 @@ private function getLinkedSections(int $tenantId, int $pageId): array $sections = []; foreach ($relationships as $rel) { - $section = ItemSection::with(['fields', 'bomItems']) - ->find($rel->child_id); + $section = ItemSection::find($rel->child_id); if ($section) { // 섹션에 연결된 필드 (entity_relationships 기반) diff --git a/app/Services/ItemMaster/ItemSectionService.php b/app/Services/ItemMaster/ItemSectionService.php index e0700cd..4283a3d 100644 --- a/app/Services/ItemMaster/ItemSectionService.php +++ b/app/Services/ItemMaster/ItemSectionService.php @@ -57,7 +57,7 @@ public function storeIndependent(array $data): ItemSection 'created_by' => $userId, ]); - return $section->load(['fields', 'bomItems']); + return $section->loadRelatedEntities(); } /** @@ -72,13 +72,15 @@ public function clone(int $id): ItemSection $original = ItemSection::where('tenant_id', $tenantId) ->where('id', $id) - ->with(['fields', 'bomItems']) ->first(); if (! $original) { throw new NotFoundHttpException(__('error.section_not_found')); } + // 원본 섹션의 관련 엔티티 로드 (entity_relationships 기반) + $original->loadRelatedEntities(); + // 섹션 복제 $cloned = ItemSection::create([ 'tenant_id' => $tenantId, @@ -93,7 +95,7 @@ public function clone(int $id): ItemSection ]); // 필드 복제 (entity_relationships 기반) - foreach ($original->fields as $orderNo => $field) { + foreach ($original->getRelation('fields') as $orderNo => $field) { $clonedField = ItemField::create([ 'tenant_id' => $tenantId, 'group_id' => $field->group_id, @@ -124,7 +126,7 @@ public function clone(int $id): ItemSection } // BOM 항목 복제 (entity_relationships 기반) - foreach ($original->bomItems as $orderNo => $bom) { + foreach ($original->getRelation('bomItems') as $orderNo => $bom) { $clonedBom = ItemBomItem::create([ 'tenant_id' => $tenantId, 'group_id' => $bom->group_id, @@ -152,7 +154,7 @@ public function clone(int $id): ItemSection ); } - return $cloned->load(['fields', 'bomItems']); + return $cloned->loadRelatedEntities(); } /** @@ -208,16 +210,17 @@ public function store(int $pageId, array $data): ItemSection // 페이지-섹션 관계 생성 EntityRelationship::link( + $tenantId, EntityRelationship::TYPE_PAGE, $pageId, EntityRelationship::TYPE_SECTION, $section->id, - $tenantId, - $data['group_id'] ?? 1, - ($maxOrder ?? -1) + 1 + ($maxOrder ?? -1) + 1, + null, + $data['group_id'] ?? 1 ); - $section->load(['fields', 'bomItems']); + $section->loadRelatedEntities(); return $section; } @@ -243,7 +246,7 @@ public function update(int $id, array $data): ItemSection 'updated_by' => $userId, ]); - $section->load(['fields', 'bomItems']); + $section->loadRelatedEntities(); return $section; } diff --git a/app/Services/ItemMaster/SectionTemplateService.php b/app/Services/ItemMaster/SectionTemplateService.php index 585adf0..fdc0b50 100644 --- a/app/Services/ItemMaster/SectionTemplateService.php +++ b/app/Services/ItemMaster/SectionTemplateService.php @@ -25,10 +25,14 @@ public function index(): Collection { $tenantId = $this->tenantId(); - return ItemSection::where('tenant_id', $tenantId) - ->with(['fields', 'bomItems']) + $sections = ItemSection::where('tenant_id', $tenantId) ->orderBy('created_at', 'desc') ->get(); + + // entity_relationships 기반이므로 각 섹션별로 관련 엔티티 로드 + $sections->each(fn ($section) => $section->loadRelatedEntities()); + + return $sections; } /** @@ -81,7 +85,7 @@ public function store(array $data): ItemSection ); } - return $section->load(['fields', 'bomItems']); + return $section->loadRelatedEntities(); } /** @@ -108,7 +112,7 @@ public function update(int $id, array $data): ItemSection 'updated_by' => $userId, ]); - return $section->fresh()->load(['fields', 'bomItems']); + return $section->fresh()->loadRelatedEntities(); } /** @@ -144,12 +148,14 @@ public function destroy(int $id): void $section->delete(); // 4. 하위 필드/BOM도 Soft Delete - foreach ($section->fields as $field) { + $section->loadRelatedEntities(); + + foreach ($section->getRelation('fields') as $field) { $field->update(['deleted_by' => $userId]); $field->delete(); } - foreach ($section->bomItems as $bomItem) { + foreach ($section->getRelation('bomItems') as $bomItem) { $bomItem->update(['deleted_by' => $userId]); $bomItem->delete(); }