ORM dot notation with many_many and belongs_many_many relationships

Silverstripe Version: 5.1

Question:
I am specifying the return ends of a many_many and belongs_many_many relation using dot notation like this:

ServicePage.php
namespace MyNamespace;
use SilverStripe\Assets\Image;
class ServicePage extends Page
{
    private static $many_many = [  
        'ProjectImages' => Image::class . '.ServicePages',
    ];
}

MyImageExtension.php
namespace MyNamespace;
class MyImageExtension extends DataExtension
{
    private static $belongs_many_many = [
        'ServicePages' => ServicePage::class . '.ProjectImages',
    ];
}

Images.yml
SilverStripe\Assets\Image:
  extensions:
    - MyNamespace\MyImageExtension

However when I /dev/build I get:

InvalidArgumentException: many_many relation MyNamespace\ServicePage.ProjectImages references class SilverStripe\Assets\Image.ServicePage which doesn't exist

This feels like a namespace issue, but I am probably wrong.
How do you use “dot” notation like this across different namespaces?

I figured it out in the end, you don’t add the dot notation to the many_many, just to the belongs_many_many:

ServicePage.php
namespace MyNamespace;
use SilverStripe\Assets\Image;
class ServicePage extends Page
{
    private static $many_many = [  
        'ProjectImages' => Image::class,
    ];
}

MyImageExtension.php
namespace MyNamespace;
class MyImageExtension extends DataExtension
{
    private static $belongs_many_many = [
        'ServicePages' => ServicePage::class . '.ProjectImages',
    ];
}
1 Like