问题
I'm trying to create a Team List like this.
TeamHolder - for all teams. Has Teampages as children
TeamPage - description and image of the team. And also an has_one relation to TeamMember(Dataobject)
TeamMember - has the members. here you also should be able to define in which teams the player is. here's my code for this. But i don't know how i can make a has_many relation to my team pages, so that you can choose multiple teams for one player. has anyone a solution for this?
http://www.sspaste.com/paste/show/526422c0d33bc
thx in advance
回答1:
if you want 1 Player (TeamMember) to be in multiple Teams, and Teams to have multiple Players, you need a many_many. If you use has_many here, then the Team would have a PlayerID, which means there can only be 1 Player in each team.
class TeamPage extends Page {
private static $many_many = array('TeamMembers' => 'TeamMember');
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root', Tab::create('MembersTab', 'Team Members'));
$fields->addFieldToTab('Root.MembersTab', GridField::create('TeamMembers', 'The Team Members', $this->TeamMembers(), GridFieldConfig_RelationEditor::create());
return $fields;
}
}
class TeamMember extends DataObject {
private static $belongs_many_many = array('Teams' => 'TeamPage');
public function getCMSFields() {
$fields = FieldList::create();
if (!$this->isInDB()) {
// if the TeamMember has not been saved yet, display a message that it needs to be saved before teams can be assigned
$fields->push(ReadOnlyField::create('Teams', '', 'Save to assign Teams'));
} else {
$config = GridFieldConfig_RelationEditor::create();
// if you don't want the "add a new team" button on this grid, you can remove that with the following line:
// $config->removeComponentsByType('GridFieldAddNewButton');
$fields->push(GridField::create('Teams', 'Team this Member is in', $this->Teams(), $config);
}
return $fields;
}
}
NOTE here that I used GridFieldConfig_RelationEditor
instead of GridFieldConfig_RecordEditor
which adds a GridFieldAddExistingAutocompleter
to your gridfield that lets you link objects.
来源:https://stackoverflow.com/questions/19481348/silverstripe-3-1-one-player-has-many-teams