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

Placeholder: Implement polymorphic many-many-through 'to' support #9148

Open
kinglozzer opened this issue Jul 26, 2019 · 1 comment
Open

Comments

@kinglozzer
Copy link
Member

Affected Version

4.x

Description

Polymorphic many-many-through currently only supports a polymorphic 'from' part, not 'to'. For example:

class Basket extends DataObject
{
    private static $many_many = [
        'Items' => [
            'through' => BasketItem::class,
            'to' => 'Item',
            'from' => 'Basket'
        ]
    ];
}

class BasketItem extends DataObject
{
    private static $db = [
        'Quantity' => 'Int'
    ];

    private static $has_one = [
        'Item' => DataObject::class,
        'Basket' => Basket::class
    ];
}

This triggers an error on dev/build:

// @todo support polymorphic 'to'
throw new InvalidArgumentException(
"many_many through relation {$parentClass}.{$component} {$key} references a polymorphic field "
. "{$joinClass}::{$relation} which is not supported"
);

There are a few non-trivial challenges here. The 'to' side of the relation is used as the $dataClass when constructing a ManyManyThroughList. If $dataClass === DataObject::class, that causes a number of issues with with the underlying DataQuery logic as there’s obviously no DataObject table it can run queries on.

@GuySartorelli
Copy link
Member

I'm not convinced this is something we should support.
The case where from is not polymorphic, but to is.... just swap those around so the from is the polymorphic instead. That's supported.

The case where from and to are both polymorphic I think would be extremely difficult to do, and probably is a sign that the relation needs to be better thought out anyway.

I know this is a super old issue but @kinglozzer can you think of a good use case for this that isn't supported by the existing functionality?

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