75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class DoctorSpecializationModel extends Model
|
|
{
|
|
protected $table = 'doctor_specializations';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [
|
|
'doctor_id',
|
|
'specialization_id',
|
|
'status',
|
|
'created_by',
|
|
'updated_by',
|
|
];
|
|
protected $useTimestamps = true;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
|
|
public function getNamesForDoctor(int $doctorId): array
|
|
{
|
|
$rows = $this->select('specializations.name')
|
|
->join('specializations', 'specializations.id = doctor_specializations.specialization_id')
|
|
->where('doctor_specializations.doctor_id', $doctorId)
|
|
->findAll();
|
|
|
|
return array_values(array_filter(array_map(
|
|
static fn (array $row) => ! empty($row['name']) ? $row['name'] : null,
|
|
$rows
|
|
)));
|
|
}
|
|
|
|
public function deleteByDoctorId(int $doctorId): void
|
|
{
|
|
$this->where('doctor_id', $doctorId)->delete();
|
|
}
|
|
|
|
public function syncDoctorSpecializations(int $doctorId, array $specializationIds, ?int $actorId = null): void
|
|
{
|
|
$specializationIds = array_values(array_unique(array_map('intval', $specializationIds)));
|
|
$existingRows = $this->where('doctor_id', $doctorId)->findAll();
|
|
$existingBySpecId = [];
|
|
|
|
foreach ($existingRows as $row) {
|
|
$existingBySpecId[(int) $row['specialization_id']] = $row;
|
|
}
|
|
|
|
$existingIds = array_keys($existingBySpecId);
|
|
$toDelete = array_diff($existingIds, $specializationIds);
|
|
$toInsert = array_diff($specializationIds, $existingIds);
|
|
|
|
if ($toDelete !== []) {
|
|
$this->where('doctor_id', $doctorId)
|
|
->whereIn('specialization_id', $toDelete)
|
|
->delete();
|
|
}
|
|
|
|
foreach ($toInsert as $specializationId) {
|
|
$this->insert([
|
|
'doctor_id' => $doctorId,
|
|
'specialization_id' => $specializationId,
|
|
'status' => 1,
|
|
'created_by' => $actorId,
|
|
'updated_by' => $actorId,
|
|
]);
|
|
}
|
|
}
|
|
}
|