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.

Data Model Questions /

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

Many many relationships question.


Go to End


1076 Views

Avatar
T3nD4n

Community Member, 16 Posts

16 May 2013 at 8:01am

First of all, sorry about my poor english.
I'am new in silverstripe and i have a question about relationships.

here are my classes:

class Jugador extends DataObject {
	static $db = array(
			'Nombre' => 'Varchar',
			'Apellido' => 'Varchar',
	);
	static  $belongs_many_many = array(
			'Partido' => 'Partido'
	);
}

and

class Partido extends Page {

	public static $db = array(
		"Fecha"=>"date",
		"Resultado"=>"Varchar"
	);

	public static $many_many = array(
		"Jugadores"=>"Jugador"
	);
	
	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$config = GridFieldConfig_RelationEditor::create();
		$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
				'Nombre' => 'Nombre',
				'Apellido'=> 'Apellido' 
		));
		
		$config->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchFields(array(
				'Nombre', 
				'Apellido'))->setResultsFormat('$Nombre - $Apellido');

		$jugadoresField = new GridField(
				'Jugadores', // Field name
				'Jugador', // Field title
				$this->Jugadores(), 
				$config
		);
		
		$fields->addFieldToTab('Root.Jugadores', $jugadoresField);
		
		$dateField = new DateField('Fecha');
		$dateField->setConfig('showcalendar', true);
		$fields->addFieldToTab('Root.Main', $dateField, 'Content');
		$fields->addFieldToTab("Root.Main", new TextField('Resultado'), 'Content');
		
		return $fields;
	}
}

so far i have a many many relation between "Partido" and "Jugador" (in english Match and players).
if i add a "partido" i can link existing "jugador" to this new partido and works fine.
the thing that i dont how to do is, that each player must have a diferent score on each "partido".
and i dont know how to add that relation in the classes.

any help will be appreciated.