remove entry totals from Doubler database. Save list of entries for each doubler.

This commit is contained in:
Matt Young 2025-06-23 00:52:19 -05:00
parent a27b8166e2
commit 630efaf00f
2 changed files with 12 additions and 5 deletions

View File

@ -5,8 +5,6 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use function PHPUnit\Framework\isInstanceOf;
class Doubler extends Model class Doubler extends Model
{ {
// Specify that we're not using a single primary key // Specify that we're not using a single primary key
@ -16,6 +14,10 @@ class Doubler extends Model
protected $guarded = []; protected $guarded = [];
protected $casts = [
'entries' => 'array',
];
public function student(): BelongsTo public function student(): BelongsTo
{ {
return $this->belongsTo(Student::class); return $this->belongsTo(Student::class);
@ -39,7 +41,8 @@ class Doubler extends Model
*/ */
public static function syncForEvent($eventId): void public static function syncForEvent($eventId): void
{ {
if (isInstanceOf(Event::class, $eventId)) {
if ($eventId instanceof Event) {
$eventId = $eventId->id; $eventId = $eventId->id;
} }
@ -61,6 +64,11 @@ class Doubler extends Model
} else { } else {
$acceptedEntryId = null; $acceptedEntryId = null;
} }
// Form a list of entry IDs for this doubler
$entryList = [];
$entryList = $student->entriesForEvent($eventId)->pluck('id')->toArray();
// Create or update the doubler record // Create or update the doubler record
static::updateOrCreate( static::updateOrCreate(
[ [
@ -68,8 +76,8 @@ class Doubler extends Model
'event_id' => $eventId, 'event_id' => $eventId,
], ],
[ [
'entries' => $entryList,
'accepted_entry' => $acceptedEntryId, 'accepted_entry' => $acceptedEntryId,
'entry_count' => $student->entriesForEvent($eventId)->count(),
] ]
); );
} }

View File

@ -20,7 +20,6 @@ return new class extends Migration
$table->foreignIdFor(Event::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate(); $table->foreignIdFor(Event::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
// Doubler Specific Fields // Doubler Specific Fields
$table->integer('entry_count');
$table->json('entries')->nullable(); $table->json('entries')->nullable();
$table->foreignIdFor(Entry::class, 'accepted_entry')->nullable()->constrained('entries')->cascadeOnDelete()->cascadeOnUpdate(); $table->foreignIdFor(Entry::class, 'accepted_entry')->nullable()->constrained('entries')->cascadeOnDelete()->cascadeOnUpdate();