fix(item-master): EntityRelationship::link() 파라미터 순서 및 관계 로드 오류 수정

- EntityRelationship::link() 호출 파라미터 순서 수정 (3개 파일)
  - ItemSectionService: tenantId를 첫 번째 파라미터로 변경
  - ItemBomItemService: tenantId를 첫 번째 파라미터로 변경
  - ItemFieldService: tenantId를 첫 번째 파라미터로 변경

- ItemSection 모델의 fields()/bomItems() 관계 메서드 문제 해결
  - 쿼리빌더 반환으로 인한 addEagerConstraints() 에러 수정
  - loadRelatedEntities() 메서드 신규 추가
  - with()/load() 대신 setRelation()으로 데이터 설정

- 영향받은 서비스 파일 전체 수정
  - ItemSectionService, SectionTemplateService, ItemMasterService
This commit is contained in:
2025-11-27 16:49:36 +09:00
parent 29dd554bbb
commit c5eee5561f
6 changed files with 46 additions and 35 deletions

View File

@@ -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;
}
/**

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 기반)

View File

@@ -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;
}

View File

@@ -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();
}