Silverstripe 3.1 - One player has many teams

跟風遠走 提交于 2019-12-11 06:15:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!