Silverstripe 3.1 - 一名球员有很多球队

时间:2013-10-20 18:45:07

标签: php silverstripe

我正在尝试创建像这样的团队列表。

TeamHolder - 适用于所有团队。 Teampages as children

TeamPage - 团队的描述和图像。并且还与TeamMember(Dataobject)的一个has_one关系

TeamMember - 有成员。在这里你也应该能够定义玩家所在的队伍。这是我的代码。但我不知道如何与我的团队页面建立一个has_many关系,这样你就可以为一个玩家选择多个团队。谁有解决方案?

http://www.sspaste.com/paste/show/526422c0d33bc

事先提前

1 个答案:

答案 0 :(得分:3)

如果您希望1个玩家(TeamMember)参加多个团队,并且团队拥有多个玩家,则需要多个玩家。如果你在这里使用has_many,那么Team将拥有一个PlayerID,这意味着每个团队中只能有1个玩家。

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

请注意,我使用的是GridFieldConfig_RelationEditor而不是GridFieldConfig_RecordEditor,它会在您的网格域中添加GridFieldAddExistingAutocompleter,以便您链接对象。