Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Checkbox in Gridfield with relation


Go to End


1559 Views

Avatar
Dobby

Community Member, 15 Posts

9 August 2016 at 1:23am

I have an interessting problem and hope someone can help.

It's a project for a sportsclub with several teams.

I have several dataobject with relations.

First I have the games with a relation to the lineup, who is playing in this game.
I get the data for the lineup from seasonplayer where I have the data who is playing for a certain team in a certain season.

So first I have a game and there I want the lineup, which is related to the game (of course ;) ). The relation between the game and the lineup is has_many.
I created a gridfield with the list of the players for that season. In this gridfield I want a checkbox who plays in this game. If there is an entry for a player in the table lineup for this game, the checkbox should be selected. If not empty.
Maybe someone knows how I can create that.

I tried the addon GridFieldRelationHandler, but I get an error that a RelationList is needed. I don't know how to create one, because I didn't find something helpful about that. I hope somebody can help.

Here is the code

game

class Spiele extends DataObject {
	
	private static $db = array(
	"Datum" => "Date",
	"Zeit" => "Time",
	"ToreHeim" => "Int",
	"ToreGast" => "Int",
	"Zuschauer" => "Int",
	"Gespielt" => "Boolean",
	"Stats" => "Boolean",
	"Spieldauer" => "Time"
	);
	
	private static $has_one = array(
	"Heim" => "Teams",
	"Gast" => "Teams",
	"Saison" => "Saison",
	"SpielTyp" => "SpielTyp",
	"ErgebnisArt" => "ErgebnisArt"
	);
	private static $has_many = array(
	"Tore" => "Tore",
	"Strafen" => "Strafen",
	"Aufstellung" => "Aufstellung"
	);
	 
function getCMSFields() {
		

		$fields = parent::getCMSFields();
		$fields->addFieldToTab("Root.Main", new CheckboxField("Gespielt","Gespielt"));
		$fields->addFieldToTab("Root.Main", new DropdownField("SpielTypID","Art des Spiels",SpielTyp::get()->Map("ID", "Name","-")));
		$fields->addFieldToTab("Root.Main", new TextField("Zuschauer","Zuschauer"));
		if ($this->SpielTypID == 1) {
		$fields->addFieldToTab("Root.Main", new DropdownField("HeimID","Heimteam",Teams::get()->Map("ID", "Name","-")));
		$fields->addFieldToTab("Root.Main", new DropdownField("GastID","Gastteam",Teams::get()->Map("ID", "Name","-")));	
		} else {
		$fields->addFieldToTab("Root.Main", new DropdownField("HeimID","Heimteam",Teams::get()->filter(array("Saison.ID" => $this->SaisonID))->Map("ID", "Name","-")->toArray(),1));
		$fields->addFieldToTab("Root.Main", new DropdownField("GastID","Gastteam",Teams::get()->filter(array("Saison.ID" => $this->SaisonID))->Map("ID", "Name","-")->toArray(),1));}
	 	$fields->addFieldToTab("Root.Main", new TextField("ToreHeim","Tore Heim"));
		$fields->addFieldToTab("Root.Main", new TextField("ToreGast","Tore Gast"));
	 	$fields->addFieldToTab("Root.Main", new CheckboxField("Stats","Stats"));
	 	
	/*Aufstellung */								
	 $fields->addFieldToTab('Root.Aufstellungen', new GridField(
            'Aufstellung',
            'Aufstellung',
            SpielerSaison::get()->filter(array("SaisonID" => $this->SaisonID)),
   			GridFieldConfig_RecordEditor::create()
   			
		));
		$grid->getConfig()->getComponentByType('GridFieldEditableColumns')->setDisplayFields(array(
                "Checkbox should be here"
		'Nr' => "Nr",
		'Spieler.Name' => 'Spieler'	
	));	
	 	return $fields;
	 } 

playerseason

class SpielerSaison extends DataObject {
	
	private static $db = array(
	"Nr" => "Int"	
	);
	
	private static $has_one = array(
	"Spieler" => "Spieler",
	"Saison" => "Saison",
	"Team" => "Teams"
	);

			 
function getCMSFields() {
		

		$fields = parent::getCMSFields();

	 	return $fields;
	 } 

lineup

class Aufstellung extends DataObject {
	
	private static $db = array(
	"Eiszeit" => "Varchar(6)"
	);
	
	private static $has_one = array(
	"Spiel" => "Spiele",
	"Spieler" => "Spieler"
	);

		 
function getCMSFields() {
		
echo $this->SaisonID;
		$fields = parent::getCMSFields();
	 	return $fields;
	 }
}