Skip to content

Instantly share code, notes, and snippets.

@altwohill
Last active March 12, 2020 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altwohill/df5e06291405cf162351a7a6f3991913 to your computer and use it in GitHub Desktop.
Save altwohill/df5e06291405cf162351a7a6f3991913 to your computer and use it in GitHub Desktop.
<?php
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
class Team extends DataObject
{
private static $db = [
"Name" => "Varchar(255)",
];
private static $many_many = [
"Supporters" => [
'through' => TeamSupporter::class,
'from' => 'Team',
'to' => 'Supporter',
]
];
}
class Supporter extends DataObject
{
private static $db = [
"Name" => "Varchar(255)"
];
private static $belongs_many_many = [
'Supports' => Team::class,
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
if ($this->ID) {
$manymanyfields = FieldList::create(
TextField::create("Name"),
TextField::create("ManyMany[Ranking]", "Ranking")
);
$config = GridFieldConfig_RelationEditor::create();
$config->getComponentByType(GridFieldDetailForm::class)->setFields($manymanyfields);
$grid = GridField::create(
"Supports",
"Supports",
$this->Supports(),
$config
);
$fields->findOrMakeTab("Root.Supports")->replaceField("Supports", $grid);
}
return $fields;
}
}
class TeamSupporter extends DataObject
{
private static $db = [
'Ranking' => 'Int',
];
private static $has_one = [
'Team' => Team::class,
'Supporter' => Supporter::class,
];
private static $default_sort = '"TeamSupporter"."Ranking" ASC';
}
class TeamAdmin extends ModelAdmin
{
private static $managed_models = [
Team::class,
];
private static $url_segment = "teams";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment