Files
sam-api/app/Services/ItemMaster/SectionTemplateService.php
hskwon c5eee5561f 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
2025-11-27 16:49:36 +09:00

164 lines
5.0 KiB
PHP

<?php
namespace App\Services\ItemMaster;
use App\Models\ItemMaster\EntityRelationship;
use App\Models\ItemMaster\ItemPage;
use App\Models\ItemMaster\ItemSection;
use App\Services\Service;
use Illuminate\Database\Eloquent\Collection;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* SectionTemplateService
*
* 섹션 관리 서비스 (참조 방식)
* - 섹션은 독립 엔티티로 생성
* - 페이지와는 entity_relationships로 링크 연결
*/
class SectionTemplateService extends Service
{
/**
* 섹션 목록 (독립 섹션 전체)
*/
public function index(): Collection
{
$tenantId = $this->tenantId();
$sections = ItemSection::where('tenant_id', $tenantId)
->orderBy('created_at', 'desc')
->get();
// entity_relationships 기반이므로 각 섹션별로 관련 엔티티 로드
$sections->each(fn ($section) => $section->loadRelatedEntities());
return $sections;
}
/**
* 독립 섹션 생성 (page_id가 있으면 링크도 연결)
*
* @param array $data ['page_id'(optional), 'title', 'type', 'description', 'is_default']
*/
public function store(array $data): ItemSection
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$pageId = $data['page_id'] ?? null;
// page_id가 있으면 페이지 존재 확인
if ($pageId) {
$page = ItemPage::where('tenant_id', $tenantId)->find($pageId);
if (! $page) {
throw new NotFoundHttpException(__('error.page_not_found'));
}
}
// 1. 독립 섹션 생성
$section = ItemSection::create([
'tenant_id' => $tenantId,
'group_id' => 1,
'title' => $data['title'],
'type' => $data['type'],
'order_no' => 0,
'is_template' => false,
'is_default' => $data['is_default'] ?? false,
'description' => $data['description'] ?? null,
'created_by' => $userId,
]);
// 2. page_id가 있으면 링크 연결
if ($pageId) {
$maxOrderNo = EntityRelationship::where('tenant_id', $tenantId)
->where('parent_type', EntityRelationship::TYPE_PAGE)
->where('parent_id', $pageId)
->where('child_type', EntityRelationship::TYPE_SECTION)
->max('order_no') ?? -1;
EntityRelationship::link(
$tenantId,
EntityRelationship::TYPE_PAGE,
$pageId,
EntityRelationship::TYPE_SECTION,
$section->id,
$maxOrderNo + 1
);
}
return $section->loadRelatedEntities();
}
/**
* 섹션 수정
*/
public function update(int $id, array $data): ItemSection
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$section = ItemSection::where('tenant_id', $tenantId)
->where('id', $id)
->first();
if (! $section) {
throw new NotFoundHttpException(__('error.section_not_found'));
}
$section->update([
'title' => $data['title'] ?? $section->title,
'type' => $data['type'] ?? $section->type,
'description' => $data['description'] ?? $section->description,
'is_default' => $data['is_default'] ?? $section->is_default,
'updated_by' => $userId,
]);
return $section->fresh()->loadRelatedEntities();
}
/**
* 섹션 삭제 (Soft Delete) + 링크 해제
*/
public function destroy(int $id): void
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$section = ItemSection::where('tenant_id', $tenantId)
->where('id', $id)
->first();
if (! $section) {
throw new NotFoundHttpException(__('error.section_not_found'));
}
// 1. 모든 부모 링크 해제 (페이지-섹션 관계)
EntityRelationship::where('tenant_id', $tenantId)
->where('child_type', EntityRelationship::TYPE_SECTION)
->where('child_id', $id)
->delete();
// 2. 모든 자식 링크 해제 (섹션-필드, 섹션-BOM 관계)
EntityRelationship::where('tenant_id', $tenantId)
->where('parent_type', EntityRelationship::TYPE_SECTION)
->where('parent_id', $id)
->delete();
// 3. 섹션 Soft Delete
$section->update(['deleted_by' => $userId]);
$section->delete();
// 4. 하위 필드/BOM도 Soft Delete
$section->loadRelatedEntities();
foreach ($section->getRelation('fields') as $field) {
$field->update(['deleted_by' => $userId]);
$field->delete();
}
foreach ($section->getRelation('bomItems') as $bomItem) {
$bomItem->update(['deleted_by' => $userId]);
$bomItem->delete();
}
}
}