Skip to content

Commit f1826ea

Browse files
committed
1 parent ea8230f commit f1826ea

5 files changed

Lines changed: 32 additions & 32 deletions

File tree

app/src/Database/Models/Member.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,19 @@
1414
use Illuminate\Database\Eloquent\Relations\HasOne;
1515
use UserFrosting\Sprinkle\Account\Database\Models\User;
1616

17-
trait LinkMemberAux
18-
{
19-
/**
20-
* The "booting" method of the trait.
21-
*/
22-
protected static function bootLinkMemberAux(): void
23-
{
24-
/**
25-
* Create a new MemberAux if necessary, and save the associated member data every time.
26-
*/
27-
static::saved(function ($member) {
28-
$member->createAuxIfNotExists();
29-
30-
if ($member->auxType) {
31-
// Set the aux PK, if it hasn't been set yet
32-
if (!$member->aux->id) {
33-
$member->aux->id = $member->id;
34-
}
35-
36-
$member->aux->save();
37-
}
38-
});
39-
}
40-
}
41-
4217
/**
4318
* @property MemberAux $aux
4419
* @property string $city
4520
* @property string $country
4621
*/
4722
class Member extends User
4823
{
49-
use LinkMemberAux;
50-
5124
protected $fillable = [
5225
'user_name',
5326
'first_name',
5427
'last_name',
5528
'email',
5629
'locale',
57-
'theme',
5830
'group_id',
5931
'flag_verified',
6032
'flag_enabled',
@@ -76,6 +48,23 @@ protected static function boot()
7648
static::addGlobalScope(new MemberAuxScope());
7749
}
7850

51+
/**
52+
* Create a new MemberAux if necessary, and save the associated member data every time.
53+
*/
54+
protected static function booted(): void
55+
{
56+
static::saved(function (Member $member) {
57+
$member->createAuxIfNotExists();
58+
59+
// Set the aux PK, if it hasn't been set yet
60+
if ($member->aux->id !== 0) {
61+
$member->aux->id = $member->id;
62+
}
63+
64+
$member->aux->save();
65+
});
66+
}
67+
7968
/**
8069
* Custom mutator for Member property.
8170
*/

app/src/Database/Models/MemberAux.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use UserFrosting\Sprinkle\Core\Database\Models\Model;
1414

1515
/**
16+
* @property int $id
1617
* @property string $city
1718
* @property string $country
1819
*/

app/src/Database/Scopes/MemberAuxScope.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@ class MemberAuxScope implements Scope
2525
public function apply(Builder $builder, Model $model)
2626
{
2727
$baseTable = $model->getTable();
28-
// Hardcode the table name here, or you can access it using the classMapper and `getTable`
28+
// Hardcode the table name here, or inject the model and use `getTable`
2929
$auxTable = 'members';
3030

3131
// Specify columns to load from base table and aux table
32-
// @phpstan-ignore-next-line
3332
$builder->addSelect(
3433
"$baseTable.*",
3534
"$auxTable.city as city",
3635
"$auxTable.country as country"
3736
);
3837

3938
// Join on matching `member` records
40-
// @phpstan-ignore-next-line
4139
$builder->leftJoin($auxTable, function ($join) use ($baseTable, $auxTable) {
4240
$join->on("$auxTable.id", '=', "$baseTable.id");
4341
});

phpstan-baseline.neon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Query\\\\Builder\\:\\:addSelect\\(\\)\\.$#"
5+
count: 1
6+
path: app/src/Database/Scopes/MemberAuxScope.php
7+
8+
-
9+
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Query\\\\Builder\\:\\:leftJoin\\(\\)\\.$#"
10+
count: 1
11+
path: app/src/Database/Scopes/MemberAuxScope.php

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ includes:
99
- vendor/phpstan/phpstan-strict-rules/rules.neon
1010
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
1111
- vendor/phpstan/phpstan-mockery/extension.neon
12-
- vendor/phpstan/phpstan-phpunit/extension.neon
12+
- vendor/phpstan/phpstan-phpunit/extension.neon
13+
- phpstan-baseline.neon

0 commit comments

Comments
 (0)