Jump to:

22976 Posts in 11667 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » [SOLVED]Lost relationhips reference in nested Gridfields.

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 277 Views
  • T3nD4n
    Avatar
    Community Member
    16 Posts

    [SOLVED]Lost relationhips reference in nested Gridfields. Link to this post

    Hi! i'm very newbie in Silverstripe, and here i'm again with a question.

    Here are my clases so you can see the relationships.

    class Partido extends Page {

       static $db = array(
          'Resultado' => 'Varchar',
          'Fecha' => 'date',
          'GolesRojo' => 'int',
          'GolesNegro' => 'int'
       );

       static $has_many = array(
          'Equipos' => 'Equipo',
          'Puntuaciones' => 'Puntuacion

       public function getCMSFields() {
          $fields = parent::getCMSFields();

          $dateField = new DateField('Fecha');
          $dateField->setConfig('showcalendar', true);
          
          $fields->addFieldToTab('Root.Main', $dateField, 'Content');
          $fields->addFieldToTab("Root.Main", new TextField("Resultado"), 'Content');
                
          $config = GridFieldConfig_RecordEditor::create();
          
          $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
                'Color' => 'Color'
          ));
          $equipos = new GridField(
                'Equipos', // Field name
                'Equipo', // Field title
                $this->Equipos(),
                $config
          );
          
          $fields->addFieldToTab('Root.Equipos', $equipos);
          return $fields;
       }

    class Equipo extends DataObject {

       static $db = array(
          'Color' => 'Varchar',
       );

       static $many_many = array(
          'Jugadores' => 'Jugador'
       );

       static $has_one = array(
          'Partido' => 'Partido'
       );

       public function getCMSFields() {
       
          $fields = parent::getCMSFields();

          $field = new DropdownField('PartidoID', 'Partido', Partido::get()->map('ID', "CustomTitle"));
          $field->setDisabled(true);
          $field->performDisabledTransformation();
          $fields->push($field);
                   
          $config = GridFieldConfig_RelationEditor::create();
          $config->removeComponentsByType('GridFieldAddNewButton');
          $config->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchFields(array('Nombre', 'Apellido'))->setResultsFormat('$Nombre - $Apellido');
          $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
                'Nombre' => 'Nombre',
                'Apellido'=> 'Apellido',
                'Puntuaciones.Puntos' => "Puntos"
          ));

          $puntuaciones = new GridField(
                'Jugadores', // Field name
                'Jugador', // Field title
                $this->Jugadores(), // List of all related students
                $config
          );

          $fields->addFieldToTab('Root.Main', $puntuaciones);
          return $fields;
       }
    }

    class Jugador extends DataObject {
       static $db = array(
          'Nombre' => 'Varchar',
          'Apellido' => 'Varchar'
       );
       
       static $has_many = array(
          'Puntuaciones' => 'Puntuacion',
       );
       
       static $belongs_many_many = array(
          'Equipos' => 'Equipo',
       );
       
       public function getCustomTitle() {
          return $this->Nombre." ".$this->Apellido;
       }
       
       public function getCMSFields() {
          
          $fields = parent::getCMSFields();
          $field = new TextField('PuntosID', 'Puntaje', $this->Puntuaciones.PuntosID);
          $fields->push($field);
          /*$fields->addFieldToTab("Root.Main", new TextField("Nombre"), 'Content');
          $fields->addFieldToTab("Root.Main", new TextField("Apellido"), 'Content');
          $fields->addFieldToTab("Root.Main", new TextField("Puntos"), 'Content');
          return $fields;
          // Name, Description and Website fields
          $config = GridFieldConfig_RelationEditor::create();
          // Set the names and data for our gridfield columns
          $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
                'Puntos' => 'Puntos',
                //'Project.Title'=> 'Project' // Retrieve from a has-one relationship
          ));
          
          /*return new FieldList(
                new TextField('Nombre', 'Nombre'),
                new TextField('Apellido', 'Apellido'),
                new DropdownField(
                      'Puntuacion',
                      'Puntuacion',
                      //Puntuacion::get()->map("Puntos", "Puntos")
                )
                //new TextField('Puntuacion.Puntos', 'Puntuacion')
                //new GridField("Puntuacion", "Puntuacion", $this->Puntos(), $config, $this)
          );*/
          $jugador = Jugador::get()->byID($this->ID);
          if($jugador){
             $config = GridFieldConfig_RelationEditor::create();
             
             $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
                   'Puntos' => 'Puntos'
             ));
             $equipos = new GridField(
                   'Puntuaciones', // Field name
                   'Puntuacion', // Field title
                   $this->Puntuaciones(), // List of all related students
                   $config
             );
             
             $fields->addFieldToTab('Root.Main', $equipos);
          }
          return $fields;
       }
       
       /*static $searchable_fields = array(
             'Nombre',
             'Apellido'
             // leaves out the 'Price' field, removing it from the search
       );*/
       
       public function partido(){
          $quipo = Equipo::get()->filter(array("JugadorID" => $this->id));
          Debug::show($equipo);
       }
    }

    class Puntuacion extends DataObject {
       static $db = array(
          'Puntos' => 'Varchar',
       );
       
       static $has_one = array(
          'Jugador' => 'Jugador',
          'Partido' => 'Partido'
       );
       
       public function getCMSFields() {
          //Debug::show($this);
          $fields = parent::getCMSFields();
          $fields->push(new TextField('Puntos', 'Puntaje'));
                
          $field = new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('ID', "CustomTitle"));
          $field->setDisabled(true);
          $field->performDisabledTransformation();
          $fields->push($field);
             
          $partido = Partido::get()->filter( array("ID" => $this->PartidoID));
          $partido = new TextField("PartidoID", "sarasa", $partido->ID);
          $partido = $partido->setDisabled(true);
          $partido->performDisabledTransformation();
          $fields->push($partido);

          $fields->push($field);
                      
          return $fields;
       }
    }


    In this image the relarionships see better.

    to add a "Puntuacion" are the next steps.

    1. add a new "Partido".
    2. Inside the "partido" whit GridField add a new "Equipo".
    3 Inside the "Equipo" whit another Gridfield, link existings "Jugador". so far so good and working.
    4. inside the "Jugador" whit another gridfield for "Puntuaciones" try to add a "Puntuacion" but wen the screen of new "Puntuacion" shows, I lost the "Partido" relation.
    in other words I dont know to which partido i'm going to record the "Puntuacion", only the "JugadorID" remains, and I have to set manually with a dropdowfiel or something the PartidoID and this is what I dont want.

    I hope you understand what the problem is and any help will be grateful.

    Bye!

    PD: Sorry about my poor english!!!

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: [SOLVED]Lost relationhips reference in nested Gridfields. Link to this post

    Hi again,
    If I understand correctly a Puntuacion can belong to multiple Partido? And a Partido can belong to multiple Puntuacion?
    You might be after a many_many/belongs_many_many relationship
    Remove the has_one/has_many and try adding these.

    class Partido extends Page {
    static $many_many = array(
    'Puntuaciones' => 'Puntuacion
    );
    }

    class Puntuacion extends DataObject {
    static $belongs_many_many = array(
    'Partidos' => 'Partido'
    );
    }

  • T3nD4n
    Avatar
    Community Member
    16 Posts

    Re: [SOLVED]Lost relationhips reference in nested Gridfields. Link to this post

    Hi Bambii7 thankĀ“s for your reply.

    No, a "Puntuacion"(score) belongs to one especific "Jugador"(player) and "Paritdo"(Match) together. so a "Jugador" can have one "Puntuacion" for each "Partido" that he plays.

    as show the image of the tables, the table Puntuacion has PartidoID and JugadorID.

    To add "Puntuacion" I use Gridfields
    here the code:

    class Jugador extends DataObject {
    ......
    ......
    public function getCMSFields() {
          $fields = parent::getCMSFields();
          $jugador = Jugador::get()->byID($this->ID);
          if($jugador){// Player exists
             $config = GridFieldConfig_RelationEditor::create();
             
             $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
                   'Puntos' => 'Puntos'
             ));
             $puntos= new GridField(
                   'Puntuaciones',
                   'Puntuacion',
                   $this->Puntuaciones(),
                   $config
             );
             
             $fields->addFieldToTab('Root.Main', $puntos);
          }
          return $fields;
       }


    My problems resides specificly when i want to add a Puntuacion, because the gridfield seems only to manage the relationship between the "Jugador" and the new "Puntuacion", but the Jugador already has a relationships with a Equipo to which it belongs, and that Equipo belongs to a "Partido". and to add a puntuacion I need the PartidoID too.

    So in class Puntuacion y don`t know how to have access to that PartidoID when I'm adding a new Puntuacion.

    The solution I found is by adding a dropdownField that shows all the Partidos, and I have to manually select the correct Partido.
    but I want to know if exist a way to do it automaticly.

    here the partial solution y found:

    class Puntuacion extends DataObject {
    static $db = array(
    'Puntos' => 'Varchar',
    );

    static $has_one = array(
    'Jugador' => 'Jugador',
    'Partido' => 'Partido'
    );

    public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->push(new TextField('Puntos', 'Puntaje')); // the db field

    $field = new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('ID', "CustomTitle")); //recognized by the relarion in the gridfield, I disabled this for prevent people change the player.
    $field->setDisabled(true);
    $field->performDisabledTransformation();
    $fields->push($field);

    $fields->push( new DropdownField('PartidoID', 'Partido', Partido::get()->map('ID', "CustomTitle")));// Partial solution, selec the Partido manually.

    return $fields;
    }
    }

    I hope you understand a little better.
    Thanks for your time!

    Edit:
    add this image, i thinks explains better what i am talking about.

  • T3nD4n
    Avatar
    Community Member
    16 Posts

    Re: [SOLVED]Lost relationhips reference in nested Gridfields. Link to this post

    Solved

    I get the ID of the partido from the breadcrum and insert to the form corresponding to the Partido.

    class Puntuacion extends DataObject {
       static $db = array(
          'Puntos' => 'Varchar',
       );
       
       static $has_one = array(
          'Jugador' => 'Jugador',
          'Partido' => 'Partido'
       );
       
       public function getCMSFields() {
          //Debug::show();
          
          $fields = parent::getCMSFields();
          $fields->push(new TextField('Puntos', 'Puntaje'));
          $field = new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('ID', "CustomTitle"));
          
          $controller = Controller::curr();
          $breadcrum = $controller->breadcrumbs();

          $partido = $breadcrum ->last();
          $idPartido = end(explode("/",$partido->Link));
          $partido = Partido::get()->byId($idPartido);

       
          $field->setDisabled(true);
          $field->performDisabledTransformation();
          $fields->push($field);
       
          $field = new TextField("PartidoID", "Partido", $partido->ID);
          $fields->push($field);
          $fields->push($field);
       
          return $fields;
          
       }
    }

    277 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.