Files
sam-manage/app/Models/Department.php
김보곤 051707c1be fix: [database] codebridge 모델 cross-DB 관계 오류 수정
- samdb 공유 모델에 $connection = 'mysql' 명시적 선언
- codebridge 모델에서 eager-load 시 connection 상속 방지
- 영향 모델: User, Tenant, Department, Process, File(2), Approval, AiQuotationModule, InterviewProject
2026-03-09 23:35:13 +09:00

77 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Department extends Model
{
use SoftDeletes;
protected $connection = 'mysql';
protected $table = 'departments';
protected $fillable = [
'tenant_id',
'parent_id',
'code',
'name',
'description',
'is_active',
'sort_order',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'tenant_id' => 'integer',
'parent_id' => 'integer',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
protected $hidden = [
'deleted_by',
'deleted_at',
];
/**
* 상위 부서
*/
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
/**
* 하위 부서
*/
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id');
}
/**
* 부서-사용자 매핑 (DepartmentUser pivot)
*/
public function departmentUsers(): HasMany
{
return $this->hasMany(DepartmentUser::class, 'department_id');
}
/**
* 부서에 속한 사용자들 (belongsToMany)
*/
public function users()
{
return $this->belongsToMany(User::class, 'department_user')
->withTimestamps()
->withPivot(['tenant_id', 'is_primary', 'joined_at', 'left_at']);
}
}