Accessing fields on many_many through relationship joined on a separate DataObject

Silverstripe Version: 4.5

Question:

I have set up the following:

class Trip extends DataObject
{
    private static $many_many = [
        "Clients" => [
            'through' => Booking::class,
            'from' => 'Trip',
            'to' => 'Client',
        ]
    ];
}
class Client extends Member
{
    private static $belongs_many_many = [
        'Trips' => Trip::class,
    ];
}
class Booking extends DataObject
{
    private static $db = [
        'Status' => 'Enum("unconfirmed,confirmed","unconfirmed")',
    ];

    private static $has_one = [
        'Trip' => Trip::class,
        'Client' => Client::class,
    ];
}

I’ve set up a Clients GridField (in the Trip Details form in CMS) with editable columns. I’d like to be able to add the Booking.Status field as an editable field, but haven’t figured out how to do so. Eg. a row in GridField would show Client.Name, Client.Email, Booking.Status (all fields except Booking.Status are already working fine).

Any help would be much appreciated!

1 Like

Ok, so I’ve managed to access the field info with Join.Status, but it won’t give me an editable field, eg. I’d expect the following to give me an editable dropdown field ???

$displayfields = [
		'Name' => [
			'title' => 'Name',
			'field' => ReadonlyField::class
        ],
		'Join.Status' => [
			'title' => 'Booking Status',
			'callback' => function($record, $column, $grid) {
				return DropdownField::create($column)
                    ->setSource(Booking::get()->DBObject('Status')->enumValues());
			}
		]
    ];

Bumping as I too am having trouble accessing ManyMany “through” fields. ManyMany_extraFields seem to work fine, but using ‘ManyMany[FieldName]’ doesn’t seem to work in the same way when the Field is managed by the through object (as above).
Any help would be much appreciated.

If I understood you correctly, the problem is dot syntax is not fully supported (yet) there. See the issue I filed on GitHub.

As mentioned by the last comment, the solution was to apply silverstripe/silverstripe-framework#9192, still WIP but already used in production.

1 Like