From 5d43ab39fe3304427a278cf0af344aa29d770d31 Mon Sep 17 00:00:00 2001 From: William Allen <16820599+williamjallen@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:43:10 -0400 Subject: [PATCH] Create `BlockedBuild` Eloquent model (#1757) The "blocked build" functionality currently resides entirely within the project model. This PR creates a new Eloquent model to represent the `blockbuild` table. This PR is part of our ongoing effort to migrate our SQL queries to Laravel's Eloquent ORM. --- app/Models/BlockedBuild.php | 43 +++++++++++++++++++++++++++++++++ app/Models/Project.php | 8 ++++++ app/cdash/app/Model/Project.php | 18 ++++++++------ phpstan-baseline.neon | 5 ++++ 4 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 app/Models/BlockedBuild.php diff --git a/app/Models/BlockedBuild.php b/app/Models/BlockedBuild.php new file mode 100644 index 0000000000..9578bcc93e --- /dev/null +++ b/app/Models/BlockedBuild.php @@ -0,0 +1,43 @@ + + */ +class BlockedBuild extends Model +{ + protected $table = 'blockbuild'; + + public $timestamps = false; + + protected $fillable = [ + 'projectid', + 'buildname', + 'sitename', + 'ipaddress', + ]; + + protected $casts = [ + 'id' => 'integer', + 'projectid' => 'integer', + ]; + + /** + * @return BelongsTo + */ + public function project(): BelongsTo + { + return $this->belongsTo(Project::class, 'id', 'projectid'); + } +} diff --git a/app/Models/Project.php b/app/Models/Project.php index 065ab7e0af..db62a88501 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -140,4 +140,12 @@ public function builds(): HasMany ->orWhere('parentid', -1); }); } + + /** + * @return HasMany + */ + public function blockedbuilds(): HasMany + { + return $this->hasMany(BlockedBuild::class, 'projectid', 'id'); + } } diff --git a/app/cdash/app/Model/Project.php b/app/cdash/app/Model/Project.php index 71b63cc30c..c899e88081 100644 --- a/app/cdash/app/Model/Project.php +++ b/app/cdash/app/Model/Project.php @@ -1264,17 +1264,21 @@ public function InitialSetup(): bool public function AddBlockedBuild(string $buildname, string $sitename, string $ip): int { - return DB::table('blockbuild')->insertGetId([ - 'projectid' => $this->Id, - 'buildname' => $buildname, - 'sitename' => $sitename, - 'ip' => $ip, - ]); + return EloquentProject::findOrFail((int) $this->Id) + ->blockedbuilds() + ->create([ + 'buildname' => $buildname, + 'sitename' => $sitename, + 'ipaddress' => $ip, + ])->id; } public function RemoveBlockedBuild(int $id): void { - DB::table('blockbuild')->delete($id); + EloquentProject::findOrFail((int) $this->Id) + ->blockedbuilds() + ->findOrFail($id) + ->delete(); } /** Delete old builds if this project has too many. */ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fb303fb5de..504979af8d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -9383,6 +9383,11 @@ parameters: count: 1 path: app/cdash/app/Model/Project.php + - + message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:findOrFail\\(\\)\\.$#" + count: 1 + path: app/cdash/app/Model/Project.php + - message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\\\:\\:betweenDates\\(\\)\\.$#" count: 1