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.

General Questions /

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

SS3 Gridfield add


Reply


6 Posts   425 Views

Avatar
T3nD4n

Community Member, 16 Posts

28 May 2013 at 9:53am

Edited: 28/05/2013 9:59am

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!!

Avatar
Bambii7

Community Member, 254 Posts

28 May 2013 at 1:17pm

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');

Avatar
T3nD4n

Community Member, 16 Posts

28 May 2013 at 2:33pm

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!

Avatar
T3nD4n

Community Member, 16 Posts

28 May 2013 at 2:56pm

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')));
      }   

Avatar
T3nD4n

Community Member, 16 Posts

29 May 2013 at 6:41am

Edited: 29/05/2013 6:44am

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));
      }
   

Avatar
T3nD4n

Community Member, 16 Posts

29 May 2013 at 7:31am

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"