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.

Data Model Questions /

Many many relationships question.


Reply


378 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.