Files
alrahma_sunday_school/app/Models/PromotionQueueModel.php
root 716cc8b8d3
Tests / PHPUnit (push) Failing after 1m20s
fix school year for all tables
2026-07-18 14:45:38 -04:00

56 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
use App\Models\Concerns\SchoolYearAutoFillTrait;
class PromotionQueueModel extends Model
{
use SchoolYearAutoFillTrait;
protected $table = 'promotion_queue';
protected $primaryKey = 'id';
protected $allowedFields = [
'student_id',
'from_class_section_id',
'to_class_id',
'to_class_section_id',
'school_year_from',
'school_year_to',
'status',
'created_at',
'updated_at',
'updated_by',
'school_year',
];
protected $validationRules = [
'school_year' => 'required|string|max_length[9]',
];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
public function upsert(array $data): bool
{
if (empty($data['student_id']) || empty($data['school_year_to'])) {
return false;
}
$data['school_year'] = (string)($data['school_year'] ?? $data['school_year_from'] ?? $data['school_year_to']);
$existing = $this->where('student_id', (int)$data['student_id'])
->where('school_year_to', (string)$data['school_year_to'])
->first();
if ($existing) {
return (bool) $this->update((int)$existing[$this->primaryKey], array_merge($existing, $data));
}
return (bool) $this->insert($data);
}
}