Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » SS3 Gridfield add

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: 375 Views
  • T3nD4n
    Avatar
    Community Member
    16 Posts

    SS3 Gridfield add Link to this post

    Hi all.
    i have a problem and don't know why.

    here is the gridfield:

    class Partido extends Page {

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

       static $has_many = array(
          //'Jugadores' => 'Jugador'
          '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(
                'Jugador.Nombre' => 'Nombre',
                'Jugador.Apellido'=> 'Apellido',
                'Puntos'=> 'Puntuacion'
          ));
          
          $studentsField = new GridField(
                'Puntuaciones',
                'Puntuacion',
                $this->Puntuaciones(),
                $config
          );
          
          $fields->addFieldToTab('Root.Jugadores', $studentsField);
          return $fields;
       }
    }

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

    works fine, when i click "add" button the admin show me this.

    if I create a record, all is working.
    but is not showing me the name of the "jugador", and have to solve it.

    to solve it i did this to "Puntuacion"

    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'));
                
          $jugador = Jugador::get()->byID($this->JugadorID);
          
          if($jugador){
             $field = new TextField('JugadorID', 'Jugador', $jugador->Apellido);
             $field->setDisabled(true);
             $field->performDisabledTransformation();
             $fields->push($field);
          }else{
             $fields->push(new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('Nombre', 'Apellido')));
          }      
          
          $partido = Partido::get()->byID($this->PartidoID);
          //Debug::show($this);
          
          //Debug::show();
          if($partido){
             $field = new TextField('PartidoID', 'Partido', $partido->Fecha);
             $field->setDisabled(true);
             $field->performDisabledTransformation();
             $fields->push($field);
          }else{
             $fields->push(new DropdownField('PartidoID', 'Partido', Partido::get()->map('Fecha', "Resultado")));
          }
          
                
          return $fields;
          
       }
       
       
    }

    now seems to be solved because now i can see de Lastname (Apellido)

    But when I click the create button the player seems to not being saved

    here is an image.

    any help will be appreciated!!

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: SS3 Gridfield add Link to this post

    You could try adding Summary Fields to Puntuacion, it looks as if there is a record in the last screen shot.

    public static $summary_fields = array('Puntos','Partido.Resultado');

  • T3nD4n
    Avatar
    Community Member
    16 Posts

    Re: SS3 Gridfield add Link to this post

    Hi Bambii7 thanks for your post, i find the problem.

    the error was in the DropdownFields.

    $fields->push(new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('Nombre', 'Apellido')));

    Should be

    $fields->push(new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('ID', 'Apellido')));

    therefore the record in the table Puntuacion was created but JugadorID was empty.

    anyway now i have another problem, in this part of code.

    $jugador = Jugador::get()->byID($this->JugadorID);
    if($jugador){
             $field = new TextField('JugadorID', 'Jugador', $jugador->Apellido);
             $field->setDisabled(true);
             $field->performDisabledTransformation();
             $fields->push($field);
          }else{
             $fields->push(new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('ID', 'Apellido')));
          }   

    I verified if I go to edit a "Puntuacion"or to create a new one.

    when I enter to edit one, i need to do something like this.

    $field = new TextField('JugadorID', 'Jugador', 'JugadorID' => $jugador->Apellido);

    note the third parameter of TExtfield, how can show the name of the "jugador" instead de ID in the textfield, but in the fact when i save it the value saved is the JugadorID

    Thanks!

  • T3nD4n
    Avatar
    Community Member
    16 Posts

    Re: SS3 Gridfield add Link to this post

    Solved
    i change this:

    $jugador = Jugador::get()->byID($this->JugadorID);
    if($jugador){
             $field = new TextField('JugadorID', 'Jugador', $jugador->Apellido);
             $field->setDisabled(true);
             $field->performDisabledTransformation();
             $fields->push($field);
          }else{
             $fields->push(new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('ID', 'Apellido')));
          }   

    for this:

    $jugador = Jugador::get()->byID($this->JugadorID);
    if($jugador){
             $field = new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('ID', "Nombre"));
             $field->setDisabled(true);
             $field->performDisabledTransformation();
             $fields->push($field);
          }else{
             $fields->push(new DropdownField('JugadorID', 'Jugador', Jugador::get()->map('ID', 'Apellido')));
          }   

  • T3nD4n
    Avatar
    Community Member
    16 Posts

    Re: SS3 Gridfield add Link to this post

    Hi all!
    I need help with another thing please.

    once I click "add Puntuacion", how can I get the "ID" of the "Partido" to which I am adding this "Punctuacion" and then map it to show it in DropdownField

    something like this, in the bolded part .

    if($partido){
             
             $field = new DropdownField('PartidoID', 'Partido', Partido::get()->map('ID', "CustomTitle"));
             $field->setDisabled(true);
             $field->performDisabledTransformation();
             $fields->push($field);
          }else{
             $game = Partido::get()->byID("here the ID Of partido");
       
             $game = $game->map('ID',"Title")); //doesn't work map on byID() only on get()

             $fields->push(new DropdownField('PartidoID', 'Partido', $game));
          }
       

  • T3nD4n
    Avatar
    Community Member
    16 Posts

    Re: SS3 Gridfield add Link to this post

    Solved,

    have to change this

    ...
    $config = GridFieldConfig_RecordEditor::create();
    ...

    for this:

    ...
    $config = GridFieldConfig_RelationEditor::create();
    ...

    In the config of the Gridfield, in the Partido Class.
    and automatically adds the "Puntuacion to the corresponding "Partido"

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