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.

General Questions /

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

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

[SOLVED]Lost relationhips reference in nested Gridfields.


Go to End


4 Posts   1321 Views

Avatar
T3nD4n

Community Member, 16 Posts

30 May 2013 at 6:49am

Edited: 30/05/2013 7:03am

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

Avatar
Bambii7

Community Member, 254 Posts

31 May 2013 at 12:57pm

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

Avatar
T3nD4n

Community Member, 16 Posts

31 May 2013 at 2:30pm

Edited: 31/05/2013 2:57pm

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.

Avatar
T3nD4n

Community Member, 16 Posts

7 June 2013 at 8:25am

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