55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class SpecializationModel extends Model
|
|
{
|
|
protected $table = 'specializations';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $protectFields = true;
|
|
protected $allowedFields = ['name', 'status', 'created_at'];
|
|
protected $useTimestamps = true;
|
|
protected $createdField = 'created_at';
|
|
|
|
public function getOptionNames(): array
|
|
{
|
|
$databaseNames = $this->select('name')->orderBy('name', 'ASC')->findColumn('name') ?: [];
|
|
natcasesort($databaseNames);
|
|
return array_values(array_unique($databaseNames));
|
|
}
|
|
|
|
public function ensureNamesExist(array $names): array
|
|
{
|
|
$names = array_values(array_unique(array_filter(array_map(
|
|
static fn ($name) => trim((string) $name),
|
|
$names
|
|
))));
|
|
|
|
if ($names === []) {
|
|
return [];
|
|
}
|
|
|
|
$existingRows = $this->whereIn('name', $names)->findAll();
|
|
$nameToId = [];
|
|
|
|
foreach ($existingRows as $row) {
|
|
$nameToId[$row['name']] = (int) $row['id'];
|
|
}
|
|
|
|
foreach ($names as $name) {
|
|
if (isset($nameToId[$name])) {
|
|
continue;
|
|
}
|
|
|
|
$this->insert(['name' => $name, 'status' => 1]);
|
|
$nameToId[$name] = (int) $this->getInsertID();
|
|
}
|
|
|
|
return $nameToId;
|
|
}
|
|
}
|