Skip to content

Commit

Permalink
Create BlockedBuild Eloquent model (#1757)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
williamjallen committed Oct 25, 2023
1 parent 8dec5f2 commit 5d43ab3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
43 changes: 43 additions & 0 deletions app/Models/BlockedBuild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property int $projectid
* @property string $buildname
* @property string $sitename
* @property string $ipaddress
*
* @mixin Builder<BlockedBuild>
*/
class BlockedBuild extends Model
{
protected $table = 'blockbuild';

public $timestamps = false;

protected $fillable = [
'projectid',
'buildname',
'sitename',
'ipaddress',
];

protected $casts = [
'id' => 'integer',
'projectid' => 'integer',
];

/**
* @return BelongsTo<Project, self>
*/
public function project(): BelongsTo
{
return $this->belongsTo(Project::class, 'id', 'projectid');
}
}
8 changes: 8 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,12 @@ public function builds(): HasMany
->orWhere('parentid', -1);
});
}

/**
* @return HasMany<BlockedBuild>
*/
public function blockedbuilds(): HasMany
{
return $this->hasMany(BlockedBuild::class, 'projectid', 'id');
}
}
18 changes: 11 additions & 7 deletions app/cdash/app/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\\<App\\\\Models\\\\BlockedBuild\\>\\:\\:findOrFail\\(\\)\\.$#"
count: 1
path: app/cdash/app/Model/Project.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Build\\>\\:\\:betweenDates\\(\\)\\.$#"
count: 1
Expand Down

0 comments on commit 5d43ab3

Please sign in to comment.