Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager-loaded polymorphic relationship error #383

Open
A-Lawrence opened this issue Nov 10, 2020 · 2 comments
Open

Eager-loaded polymorphic relationship error #383

A-Lawrence opened this issue Nov 10, 2020 · 2 comments
Assignees

Comments

@A-Lawrence
Copy link

A-Lawrence commented Nov 10, 2020

Describe the bug

When eager-loading polymorphic relationships, the polymorphic relation appears to be ignored when retrieving from the cache.

The exception below, is raised:

Call to undefined method Admin\Models\Inventory::settings()

Eloquent Query

Scenario here is that we have SaleItems, with an associated Inventory. Each Inventory can have a polymorphic source (such as an Event).

This query then produces are an error:

        $orderItems = $orderItemQuery
            ->with([
                'saleItem.inventory.source.company',
                'saleItem.inventory.source.settings',
                'saleItem.inventory.source.stream',
                'saleItem.pricing',
                'saleItem.item'])
            ->latest()
            ->paginate($count);

It's as though the source part of the eager loaded relationship, for settings or stream, is not loaded (settings exists, stream does not so I'd expect a null return). The only reason that saleItem.inventory.source.company works is because Inventory has a company() relationship too, to scope to the current tenant.

The setup for the involved models, is as below - all of them extend an abstract Model which uses the Cachable trait.

class OrderItem extends Model {
    public function saleItem()
    {
        return $this->belongsTo(SaleItem::class);
    }
}

class SaleItem {
    public function inventory()
    {
        return $this->belongsTo(Inventory::class);
    }
}

class Inventory extends Model {
    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function source()
    {
        return $this->morphTo('source', 'entity_type', 'entity_id');
    }
}

class Event extends Model {
    public function inventory(): MorphOne
    {
        return $this->morphOne(Inventory::class, 'inventory', 'entity_type', 'entity_id');
    }

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function settings()
    {
        return $this->hasOne(EventSetting::class)->withDefault();
    }

    public function stream()
    {
        return $this->hasOne(EventStream::class)->withDefault();
    }
}

Stack Trace
https://flareapp.io/share/x7XgnRRP#F78

Environment

  • PHP: 7.4.9
  • OS: MacOS 10.15
  • Laravel: 8.13.0
  • Model Caching: 0.11.0

Edit: Potentially related to #379 - I hadn't spotted this one before posting!

@mikebronner
Copy link
Owner

mikebronner commented Dec 11, 2020

Hi @A-Lawrence, thanks for submitting this issue! I will try to get to it as soon as possible, which might be some time, as we re in the middle of moving. I welcome PRs with failing tests, as well as fixes, if you are able. :) Thanks!

@markodobric
Copy link

hey @mikebronner,

thank you for your great package!

but this issue still exists and represents blocker for fully usage, is there a plan to get this fixed?

thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants