silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
723 stars 821 forks source link

Allow "from" class in many_many through to accept subclasses #11229

Closed GuySartorelli closed 4 months ago

GuySartorelli commented 4 months ago

Subclasses should be allowed to declare usage of a given many_many through join class. For example, say I have a module that provides a MySpecialModel class. I want to make it easy to have a many_many through relation from this class to pages - but explicitly to pages. My use case doesn't allow random dataobjects to own my special model.

The below code demonstrates how this could be used (replacing MySpecialModel with Image so you can dump this int a project and try it out).

This fails because the linking object's relation class doesn't exactly match the owner. Sharing the linking objects across various entries in the ancestry should be a supported use case.

class HomePage extends Page
{
    private static $many_many = [
        'HeroImages' => [
            'through' => PageImageLink::class,
            'from' => 'Page',
            'to' => 'Image',
        ]
    ];

}
class PageImageLink extends DataObject
{
    private static $has_one = [
        'Page' => SiteTree::class,
        'Image' => Image::class,
    ];
}

PRs

GuySartorelli commented 4 months ago

@emteknetnz You've assigned this to me but the PR is merged - before I close this I just want to double check if that was a mistake, or if there's something I'm missing here?