Fix: FIxing Bug on resolving relation on Products, Orders, and Users
This commit is contained in:
+23
-13
@@ -102,9 +102,21 @@ class CorePlugin implements Plugin
|
|||||||
CustomerResource::class => CustomerErasureRelationsExtension::class,
|
CustomerResource::class => CustomerErasureRelationsExtension::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Product::macro('reviews', function (): HasMany {
|
// resolveRelationUsing(), not macro() — Illuminate\Database\Eloquent\
|
||||||
/** @var Product $this */
|
// Model does not use the Macroable trait in this Laravel version, so
|
||||||
return $this->hasMany(ProductReview::class);
|
// Product::macro(...)/Customer::macro(...)/$userModel::macro(...)
|
||||||
|
// silently fall through to Model::__callStatic(), which instantiates
|
||||||
|
// the model and tries to call the method as a real one, hitting
|
||||||
|
// newQuery()->getConnection() — this crashes every console command
|
||||||
|
// and every request, since CorePlugin::register() runs during
|
||||||
|
// provider registration, before the DB connection is configured
|
||||||
|
// ("Call to a member function connection() on null"). This bit us
|
||||||
|
// once already; resolveRelationUsing() is Eloquent's real, intended,
|
||||||
|
// connection-free extension point for exactly this (Order::
|
||||||
|
// resolveRelationUsing('shipments', ...) in ShippingServiceProvider
|
||||||
|
// already uses it correctly).
|
||||||
|
Product::resolveRelationUsing('reviews', function (Product $product): HasMany {
|
||||||
|
return $product->hasMany(ProductReview::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Customer::erasureRequests()/exportRequests() and the User-model
|
// Customer::erasureRequests()/exportRequests() and the User-model
|
||||||
@@ -115,24 +127,22 @@ class CorePlugin implements Plugin
|
|||||||
// Customer or a User (see docs/privacy.md "User-scope vs Customer-scope"),
|
// Customer or a User (see docs/privacy.md "User-scope vs Customer-scope"),
|
||||||
// so this is a MorphMany built by hand rather than a bare Eloquent
|
// so this is a MorphMany built by hand rather than a bare Eloquent
|
||||||
// convention lookup.
|
// convention lookup.
|
||||||
Customer::macro('erasureRequests', function (): MorphMany {
|
Customer::resolveRelationUsing('erasureRequests', function (Customer $customer): MorphMany {
|
||||||
/** @var Customer $this */
|
return $customer->morphMany(DataErasureRequest::class, 'subject', 'subject_type', 'subject_id');
|
||||||
return $this->morphMany(DataErasureRequest::class, 'subject', 'subject_type', 'subject_id');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Customer::macro('exportRequests', function (): MorphMany {
|
Customer::resolveRelationUsing('exportRequests', function (Customer $customer): MorphMany {
|
||||||
/** @var Customer $this */
|
return $customer->morphMany(DataExportRequest::class, 'subject', 'subject_type', 'subject_id');
|
||||||
return $this->morphMany(DataExportRequest::class, 'subject', 'subject_type', 'subject_id');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$userModel = config('auth.providers.users.model');
|
$userModel = config('auth.providers.users.model');
|
||||||
|
|
||||||
$userModel::macro('erasureRequests', function (): MorphMany {
|
$userModel::resolveRelationUsing('erasureRequests', function ($user): MorphMany {
|
||||||
return $this->morphMany(DataErasureRequest::class, 'subject', 'subject_type', 'subject_id');
|
return $user->morphMany(DataErasureRequest::class, 'subject', 'subject_type', 'subject_id');
|
||||||
});
|
});
|
||||||
|
|
||||||
$userModel::macro('exportRequests', function (): MorphMany {
|
$userModel::resolveRelationUsing('exportRequests', function ($user): MorphMany {
|
||||||
return $this->morphMany(DataExportRequest::class, 'subject', 'subject_type', 'subject_id');
|
return $user->morphMany(DataExportRequest::class, 'subject', 'subject_type', 'subject_id');
|
||||||
});
|
});
|
||||||
|
|
||||||
LunarStaff::addActivitylogExcept([
|
LunarStaff::addActivitylogExcept([
|
||||||
|
|||||||
Reference in New Issue
Block a user