First, I'd like to thank you for this package and all of the other Spatie packages.
OK here's my issue. Let's say I have a User model that uses this package. Then I make a SuperUser model that extends the User model. Since the SuperUser model has a different namespace, it is not aware of the media items attached to the User model itself. For other polymorphic relations that I have created myself, I am able to override the getMorphClass() method on the SuperUser model, like below:
public function getMorphClass()
{
return 'App\Models\User';
}
For some reason, this doesn't appear to work with this package. I'd be happy to PR something, but I'm trying to first understand whether or not this is a bug, intended functionality or maybe there is some other built-in method of solving this problem.
Thanks.
I'm thinking that a morph map should just work. If I'm not mistaken the testsuite contains a test with a morphmap.
If your SuperUser has a one on one relation with User you could add methods like this on SuperUser:
// not tested...
public function getMedia($collectionName = null) {
return $this->user->getMedia($collectionName);
}
But yeah, that's a bit cumbersome.
My SuperUser model doesn't have a one on one relation with User, it actually extends User like this:
namespace App\Models;
use App\Models\User as BaseUser;
class SuperUser extends BaseUser
So when I try to access the media, it does so with the assumption that it will be found with a model_type of App\Models\SuperUser instead of App\Models\User. While overriding the getMorphClass method, from within SuperUser, seems to work for other MorphToMany relationships that exist on User, it does not work here.
I thought about the morphMap solution as well, but that seems to provide a mapping from database to class, which isn't really what I need.
I have resorted to doing the following, which I don't love, but which does work:
$media = Media::whereModelType('App\Models\User')->whereModelId($id)->get();
Well, that's what I get for working late at night. My original solution _does_ work, I just didn't have any media attached to the model that I was testing. Sorry for bothering you. For anyone else who finds this, overriding the getMorphClass method on my SuperUser model works:
public function getMorphClass()
{
return 'App\Models\User';
}
Most helpful comment
Well, that's what I get for working late at night. My original solution _does_ work, I just didn't have any media attached to the model that I was testing. Sorry for bothering you. For anyone else who finds this, overriding the
getMorphClassmethod on mySuperUsermodel works: