2025-10-29 11:08:38 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Models\Organization;
|
|
|
|
|
use App\Models\BaseModel;
|
|
|
|
|
|
|
|
|
|
class DisciplineModel extends BaseModel {
|
|
|
|
|
protected $table = 'discipline';
|
|
|
|
|
protected $primaryKey = 'DisciplineID';
|
2026-03-10 16:40:37 +07:00
|
|
|
protected $allowedFields = ['DisciplineCode', 'DisciplineName', 'SiteID', 'Parent', 'SeqScr', 'SeqRpt', 'CreateDate', 'EndDate'];
|
2025-10-29 11:08:38 +07:00
|
|
|
|
|
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
protected $createdField = 'CreateDate';
|
|
|
|
|
protected $updatedField = '';
|
|
|
|
|
protected $useSoftDeletes = true;
|
|
|
|
|
protected $deletedField = 'EndDate';
|
|
|
|
|
|
2025-12-02 07:09:24 +07:00
|
|
|
public function getDisciplines($filter) {
|
2025-12-08 11:21:48 +07:00
|
|
|
$builder = $this->select('discipline.DisciplineID, discipline.DisciplineCode, discipline.DisciplineName, discipline.SiteID,
|
2026-03-10 16:40:37 +07:00
|
|
|
discipline.Parent, discipline.SeqScr, discipline.SeqRpt, d.DisciplineCode as ParentCode,d.DisciplineName as ParentName')
|
2025-12-08 11:21:48 +07:00
|
|
|
->join('discipline as d', 'd.DisciplineID = discipline.Parent', 'left');
|
2025-12-02 07:09:24 +07:00
|
|
|
|
|
|
|
|
if (!empty($filter['DisciplineCode'])) {
|
|
|
|
|
$builder->like('DisciplineCode', $filter['DisciplineCode'], 'both');
|
|
|
|
|
}
|
|
|
|
|
if (!empty($filter['DisciplineName'])) {
|
|
|
|
|
$builder->like('DisciplineName', $filter['DisciplineName'], 'both');
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 11:21:48 +07:00
|
|
|
$rows = $builder->findAll();
|
|
|
|
|
// Build nested structure: parent with children
|
|
|
|
|
$parents = [];
|
|
|
|
|
$children = [];
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
if (empty($row['Parent'])) {
|
|
|
|
|
$parents[$row['DisciplineID']] = $row;
|
|
|
|
|
$parents[$row['DisciplineID']]['children'] = [];
|
|
|
|
|
} else {
|
|
|
|
|
$children[$row['Parent']][] = $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Attach children to parents
|
|
|
|
|
foreach ($children as $parentId => $childList) {
|
|
|
|
|
if (isset($parents[$parentId])) {
|
|
|
|
|
$parents[$parentId]['children'] = $childList;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Return as indexed array
|
|
|
|
|
return array_values($parents);
|
2025-12-02 07:09:24 +07:00
|
|
|
}
|
2025-10-29 11:08:38 +07:00
|
|
|
}
|